简体   繁体   English

如何在子方法中访问子类中的父类变量?

[英]How to access parent class variable in child class inside a child method?

I have a class 我上课了

public class A {
    private String name;

    public void setName(String name){
        this.name = name;
    }

    public string getName(){return name;}

    public String toString() {
       StringBuilder builder = new StringBuilder();
       builder.append(name).append(", ");
       return builder.toString();
    }
}

I have a child class B that extends A and I have to access name from A in toString method in B. I have written the following code but not sure if this a good practice? 我有一个扩展A的子类B,我必须在B中的toString方法中访问A中的名称。我已经编写了以下代码但不确定这是否是一个好习惯?

public class B extends A {
    private String gender;

    public void setGender(String gender){
        this.gender = gender;
    }

    public string getGender(){return gender;}

    @Override
    public String toString() {
       c = new A();
       StringBuilder builder = new StringBuilder();
       builder.append(c.getName()).append(", ");
       builder.append(gender).append(", ");
       return builder.toString();
    }
}

EDIT: If I cannot access private String name in class B, do I have to @Override setName() by using super.setName(name) ? 编辑:如果我不能访问类B中的私有String名称,我是否必须使用super.setName(name)@Override setName() super.setName(name) In that case how different it is from using class B without extending class A if I dont want to use any of the objects of A? 在那种情况下,如果我不想使用A的任何对象,那么在不扩展A类的情况下使用B类有何不同?

I just started using JAVA to modify a service. 我刚开始使用JAVA来修改服务。

When you inherit a class, you also inherit all the public and protected methods and variables from the inherited. 继承类时,还会继承继承的所有公共和受保护方法和变量。 So you can just do 所以你可以做到

@Override
public String toString() {
   StringBuilder builder = new StringBuilder();
   builder.append(this.getName()).append(", ");
   builder.append(this.getGender()).append(", ");
   return builder.toString();
}

simply by calling getName() in your toString method or super.getName() 只需在toString方法或super.getName()调用getName()即可

like: 喜欢:

@Override
public String toString() {
   StringBuilder builder = new StringBuilder();
   builder.append(getName()).append(", ");
   builder.append(gender).append(", ");
   return builder.toString();
}

Never forget the old adage: You don't inherit your parent's privates. 永远不要忘记古老的格言: 你不会继承父母的私生子。

If you want B to have access to A.name , you have a couple of options: 如果您希望B有权访问A.name ,您有几个选择:

  • Change name to be either public (bad) or protected (better) in A . A中将name更改为public (坏)或protected (更好)。
  • Access name through A 's setters and getters, eg this.getName() 通过A的setter和getter访问name ,例如this.getName()
  • Use reflection 使用反射

What you've currently done just returns the default value of A.name and not the value of name that actually belongs to your instance of B . 您当前所做的只是返回默认A.name而不是实际属于您的B实例的名称值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在子类中访问父类的变量 - How To Access Variable of Parent Class In Child Class Java:使用父类方法访问子类变量 - Java : Using parent class method to access child class variable 如何使用子类之外的子引用访问与子变量同名的父类变量? - How to access parent class variable having same name as child variable with child reference outside the child class? 如何通过子类访问父类变量 - How to access a parent class variable via a child class 如何使用父类引用访问子类方法? - How to access child class method using parent class refrence? 为什么我们不能在 java 的子 class 的方法之外访问 class 内部的父实例变量 class? - why can't we access parent class instance variables inside the class outside the method in child class in java? 使用具有子类变量的父类方法而不覆盖 - Using a method of parent class with variable of child class without overriding 父包装子类方法 - Parent wrapping child class method 为什么父类对子类对象的引用不能访问子类变量? - Why parent class ref to child class object cannot access child class variable? 当父类是引用类型并通过方法传递时,如何从子类访问属性? - How can I access an attribute from a child class when the parent class is the reference type and passed through a method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM