You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
700 B

abstract class Animal {
// 抽象方法:动物叫声
public abstract void makeSound();
}
// 2. Swimmable 接口
interface Swimmable {
// 游泳方法
void swim();
}
// 3. Dog 类:继承 Animal,实现 Swimmable
class Dog extends Animal implements Swimmable {
@Override
public void makeSound() {
System.out.println("小狗汪汪叫:汪汪汪!");
}
@Override
public void swim() {
System.out.println("小狗在水里狗刨式游泳!");
}
}
// 4. Cat 类:继承 Animal,不实现 Swimmable
class Cat extends Animal {
@Override
public void makeSound() {
System.out.println("小猫喵喵叫:喵喵喵!");
}
}