繁体   English   中英

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

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

如何访问在其超类中声明的子类的字符串变量?

我有这样的课程:

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值,而不是父类?

有一种非常普通的方法可以做到这一点。 这是一个Java“模式”。 该技术是面向对象设计中使用吸气剂/塞特方法的一个驱动示例。

在父级中声明值A的抽象getter / setter方法, 在子类中完全定义它们。 父级普遍使用getter / setter方法来访问值。

这样,父级和子级将始终使用子级中声明,管理和更新的“ A”值。 获取器和设置器不能是静态的(当然,这没有意义)。 此方法允许父级和子级保持独立,并且可以以非常灵活和简洁的方式修改/调整子级。 特别地,子类不需要主动维护A,并且可以以JIT(即时)方式计算,仔细检查或委托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; }
   . . .
}

(我在编辑问题时正在回答该问题;如果我误解了该问题,表示歉意。)

只要您不使用静态成员,所需的行为就是正常行为。 将所需成员定义为超类中的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 Child2 class Child1class Child2 class Child1中的每个方法上重写该方法以返回各自的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";
   }
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM