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