开发者

Is it me or is there a typo in this courseware entry?

开发者 https://www.devze.com 2022-12-16 07:42 出处:网络
I took this from an online MIT courseware discussion (pdf warning): public class Human { private String name;

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 ;-)

0

精彩评论

暂无评论...
验证码 换一张
取 消