简体   繁体   English

这个java代码如何执行代码?

[英]How does this java code execute code?

I have a project in my programming class and I'm failing a test case. 我的编程课中有一个项目,我没有测试用例。 I look into the test driver and find this: 我查看测试驱动程序并找到:

 private static boolean testSquareArch()
  {
  boolean pass = true;
  int test = 1;
  int cnt;
  Square sq;
  Class cl;

  System.out.println("Square architecture tests...");

  sq = new Square(true, true, true, true, 0, 0);

  cl = sq.getClass();
  cnt = cl.getFields().length;
  pass &= test(cnt == 5, test++);  //FAILING THIS TEST

What does this do and how does it check my code? 这是做什么的以及如何检查我的代码?

Also while I'm here, what does this do? 我也在这里,这又做什么?

  // Count and test number of of PACKAGE fields
  cnt = cl.getDeclaredFields().length
      - countModifiers(cl.getDeclaredFields(), Modifier.PRIVATE)
      - countModifiers(cl.getDeclaredFields(), Modifier.PROTECTED)
      - countModifiers(cl.getDeclaredFields(), Modifier.PUBLIC);
  pass &= test(cnt == 5, test++);  // Test 8 

I'm failing these test cases and just want to know why. 我没有通过这些测试用例,只是想知道原因。 Thanks 谢谢

If you are asking about the &= assignment operator, it keeps the variable true only if the right side argument is also true (if it was already false, it will stay false). 如果您询问&=赋值运算符,只有当右侧参数也为真时,它才会使变量保持为真(如果它已经为假,则它将保持为假)。 It works just like += and a &= b is the same as a = a & b , the operator & being the boolean conjuction (AND). 它的工作方式与+=a &= ba = a & b ,运算符&是布尔结合(AND)。

 pass &= test(cnt == 5, test++); 

is shorthand for 是简写

 if( ! test( cnt == 5, test ) )
     pass = false;
 test++;

I assume that it is part of unit testing code and asserts that cnt == 5, also counting the number of tests and the total result (pass or fail). 我假设它是单元测试代码的一部分,并断言cnt == 5,还计算测试次数和总结果(通过或失败)。

Using Junit, you do not have to manually take care of the number of tests, or the end result, and you could write 使用Junit,您不必手动处理测试次数或最终结果,您可以编写

assertEquals("count is correct", 5, cnt);

This will also produce useful output to help figure out exactly what failed (such as the value of an incorrect count). 这也将产生有用的输出,以帮助确定失败的确切内容(例如错误计数的值)。

它似乎是在检查您在班级中声明了多少个字段,以及其中有多少是私人可见性。

If you're wondering about &= , here's a quote from JLS 15.22.2. 如果你想知道&= ,这是JLS 15.22.2的引用 Boolean Logical Operators &, ^, and | 布尔逻辑运算符&,^和| .

For &, the result value is true if both operand values are true; 对于&,如果两个操作数值都为真,则结果值为true; otherwise, the result is false. 否则,结果是错误的。

The operators & and | 运营商&| for booleans are the lesser known cousins of && ( 15.23 ) and || 对于布尔人来说,是&&15.23 )和||鲜为人知的表兄弟 ( 15.24 ). 15.24 )。 The latter two are "conditional" operators; 后两者是“有条件”的运营商; they short-circuit and only evaluate the right hand side if it's actually needed. 它们是短路的,只有在实际需要时才评估右侧。

The && operator is like & (§15.22.2), but evaluates its right-hand operand only if the value of its left-hand operand is true . &&运算符类似于& (§15.22.2),但仅当其左侧操作数的值为true时才计算其右侧操作数。

&= is the compound assignment version of & . &=是的化合物分配版本& See JLS 15.26.2. JLS 15.26.2。 Compound Assignment Operators . 复合赋值运算符

A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once. 形式E1 op = E2的复合赋值表达式等效于E1 =(T)((E1)op(E2)),其中T是E1的类型,除了E1仅被评估一次。

In other words, these are equivalent (assumed boolean b ): 换句话说,这些是等价的(假设boolean b ):

b &= bool_expr;
b = b & bool_expr;
if (!bool_expr) b = false;

This may also be your issue ( java.lang.Class#getDeclaredFields ): 这也可能是你的问题( java.lang.Class#getDeclaredFields ):

Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object. 返回Field对象的数组,这些对象反映由此Class对象表示的类或接口声明的所有字段。 This includes public, protected, default (package) access, and private fields, but excludes inherited fields . 这包括公共,受保护,默认(包)访问和私有字段, 但不包括继承的字段

Your count may not be correct if you want to also count the inherited fields. 如果您还要计算继承的字段,则计数可能不正确。

In Java the namespaces for variables and methods don't conflict. 在Java中,变量和方法的名称空间不会发生冲突。 So it looks like the driver has both a variable and a method called "test". 所以看起来驱动程序既有变量又有一个名为“test”的方法。

Without the test() method and your own source code, it is hard to say what is wrong, but it looks like you have the wrong number of fields in your Square class. 如果没有test()方法和您自己的源代码,很难说出了什么问题,但看起来您的Square类中的字段数量是错误的。 (There should be five.) (应该有五个。)

是的,似乎需要更多的字段来完成声明,这应该修复命名空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM