java - 使用子类和父类(super class)的不死族和吸血鬼程序

标签 java subclass superclass

class Undead{
   protected String food;
   protected String comesOut;
   Undead(){

   }
   Undead(String f){
       food = f;
   }
   public void setFood(String f){
       food = f;
   }
   public String getFood(){
       return food;
   }
   public void eats(){
       System.out.println("The undead eats "+ food);
   }
   public void comesOut(){
       System.out.println("The undead can come out" + comesOut);
   }

}

class Vampire extends Undead {
   private String name ;
   Vampire(){
   }
   Vampire(String n){
       name = n;
   }
   Vampire(String n, String f){
       name = n;
       food = f;
   }
   public void setName(String n){
       name = n;
   }
   public String getName(){
       return name;
   }
   public void eats(){
       System.out.println(name + "drinks" + food);
   }
   public void comesOut(){
       System.out.println(name + "comes out"+ comesOut);
   }
}

我的不死父类(super class)和吸血鬼子类的代码如下。

当我尝试在一个名为 coderunner 的网站上运行它时;该网站没有给我主要功能,这个问题也没有给我示例输出。

我收到以下错误:

Syntax Error(s)
__Tester__.java:62: cannot find symbol
symbol : method setComesOut(java.lang.String)
location: class Undead
u.setComesOut("at anytime");
^
__Tester__.java:69: cannot find symbol
symbol : method setComesOut(java.lang.String)
location: class Vampire
v1.setComesOut("at night");
^
2 errors

我在 eclipse 上尝试了这个,似乎没有任何错误。知道问题出在哪里。

编辑:

The superclass Undead contains

A protected String variable food.
A private String variable comesOut.
Two constructors
- one takes no parameter.
- one takes a String parameter food and assigns this value to the variable food.
A public method setFood() that takes a String parameter and sets the variable food to the parameter's value.
A public method getFood() that returns the value of food.
A public method eats() that prints "The undead eats [food]".
A public method comesOut() that prints "The undead can come out [comesOut]".

The subclass Vampire contains

A private String variable name.
Three constructors.
- one takes no parameter.
- one takes a String parameter name and assigns this value to the variable name.
- one takes two String parameters name and food and sets the variables name and food to these values.  
A public setName() method that takes a String parameter and sets name to the value of this parameter.
A public getName() method that returns the value of name.
A public method eats() that prints "[name] drinks [food]".
A public method comesOut() that prints "[name] comes out [comesOut]"

最佳答案

您需要添加一个名为“setComesOut”的方法,该方法将字符串作为参数。此函数应将 Undead 变量“comesOut”设置为给定的任何参数,类似于“setName”或“setFood”方法。

关于java - 使用子类和父类(super class)的不死族和吸血鬼程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43670983/

相关文章:

widget - 在 GTK+ 的 .gui 文件中制作已知的子类小部件

python - 在 python 中派生类方法的正确方法是什么?

iphone - 为什么 Apple 不允许 UINavigationController 的子类化?除了子类化之外,我还有哪些选择?

java - 为什么我不能在子类构造函数中添加额外的参数验证?

java - 如果子类中的实例方法具有相同的签名和不同的返回类型,它是覆盖方法还是新方法?

java - 如何从 Java 字符串中提取未知长度的值?

java - 我应该处理从 EditorSupport 返回的 jface CellEditors

java - 无法理解递归阶乘

java - 500 内部服务器错误,获取请求时出现 NullPointerException

swift - 我可以在不进行子类化的情况下覆盖 func mouseDown 吗?