Browse Source

上传文件至 'w6'

main
wangbo 4 days ago
parent
commit
06cfc07fe1
  1. 37
      w6/Main.java

37
w6/Main.java

@ -0,0 +1,37 @@
abstract class Animal {
abstract void makeSound();
}
interface Swimmable {
void swim();
}
class Dog extends Animal implements Swimmable {
public void makeSound() {
System.out.println("Woof!");
}
public void swim() {
System.out.println("Dog is swimming!");
}
}
class Cat extends Animal {
public void makeSound() {
System.out.println("Meow!");
}
}
public class Main {
public static void main(String[] args) {
Animal myDog = new Dog();
Animal myCat = new Cat();
myDog.makeSound();
myCat.makeSound();
if (myDog instanceof Swimmable) {
((Swimmable) myDog).swim();
}
}
}
Loading…
Cancel
Save