Browse Source

W6-高钰铎-202506050304

main
Gaoyuduo 1 month ago
parent
commit
97b13e317e
  1. 3
      6.1.txt
  2. 3
      6.2.txt
  3. 11
      6.3.txt
  4. 6
      6.4.txt
  5. 15
      6.5.txt

3
6.1.txt

@ -0,0 +1,3 @@
public abstract class Animal {
public abstract void makeSound();
}

3
6.2.txt

@ -0,0 +1,3 @@
public interface Swimmable {
void swim();
}

11
6.3.txt

@ -0,0 +1,11 @@
public class Dog extends Animal implements Swimmable {
@Override
public void makeSound() {
System.out.println("狗:汪汪汪");
}
@Override
public void swim() {
System.out.println("狗会游泳");
}
}

6
6.4.txt

@ -0,0 +1,6 @@
public class Cat extends Animal {
@Override
public void makeSound() {
System.out.println("猫:喵喵喵");
}
}

15
6.5.txt

@ -0,0 +1,15 @@
public class TestAnimal {
public static void main(String[] args) {
Animal dog = new Dog();
Animal cat = new Cat();
dog.makeSound();
cat.makeSound();
// 向下转型调用游泳方法
if (dog instanceof Swimmable) {
Swimmable swimmable = (Swimmable) dog;
swimmable.swim();
}
}
}
Loading…
Cancel
Save