From 22bfc89b1c7fecfb1ce0d8d360a0a76ef19c1b1c Mon Sep 17 00:00:00 2001 From: XuJingwang Date: Tue, 14 Apr 2026 14:44:18 +0800 Subject: [PATCH] =?UTF-8?q?W6-=E5=BE=90=E6=99=AF=E6=97=BA-202414010701?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- W6/AnimalTest.java | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 W6/AnimalTest.java diff --git a/W6/AnimalTest.java b/W6/AnimalTest.java new file mode 100644 index 0000000..b49305e --- /dev/null +++ b/W6/AnimalTest.java @@ -0,0 +1,42 @@ +abstract class Animal { + public abstract void makeSound(); +} +interface Swimmable { + void swim(); +} +class Dog extends Animal implements Swimmable { + @Override + public void makeSound() { + System.out.println("汪汪汪"); + } + @Override + public void swim() { + System.out.println("狗会游泳"); + } +} +class Cat extends Animal { + @Override + public void makeSound() { + System.out.println("喵喵喵"); + } +} +public class AnimalTest { + public static void main(String[] args) { + Animal dog = new Dog(); + Animal cat = new Cat(); + System.out.println("叫声"); + dog.makeSound(); + cat.makeSound(); + System.out.println("游泳"); + if (dog instanceof Swimmable) { + ((Swimmable) dog).swim(); + } else { + System.out.println("不会游泳"); + } + if (cat instanceof Swimmable) { + ((Swimmable) cat).swim(); + } else { + System.out.println("不会游泳"); + } + } +} \ No newline at end of file