如何从父类的方法访问子类的继承成员

How to access a child's inherited member from a parent's method

我如何访问在其 superclass 中声明的子 class' 字符串变量?

我有 class 是这样的:

public class Parent {
  public void display() {
    // displays A
  }
}

public class Child1 extends Parent {
}

public class Child2 extends Parent {
}

Parent p1 = new Child1();
Parent p2 = new Child2();

p1.display(); // Currently displaying parent value of A
p2.display(); // Currently displaying parent value of A

我如何确保在这个 display() 中,p1p2 都使用它们自己的 A 值,而不是父 classes?

(我正在回答我编辑过的问题;如果我误解了这个问题,我深表歉意。)

只要您不使用静态成员,所需的行为就是正常行为。在超类中将需要的成员定义为protected;这三个 类 中任何一个的每个实例都将使用自己的值。

public class Parent {
  protected string name;

  public Parent() {
    name = "Parent's value";
  }

  public void setName(string newValue) {
    name = newValue;
  }

  public void display() {
    System.out.println(name);
  }
}

public class Child1 extends Parent {
  public Child1() {
    name = "Child1's value";
  }
}

public class Child2 extends Parent {
  public Child2() {
    name = "Child2's value";
  }
}

public static void main() {
  new Parent().display();
  new Child1().display();
  Child2 c2 = new Child2();
  c2.display();
  c2.setName("New name!");
  c2.display();
}

这会产生预期的输出:

Parent's value
Child1's value
Child2's value
New name!

或者,如果有一种方法可以获取 A 的值,例如 class Parent 中的 getA(),则将每个 class Child1class Child2 中的方法重写为return他们各自的A值。

我重复使用了@paul-hicks 的示例,并在下面演示了我的方法:

public class Parent
    ...
    public void display() {
      System.out.println(getA());
    }

    protected String getName() {
      return "parent";
    }
}

class Child1 {
   ...
   protected String getName() {
      return "child1";
   }
}

class Child2 {
   ...
   protected String getName() {
      return "child2";
   }
}

有一种非常常见的方法可以做到这一点。这是一个java'pattern'。此技术是在面向 object 的设计中使用 getter/setter 方法的驱动示例。

在 parent 中为值 A 声明抽象 getter/setter 方法,并在 child class 中完全定义它们. parent 普遍使用 getter/setter 来访问值。

这样,parent 和 children 将始终使用 'A' 中声明、管理和更新的值 child class。 getter 和 setter 不能是静态的(当然,这没有意义)。这种方法允许 parent 和 child classes 保持独立,并且 children 可以以非常灵活和干净的方式 modified/adjusted。特别是,childclasses不需要主动维护A,可以JIT(just-in-time)方式计算、复查或委托对A的请求。

完整示例:

public abstract class Parent {
  // These two methods must be overridden by all child classes. Compiler enforces that.
  public abstract int getA();
  public abstract int setA( int newA );
        // This display routine utilizes the *childs* a
  public void display() { someoutput.write( this.getA() ); }
}

public class Child1 extends Parent {
  SomeFancyOrPartularFormOfA a = null; // a Jazzy type of A, used by parent.
  @Override // Let java and IDEs know we intend to override it.
  public int getA() { return( this.a.gatherorcalcA() ); }
  @Override // Let java and IDEs know we intend to override it.
  public int setA( int newA ) { this.a.setorincorporateA( newA ); }
   . . .
}
public class Child2 extends Parent {
  Integer a = 0;  // A more mundane type of A, still used by parent.
  @Override // Let java and IDEs know we intend to override it.
  public int getA() { return( this.a ); }
  @Override // Let java and IDEs know we intend to override it.
  public int setA( int newA ) { this.a = newA; }
   . . .
}