Which three statements are true about the structure of a Java class?
A. A class can have only one private constructor.
B. A method can have the same name as a field.
C. A class can have overloaded static methods.
D. A public class must have a main method.
E. The methods are mandatory components of a class.
F. The fields need not be initialized before use.
题解
选项 A,一个类别只能拥有一个 private 修饰的私有构造器。这是错的,Java 并没有限制私有构造器的数量,举例来说:
public class Test {
private Test(){
}
private Test(int a){
}
private Test(String s){
}
}
选项 B,一个方法的名称可以和字段名称相同。这是对的,举例来说:
public class Test {
int A;
int A(){
return A;
}
}
选项 C,一个类别可以拥有多载(overload)的静态方法。这是对的,举例来说:
public class Test {
public static void test(String a, String b){
}
public static void test(int a, int b){
}
}
选项 D,一个公开的类别并不一定要有 main 方法。
选项 E,并不是每个类别都需要撰写方法,甚至类别内什么都不写也可以。
选项 F,字段在声明的时候就会自动被初始化为默认值,所以使用前不需要自行初始化,这是对的。
