【问题标题】:test case error square(int) has private access in fox测试用例错误 square(int) 在 Fox 中具有私有访问权限
【发布时间】:2021-04-23 02:11:34
【问题描述】:

每次编译时都会遇到一个错误,提示 square(int) 在 fox 中具有私有访问权限 并突出显示此代码“assertEquals(25, tod.square(5), .001);”我不知道为什么它一直这么说,代码对我来说看起来不错。这应该是代码的测试类,即使不需要。 下面是完整代码

public class FoxTest extends TestCase
{
    //~ Fields ................................................................


    //~ Constructor ...........................................................

    // ----------------------------------------------------------
    /**
     * Creates a new FoxTest test object.
     */
    public FoxTest()
    {
        // The constructor is usually empty in unit tests, since it runs
        // once for the whole class, not once for each test method.
        // Per-test initialization should be placed in setUp() instead.
    }


    //~ Methods ...............................................................

    // ----------------------------------------------------------
    /**
     * Sets up the test fixture.
     * Called before every test case method.
     */
    public void setUp()
    {
        /*# Insert your own setup code here */
    }


    // ----------------------------------------------------------
    /**
       * test the constructor
       */
      public void testConst()
      {
          Fox tod = new Fox();
          assertEquals(3, tod.getSpeed());
      }
    /**
      * test the distance to
      */
     public void testDistance()
     {
         Fox tod = new Fox();
         tod.setGridX(0);
         tod.setGridY(0);
         Fox ring = new Fox();
         ring.setGridX(0);
         ring.setGridY(3);
         assertEquals(3, ring.distanceTo(tod), .001);
     }
     
    /**
      * test the nearestRabbit
      */
     public void testNear()
     {
         Field field = new Field(400, 400, 0, 0);
         Fox tod = new Fox();
         field.add(tod, 0, 0);
         Rabbit reng = new Rabbit();
         field.add(reng, 0, 5);
         Rabbit ring = new Rabbit();
         field.add(ring, 0, 3);
         Rabbit rong = new Rabbit();
         field.add(rong, 0, 6);
         assertEquals(ring, tod.nearestRabbit());
        }
    /**
      * test the nearestRabbit
      */
     public void testNearNull()
      {
          Field field = new Field(400, 400, 0, 0);
          Fox tod = new Fox();
          field.add(tod, 0, 0);
          assertEquals(null, tod.nearestRabbit());
            }
    /**
      * test the turn()
      */
     public void testTurn()
      {
          Field field = new Field(400, 400, 0, 0);
          Fox tod = new Fox();
          field.add(tod, 0, 0);
          Rabbit reng = new Rabbit();
          field.add(reng, 0, 5);
          Rabbit ring = new Rabbit();
          field.add(ring, 1, 1);
          Rabbit rong = new Rabbit();
          field.add(rong, 0, 6);
          tod.turn();
          assertEquals(45, tod.getRotation(), .001);
      }
    /**
      * test square
      */
     public void testSquare()
      {
          Fox tod = new Fox();
          assertEquals(25, tod.square(5), .001);
      }
    /**
      * test the act() method
      */
     public void testAct()
      {
          Field field = new Field(400, 400, 0, 0);
          Fox tod = new Fox();
          field.add(tod, 0, 0);
          Rabbit ring = new Rabbit();
          field.add(ring, 7, 7);
          tod.act();
          assertEquals(45, tod.getRotation(), .001);
      }
    /**
     * test the act() method
     */
    public void testActDone()
    {
        Field field = new Field(400, 400, 0, 0);
        Fox tod = new Fox();
        field.add(tod, 0, 0);
        Rabbit reng = new Rabbit();
        field.add(reng, 0, 5);
        Rabbit ring = new Rabbit();
        field.add(ring, 1, 1);
        Rabbit rong = new Rabbit();
        field.add(rong, 0, 6);
        tod.act();
        List<Rabbit> rabbits = field.getObjects(Rabbit.class);
        assertEquals(2, rabbits.size());
    }
    /**
     */
    public void testTurnNull()
    {
        Field field = new Field(400, 400, 0, 0);
        Fox tod = new Fox();
        field.add(tod, 0, 0);
        assertEquals(null, tod.nearestRabbit());
        tod.turn();
        assertEquals(0, tod.getRotation(), .001);
    }
}

【问题讨论】:

  • Fox 类的代码在哪里?什么是测试用例?你在使用一些测试框架吗?请详细说明
  • 给出的代码是Foxclass,需要TestCase,包括Foxclass在内的4个类扩展为TestCase。我遇到的问题是上面给出的 Foxclass 中的一个错误,每次我编译它时都会说 square(int) 在 fox 中具有私有访问权限。

标签: java computer-science


【解决方案1】:

让我猜,应该有:

public class Fox {
  private int square(int x) {
    return x * x;
  }
}

应该是这样的

public class Fox {
  public int square(int x) {
    return x * x;
  }
}

【讨论】:

  • 哦,我把我的方法私下而不是公开了哇,我太笨了,谢谢你
  • 我想投票,但我需要 15 个声望,抱歉
  • @GideonAgyemangPrempeh 因为这是你的问题,你可以等一天然后接受这个答案(等待被认为是礼貌的)。这比投票要好。
猜你喜欢
  • 2016-06-20
  • 2020-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-27
  • 2018-11-29
  • 2023-03-23
  • 2014-11-24
相关资源
最近更新 更多