java吧 关注:1,237,523贴子:12,707,731
  • 4回复贴,共1

菜鸟求助~

只看楼主收藏回复


public Animal{
private String name;
private int age;
private String color;
public Animal(){}
public Animal(String name,int age,String color){
this.name = name;
this.age = age;
this.color = color;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public int getAge(){
return age;
}
public void setAge(int age){
this.age = age;
}
public String getColor(){
return color;
}
public void setColor(String color){
this.color = color;
}
public void eat(){
System.out.println("我喜欢吃好吃的~");
}
public void show(){
System.out.println("我叫"+getName()+"今年"+getAge()+"岁了,"+"我是"+getColor()+"颜色的。");
}
}
class Cat extends Animal{
public Cat(){}
public Cat(String name,int age,String color){
super(name,age,color);
}
public void playGame(){
System.out.println("我喜欢玩儿游戏~");
}
}
class Dog extends Animal{
public Dog(){}
public Dog(String name,int age,String color){
super(name,age,color);
}
public void lookDoor(){
System.out.println("我要把门看好~");
}
}
class Demo{
public static void main(String[] args){
//方式一:
Cat c = new Cat();
c.setName("咪咪");
c.setAge(3);
c.setColor("黑色");
c.show();
System.out.println("----------");
Dog d = new Dog();
d.setName("大黄");
d.setAge(2);
d.setColor("橘黄色");
d.show();
System.out.println("----------");
System.out.println("----------");
//方式二:
Cat c = new Cat("咪咪",3,"黑色");
Dog d = new Dog("大黄",2,"黄色");
c.show();
d.show();
}
}


1楼2016-04-27 23:48回复
    为什么都忘记输入输出包io.*啊,试着在程序前加上
    import java.io.*;


    来自Android客户端2楼2016-04-27 23:56
    收起回复
      你第一个Animal类 没有 class,public class Animal{ }


      IP属地:北京4楼2016-04-28 08:28
      收起回复