Java 访问超类中方法的变量
我有一个基本的代码,有一个动物类,一个狗和一个猫子类。我有说话的方法。speak方法接收一个字符串,并以猫和狗的“语言”返回一个字符串。如果字符的ascii码为偶数,则返回“uff”,否则返回“vau”。当我重写该方法时,我想从Dog类中设置oddSound和evenSound,但我找不到合适的方法来实现这一点。Java 访问超类中方法的变量,java,class,variables,Java,Class,Variables,我有一个基本的代码,有一个动物类,一个狗和一个猫子类。我有说话的方法。speak方法接收一个字符串,并以猫和狗的“语言”返回一个字符串。如果字符的ascii码为偶数,则返回“uff”,否则返回“vau”。当我重写该方法时,我想从Dog类中设置oddSound和evenSound,但我找不到合适的方法来实现这一点。 此代码来自动物类: public String speak(String what){ String speakableString = new String(); S
此代码来自动物类:
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;
}
Animal类应该声明您的speak方法,因为这对于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;
}
}
在
Animal
类中,有两个受保护的字段
protected String oddSound;
protected String evenSound;
然后,在Dog
和Cat
类中,可以设置以下字段:
oddSound = "woof";
evenSound = "woofwoof"
然后,在
speak()
方法中,您可以简单地使用this.oddSound
和this.evenSound
将它们保留为数据成员,并在各自的构造函数中设置它们:
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");
}
}
公共类动物{
私人弦乐;
私人弦乐;
受保护动物(弦乐、弦乐){
this.oddSound=oddSound;
this.evenSound=evenSound;
}
公共字符串讲话(字符串内容){
String speakableString=新字符串();
for(int i=0;i
您可以将speak实现分为两部分,以便传入要用于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");
}
公共字符串讲话(字符串内容){
返回getSpeakableString(what,new String(),new String());
}
受保护的字符串getSpeakableString(字符串what、字符串oddSound、字符串evenSound){
//这只是从你的问题中复制出来的,它很可能没有达到你真正想要的效果。
String speakableString=新字符串();
for(int i=0;i
.concat()
不会改变任何东西。如果在修复.concat()后问题仍然存在
语句,请发布相关的继承代码。听起来像oddSound
和evenSound
不应该是方法的局部变量,而应该是Dog
或Cat
类的属性。这不是一个解决错误的问题。我将通过编辑该问题来尝试更清楚地说明这一点。@mattm t这实际上是个好主意。你能详细解释一下吗?我如何从动物中访问猫的私有变量呢?事实上,这完全回答了我的问题,但另一个答案有点“更好”.是的,这是我第一次做练习时做的。但是我的老师说这不是最好的方法,我应该找到一种新的方法来做。考虑到这是一种方法,传递它们可能不是最好的主意,我们可以简单地设置一个字段或属性。
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");
}
}
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");
}