public class jicheng {
public static void main(String args[]){
rabbit b1=new rabbit("兔子");
tiger b2=new tiger("老虎");
b1.print();
b1.eat();
b1.sleep;
System.out.println("");
b2.print();
b2.eat();
b2.sleep();
}
}
class animal{
protected String name;
public void eat(){
System.out.println("吃啊");
}
public void sleep(){
System.out.println("睡觉");
}
}
class rabbit extends animal{
public rabbit(String name){
this.name=name;
}
public void eat(){
System.out.println("吃草");
}
public void sleep(){
System.out.println("睡觉");
}
}
class tiger extends animal{
public tiger(String name){
this.name=name;
}
public void eat(){
System.out.println("吃肉");
}
public void sleep(){
System.out.println("睡觉");
}
}
public class jicheng { public static void main(String args[]){ rabbit b1=new rabbit("兔子"); tiger b2=new tiger("老虎"); //要打印出动物名字,需要获取name System.out.println(b1.name); b1.eat(); b1.sleep(); System.out.println(""); System.out.println(b2.name); b2.eat(); b2.sleep(); } } class animal{ protected String name; public void eat(){ System.out.println("吃啊"); } public void sleep(){ System.out.println("睡觉"); } } class rabbit extends animal{ public rabbit(String name){ this.name=name; } public void eat(){ System.out.println("吃草"); } public void sleep(){ System.out.println("睡觉"); } } class tiger extends animal{ public tiger(String name){ this.name=name; } public void eat(){ System.out.println("吃肉"); } public void sleep(){ System.out.println("睡觉"); } }
哦哦,对了额,谢谢啊
输出的结果是兔子吃草睡觉,老虎吃肉睡觉,可是运行结果不对,把这个b1.print();删除后也还是不行,是怎么回事啊,求大神解答
package guqingtong;
public class HelloWorld {
public static void main(String args[]){
rabbit b1=new rabbit("兔子");
tiger b2=new tiger("老虎");
b1.eat();
b1.sleep();
System.out.println("");
b2.eat();
b2.sleep();
}
}
class animal{
protected String name;
public void eat(){
System.out.println("吃啊");
}
public void sleep(){
System.out.println("睡觉");
}
}
class rabbit extends animal{
public rabbit(String name){
this.name=name;
}
public void eat(){
System.out.println("吃草");
}
public void sleep(){
System.out.println("睡觉");
}
}
class tiger extends animal{
public tiger(String name){
this.name=name;
}
public void eat(){
System.out.println("吃肉");
}
public void sleep(){
System.out.println("睡觉");
}
}
//b1.print();这个方法你没有定义啊 ,怎么调用