给定以下 scala 类:
class Student (_name:String, _id:Long) {
private var name:String = _name;
private var id:Long = _id;
// 2nd C'tor
def this(_name:String) = this(_name,0);
// 3rd C'tor
def this(_id:Long) = this("No Name",_id);
def printDetails() {
println("The student's name is : " + name);
println("The student's id is : " + id);
}
}
和以下 Java 类:
public class StudentReflectionDemo {
public static void main (String[] args) {
try {
Class cl = Class.forName("ClassesAndObjects.Student");
Method[] methods = cl.getMethods();
Field[] fields = cl.getFields();
System.out.println("The methods of the Student class are : ");
for (int i = 0 ; i < methods.length; i++) {
System.out.println(methods[i]);
}
System.out.println("The fields of the Student class are : ");
for (int i = 0 ; i < fields.length; i++) {
System.out.println(fields[i]);
}
}
catch(ClassNotFoundException e) {
e.printStackTrace();
}
}
}
它确实正确输出学生类方法,但它不打印学生的类字段..
我在这里想念什么?
谢谢