I took this from an online MIT courseware discussion (pdf warning):
public class Human {
private String name;
...
public Human(String name) {
this.name = name;
}
public String getName() {
return String;
}
}
public class Student extends Human {
private String username;
public Student(String name, String username) {
super(name);
this.username = username;
}
public String getName() {
return username;
}
public String getRealName() {
return super.getName();
}
}
...
public class World {
...
void someMethod() {
Student alice = new Student("Alice", "abc");
System.out.println(alice.getRealName()开发者_JS百科); // what gets printed?
Why does getRealName return anything. I know it returns Alice because the constructor is called by super(name) but my question is about:
return String;
Why doesn't getName in the Human class have to be
return name;
It should be. It's a typo. This code as you have pasted it would not compile.
You are correct. It's a typo and should be return name
.
Please notify the instructor, or the contact person for the class, so they can update the pdf.
unless the three-dot-area contains something like
private String String = "Alice";
but, nay, I guess it's a typo ;-)
精彩评论