为什么从子类调用超类方法时我的值为空?

Why is my value null when calling superclass method from subclass?

我正在与 Java 和 Android Studio IDE 合作。我正在尝试从它的子 class 中调用 parent class 中的方法。我写了一个小测试例程,但我只得到一个空结果。

在我的主要项目中,我有 Parent 和 Child Objects/Classes,我想从 Parent Object/Class 调用的 Object/Class 中获取一个值SubClass/Object。 (House Object 由 Wall Object 组成,我的 Wall Object 正试图获得 House Class 内指定的 "Wall Type"。但下面是一个小例子我正在使用的东西。

我已经尝试搜索这个问题,但是多次阅读别人的代码只会让这个问题对我来说更加混乱。我一定误解了这应该如何工作。

我尝试重写获取数据的方式,方法是调用 super.getMethod() 或重写 Parent 的方法。我也玩过我的 variables/methods 被 public/private 保护,但一无所获。

我的ParentClass:

public class Parent {
    protected String myName;
    protected Child parentsChild;

    public Parent() {}

    public Parent (String myName, Child parentsChild) {
        this.myName = myName;
        this.parentsChild = parentsChild;
    }

    public String getMyName() {
        return this.myName;
    }

    public void setMyName(String myName) {
        this.myName = myName;
    }

    public Child getParentsChild() {
        return this.parentsChild;
    }

    public void setParentsChild(Child parentsChild) {
        this.parentsChild = parentsChild;
    }
}

我的ChildClass:

public class Child extends Parent {
    String childName;

    public Child() {

    }

    public Child(String childName) {
        this.childName = childName;
    }

    public String getChildName() {
        return childName;
    }

    public void setChildName(String childName) {
        this.childName = childName;
    }

    public String getParentName() {
        return super.getMyName();
    }

    @Override
    public String getMyName() {
        return super.getMyName();
    }
}

我的主要活动:


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        Child child = new Child("Logan");

        Parent parent = new Parent("Karrie", child);

        System.out.println("Parent: My name is " + parent.getMyName());
        System.out.println("Child: My parent's name is " + child.getParentName());
        System.out.println("@Override-> Child: My Parent's name is " + child.getMyName());
    }
}

我希望在使用 "Karrie" 构建 class Parent 时检索 Parent 的名称。相反,我只得到 "null"。好可怜的child! :-(

这是我的日志:

2019-10-17 20:25:36.594 27040-27040/com.example.android.superclassinheritancetesting I/System.out: Parent: My name is Karrie
2019-10-17 20:25:36.594 27040-27040/com.example.android.superclassinheritancetesting I/System.out: Child: My parent's name is null
2019-10-17 20:25:36.594 27040-27040/com.example.android.superclassinheritancetesting I/System.out: @Override-> Child: My Parent's name is null

非常感谢您的帮助! :]

正如评论中所指出的,在这种情况下继承可能不是正确的方法。

您可以定义单个 Node class 来为关系建模。

class Node {

  String name;
  Node parent;

  public Node(String name) {
    this.name = name;
  }

  public Node(String name, Node parent) {
    this.name = name;
    this.parent = parent;
  }

  public String getParentName() {
    return this.parent != null ? this.parent.name : null;
  }

}

然后在您的客户端代码中:

Node parent = new Node("ParentName");
Node child = new Node("ChildName", parent)
System.out.println(child.getParentName());

发生这种情况是因为您在 parent 中添加 child 值的方式。

您需要通过 child class 构造函数传递这些值。我建议您在 Parent 中创建其他构造,您只能在其中接收 myName 属性和 Child class 您使用 super 将 myName (parentName) 传递给 Parent,就像您在 get 方法中使用的那样。可以这样做:

Parent 中添加(或更改)新构造:

public Parent (String myName) {
   this.myName = myName;
}

并且在Child中:

public Child(String childName, String parentName) {
    super(parentName)
    this.childName = childName;
}

并且在 onCreate 方法中,您只需要创建一个 child 实例,如下所示:

Child child = new Child("Logan", "Karrie"); 

这样,Parent class 就不需要知道和包含 Child class 变量了。

正如其他人所说,这里使用继承可能不是实现所需功能的正确方法。对 java 教程的参考肯定会帮助您理解继承并将其与组合或聚合区分开来。您的示例同时使用了两者,使得初学者很难掌握这些概念。如果您仍然对层次关系感兴趣(parent/child,而不是 super/sub class),请查看 JDK 中的此接口及其实现。有一些方法可以在层次结构中获取父对象和子对象。 https://docs.oracle.com/javase/8/docs/api/javax/swing/tree/TreeNode.html