黑马程序员技术交流社区

标题: 为什么“吃肉”读取不出来 [打印本页]

作者: MagicBoy    时间: 2014-3-18 23:26
标题: 为什么“吃肉”读取不出来
//父类
public class Animal {
String sleep,eat;
  public Animal(){
  System.out.println("睡觉");
//程序说吃肉未被读取。哪里出错了啊?
}

}
//子类
public class Lion extends Animal {
public Lion(String eat){
this.eat=eat;
System.out.println("狮子是吃"+this.eat);
}
public static void main(String[] args){
Rabbit a=new Rabbit("吃肉");
}



作者: 方青木    时间: 2014-3-19 00:10
Rabbit a=new Rabbit("吃肉");
应该是Lion a = new Lion("吃肉");
作者: 戒爱    时间: 2014-3-19 00:20
Rabbit不是一个类,不可创建实例对象
作者: 王竞    时间: 2014-3-19 00:22
朋友,你从哪里跑出来的Rabbit,你就没有定义Rabbit这个类啊
作者: MagicBoy    时间: 2014-3-19 00:59
王竞 发表于 2014-3-19 00:22
朋友,你从哪里跑出来的Rabbit,你就没有定义Rabbit这个类啊

中间代码搞掉了,本来是定义了狮子和兔子两个类
作者: Sage    时间: 2014-3-19 10:52
  1. public class Inherit {

  2.         /**
  3.          * 继承
  4.          */
  5.         public static void main(String[] args) {
  6.                 new Lion("肉");
  7.         }

  8. }

  9. // 动物类
  10. class Animal {
  11.         private String sleep;
  12.         private String eat;

  13.         public String getSleep() {
  14.                 return sleep;
  15.         }

  16.         public void setSleep(String sleep) {
  17.                 this.sleep = sleep;
  18.         }

  19.         public String getEat() {
  20.                 return eat;
  21.         }

  22.         public void setEat(String eat) {
  23.                 this.eat = eat;
  24.         }

  25.         public Animal() {
  26.                 System.out.println("睡觉");
  27.         }
  28. }

  29. // 狮子类
  30. class Lion extends Animal {
  31.         public Lion(String eat) {
  32.                 System.out.println("狮子是吃" + eat);
  33.         }
  34. }
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2