我正在使用Java和Android Studio IDE。我正在尝试从子类的父类中调用方法。我已经编写了一个小的测试例程,但结果却是空的。
在我的主项目中,我有父级和子级对象/类,我想从子类/对象中调用的父级对象/类中获取一个值。 (房屋对象由墙对象组成,我的墙对象试图获取房屋类中指定的“墙类型”。但是下面是我正在使用的小示例。
我已经尝试搜索此问题,但是多次阅读其他人的代码只会使我感到困惑。我一定会误解这应该如何工作。
我已经尝试通过调用然后调用super.getMethod()或重写Parent方法来重写如何获取数据。我也玩过我的变量/方法是公共/私有和受保护的方法,但没有取得任何成就。
我的父母班级:
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;
}
}
我的孩子班级:
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”构造类Parent时通过它来获取Parent的名称。相反,我只会得到“ null”。多么可怜的孩子! :-(
这是我的日志:
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 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中创建其他构造,在该构造中您只能接收myName属性,在Child类中,您可以使用super将myName(parentName)传递给Parent,就像您在get中使用的那样方法。可以做如下:
在(父)中添加(或更改)新结构:public Parent (String myName) {
this.myName = myName;
}
并且在:Child
public Child(String childName, String parentName) {
super(parentName)
this.childName = childName;
}
并且在方法中,您只需要创建一个子实例,如下所示:onCreate
Child child = new Child("Logan", "Karrie");
通过这种方式,Parent类不需要知道并包含Child类变量。