CoreJava/exp4/AnimalTool.java
2024-05-09 09:10:51 +08:00

63 lines
1.0 KiB
Java

package exp4;
abstract class Animal{
String name;
int age;
public Animal(){};
public Animal(String name, int age){
this.name = name;
this.age = age;
}
abstract void eat();
abstract void show();
}
class Cat extends Animal{
public Cat(){};
public Cat(String name, int age){
super(name, age);
}
void eat(){
System.out.println("猫吃鱼");
}
void show(){
System.out.println(this.name+","+this.age);
}
}
class Dog extends Animal{
public Dog(){};
public Dog(String name, int age){
super(name, age);
}
void eat(){
System.out.println("狗吃肉");
}
void show(){
System.out.println(this.name+","+this.age);
}
}
public class AnimalTool {
public static void main(String[] args){
Animal a = new Cat();
Animal b = new Dog("旺财", 3);
a.name = "Tom";
a.age = 999;
a.show();
b.show();
a.eat();
b.eat();
}
}