问题与练习:类

Questions

public class IdentifyMyParts {
    public static int x = 7; 
    public int y = 3; 
}
IdentifyMyParts a = new IdentifyMyParts();
IdentifyMyParts b = new IdentifyMyParts();
a.y = 5;
b.y = 6;
a.x = 1;
b.x = 2;
System.out.println("a.y = " + a.y);
System.out.println("b.y = " + b.y);
System.out.println("a.x = " + a.x);
System.out.println("b.x = " + b.x);
System.out.println("IdentifyMyParts.x = " + IdentifyMyParts.x);

Exercises

Hint:

您可以使用assert语句检查分配。你写:

assert (boolean expression to test);

如果布尔表达式为 false,您将收到一条错误消息。例如,

assert toString(ACE) == "Ace";

应该返回true,因此不会出现错误消息。

如果使用assert语句,则必须使用ea标志运行程序:

java -ea YourProgram.class

检查一下你的答案。

首页