From 97b13e317e3a8d885cd1f143dffe7ac362772f60 Mon Sep 17 00:00:00 2001 From: Gaoyuduo Date: Thu, 7 May 2026 15:34:27 +0800 Subject: [PATCH] =?UTF-8?q?W6-=E9=AB=98=E9=92=B0=E9=93=8E-202506050304?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 6.1.txt | 3 +++ 6.2.txt | 3 +++ 6.3.txt | 11 +++++++++++ 6.4.txt | 6 ++++++ 6.5.txt | 15 +++++++++++++++ 5 files changed, 38 insertions(+) create mode 100644 6.1.txt create mode 100644 6.2.txt create mode 100644 6.3.txt create mode 100644 6.4.txt create mode 100644 6.5.txt diff --git a/6.1.txt b/6.1.txt new file mode 100644 index 0000000..0e98992 --- /dev/null +++ b/6.1.txt @@ -0,0 +1,3 @@ +public abstract class Animal { + public abstract void makeSound(); +} \ No newline at end of file diff --git a/6.2.txt b/6.2.txt new file mode 100644 index 0000000..26c5a27 --- /dev/null +++ b/6.2.txt @@ -0,0 +1,3 @@ +public interface Swimmable { + void swim(); +} \ No newline at end of file diff --git a/6.3.txt b/6.3.txt new file mode 100644 index 0000000..d7f462e --- /dev/null +++ b/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("狗会游泳"); + } +} \ No newline at end of file diff --git a/6.4.txt b/6.4.txt new file mode 100644 index 0000000..af09a09 --- /dev/null +++ b/6.4.txt @@ -0,0 +1,6 @@ +public class Cat extends Animal { + @Override + public void makeSound() { + System.out.println("猫:喵喵喵"); + } +} \ No newline at end of file diff --git a/6.5.txt b/6.5.txt new file mode 100644 index 0000000..865dd33 --- /dev/null +++ b/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(); + } + } +} \ No newline at end of file