【问题标题】:Is it me or is there a typo in this courseware entry?是我还是这个课件条目有错字?
【发布时间】:2010-01-15 17:21:35
【问题描述】:

我从在线MIT courseware discussion(pdf 警告)中获取此信息:

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()); // what gets printed?

为什么 getRealName 会返回任何内容。我知道它返回 Alice 因为构造函数是由 super(name) 调用的,但我的问题是:

return String;

为什么 Human 类中的 getName 不必是

return name;

【问题讨论】:

  • 我为什么要关闭?只是好奇。
  • 我讨厌在没有 cmets 的情况下关闭——但我猜这是因为它是一个非常直接的错误,甚至不会编译——它更像是一个校对问题而不是编程问题。

标签: java return


【解决方案1】:

应该是的。这是一个错字。您粘贴的这段代码无法编译。

【讨论】:

  • 我想我被麻省理工学院的消息给迷惑了,所以我想一定是我错了,而不是他们。
【解决方案2】:

你是对的。这是一个错字,应该是return name

请通知讲师或课程联系人,以便他们更新 pdf。

【讨论】:

    【解决方案3】:

    除非三点区域包含类似的东西

    private String String = "Alice";
    

    但是,不,我想这是一个错字;-)

    【讨论】:

    • 令人惊讶的是这会编译。 :)
    猜你喜欢
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-21
    相关资源
    最近更新 更多