簡體   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