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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40618029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:49:41  来源:igfitidea点击:

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

java

提问by spod

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

编辑:如果我无法访问 B 类中的私有字符串名称,是否必须@Override setName()使用super.setName(name)?在那种情况下,如果我不想使用 A 的任何对象,它与使用 B 类而不扩展 A 类有什么不同?

I just started using JAVA to modify a service.

我刚开始使用JAVA来修改一个服务。

采纳答案by usha

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

回答by patrz jaka franca

simply by calling getName()in your toString method or super.getName()

只需调用getName()您的 toString 方法或super.getName()

like:

喜欢:

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

回答by Eric M.

Never forget the old adage: You don't inherit your parent's privates.

永远不要忘记古老的格言:您不会继承父母的隐私。

If you want Bto have access to A.name, you have a couple of options:

如果您想B访问A.name,您有几个选择:

  • Change nameto be either public(bad) or protected(better) in A.
  • Access namethrough A's setters and getters, e.g. this.getName()
  • Use reflection
  • 在 中更改namepublic(坏)或protected(更好)A
  • name通过A的 setter 和 getter访问,例如this.getName()
  • 使用反射

What you've currently done just returns the defaultvalue of A.nameand not the value of name that actually belongs to your instance of B.

您当前所做的只是返回的默认值,A.name而不是实际属于您的实例的 name 值B