[英]Accessing variable of a method in superclass
我有一個基本代碼,包含Animal類和Dog和Cat子類。 我有說話的方法。 speak方法接收一個字符串,並以貓和狗“語言”返回一個字符串。 如果一個字符的ascii代碼是偶數,則返回“uff”,如果不是,則返回“vau”。 當我重寫方法時,我想從Dog類中設置oddSound和evenSound,但我找不到合適的方法來執行此操作。
此代碼來自Animal類:
public String speak(String what){
String speakableString = new String();
String oddSound = new String();
String evenSound = new String();
for (int i = 0; i < what.length(); i++) {
if((((int) what.charAt(i)) & 1) == 1){
speakableString.concat(oddSound);
}else if ((((int) what.charAt(i)) & 1) == 0){
speakableString.concat(evenSound);
}
}
speakableString = speakableString.substring(0, speakableString.length()-1);
return speakableString;
}
此代碼來自Dog類:
public String speak(String what){
//set oddSound = "vau"
//set evenSound = "uff"
return super.speak(what);
}
在Animal
類中,有兩個受保護的字段,
protected String oddSound;
protected String evenSound;
然后,在Dog
和Cat
類中,您可以設置以下字段:
oddSound = "woof";
evenSound = "woofwoof"
然后,在speak()
方法中,您可以簡單地使用this.oddSound
和this.evenSound
Animal類應聲明您的發言方法,因為這對Dog和Cat類來說都很常見。
public class Animal {
String oddSound;
String evenSound;
public Animal(String oddSound, String evenSound) {
this.oddSound = oddSound;
this.evenSound = evenSound;
}
public String speak(String what){
String speakableString = new String();
for (int i = 0; i < what.length(); i++) {
if((((int) what.charAt(i)) & 1) == 1){
speakableString = speakableString.concat(oddSound);
}else if ((((int) what.charAt(i)) & 1) == 0){
speakableString = speakableString.concat(evenSound);
}
}
speakableString = speakableString.substring(0, speakableString.length()-1);
return speakableString;
}
}
請記住,string.concat方法創建一個新對象,它不會修改調用它的實例。 請參閱String API文檔
您的Dog和Cat類應僅在需要時定義或覆蓋方法。 看來你在他們的發言方法中所做的就是調用超級方法實現。 你可以完全擺脫它。
如果要擴展Animal類以提供新方法或實現,請執行以下操作:
public class Dog extends Animal {
public Dog() {
// This calls the super constructor, which sets the oddSound and evenSound fields
super("vau", "uff");
}
// This section does nothing.
// A method implementation which only calls its super implementation is ineffective.
// If you were to provide a new implementation, this is where it would be.
//public String speak(String what) {
// super.speak(what);
//}
}
您可以將它們保存為數據成員,並將它們設置在相應的構造函數中:
public class Animal {
private String oddSound;
private String evenSound;
protected Animal (String oddSound, String evenSound) {
this.oddSound = oddSound;
this.evenSound = evenSound;
}
public String speak(String what){
String speakableString = new String();
for (int i = 0; i < what.length(); i++) {
if((((int) what.charAt(i)) & 1) == 1){
speakableString = speakableString.concat(oddSound);
}else if ((((int) what.charAt(i)) & 1) == 0){
speakableString = speakableString.concat(evenSound);
}
}
}
public class Dog extends Animal {
public Dog() {
super ("uff", "vau");
}
}
您可以將說話實現分成兩部分,以允許您傳入要用於oddSound
和evenSound
。
public String speak(String what){
return getSpeakableString(what, new String(), new String());
}
protected String getSpeakableString(String what, String oddSound, String evenSound){
//this is just copied from what you had in your question, it likely doesn't do what you actually want.
String speakableString = new String();
for (int i = 0; i < what.length(); i++) {
if((((int) what.charAt(i)) & 1) == 1){
speakableString.concat(oddSound);
}else if ((((int) what.charAt(i)) & 1) == 0){
speakableString.concat(evenSound);
}
}
speakableString = speakableString.substring(0, speakableString.length()-1);
return speakableString;
}
//in subclass
@Override
public String speak(String what){
return getSpeakableString(what, "vau", "uff");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.