From 6214943cbcdfcaf86cdbf385771f8259d3ff430c Mon Sep 17 00:00:00 2001 From: Thea Wang Date: Mon, 13 Apr 2026 17:39:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Animal.class | Bin 0 -> 206 bytes Animal.java | 3 +++ Cat.class | Bin 0 -> 375 bytes Cat.java | 6 ++++++ Dog.class | Bin 0 -> 472 bytes Dog.java | 11 +++++++++++ Main.class | Bin 0 -> 774 bytes Main.java | 25 +++++++++++++++++++++++++ Swimmable.class | Bin 0 -> 120 bytes Swimmable.java | 3 +++ 10 files changed, 48 insertions(+) create mode 100644 Animal.class create mode 100644 Animal.java create mode 100644 Cat.class create mode 100644 Cat.java create mode 100644 Dog.class create mode 100644 Dog.java create mode 100644 Main.class create mode 100644 Main.java create mode 100644 Swimmable.class create mode 100644 Swimmable.java diff --git a/Animal.class b/Animal.class new file mode 100644 index 0000000000000000000000000000000000000000..b05489d14c245388ee868cbcef0faa28fc91506e GIT binary patch literal 206 zcmX9%yAFat5S)cW1o80$7ItbOKVVFZ(aLC{u|MEq4&g`$_+M5g7Jh&qW!#Hf%*@Va zXFlK88^9KW5Dr`)B7%nwVU)|ej06>XiU~{A7nyCcW22utMRk+pos@Rz>hNa zK%y7t&|TAA)iu*!-yfd<&aoe%hPn?Yh6|72ooSVejKPW0%A6AFz5X~vfY3)2V+Apx zc_$u4k_kOa(&q;wb7mElA#_HIjw+JtQfZT#Qi^;KV2$8j%HpXTp^0@Ltr%@=5ZeFH z3I5!QGtKjQ%YjQP!{ynaKlZW7hC{)I&ajwDLSv+~ysGkvEN{dl<4Tx|dzluMo)RL? z<*mF>wh^%I*xI6t4*%Vr8u)$iIBxOnfHQG+kKREq3<%pCZQEkP&M)xQ4wS*8@eA literal 0 HcmV?d00001 diff --git a/Cat.java b/Cat.java new file mode 100644 index 0000000..5a0c4b4 --- /dev/null +++ b/Cat.java @@ -0,0 +1,6 @@ +public class Cat extends Animal { + @Override + public void makeSound() { + System.out.println("Meow!"); + } +} \ No newline at end of file diff --git a/Dog.class b/Dog.class new file mode 100644 index 0000000000000000000000000000000000000000..9fe7a79aeaac0a689120e666e9f970d74498fd7f GIT binary patch literal 472 zcmZuu%TB^j5IqBw+tw-w;`;#$6L0|^fH5&XR>YXp;QAt!Tx{<(rD*&tU1;LM5AdUm zb0NkJn|YpdW-{~r^Z5nf0u3KI_vib8xJ*%1#LRW~=YPg(u(#SVBN3 zkL8o>#8Qts;mah85=NP6MyT~>9c4P#snS`PrIAd!E>;N6tugmJt&CNHN`NZX2-O8} zf_Sv#ShHSpE;t{Rae35j^l!S(Zf*+FD!CEmaEz!LOKx8l$Pz7f_RJnl$xgfRRqi?h zeAb5&ZDRD*|D^HG+SZtKGduU=+1Z~S8RNlZI`NJdEACLA`;I>T6;|j-ug{xR(C|r=v zz(<;F=n+Zf?*UO4#7=x0)g2Q>lsK+gxUL&bKJ}CtC6 zPd-;VIi<-P`u4Dl8a=%JVqp5|X}?WYi6kRAvo;0$L<&QLb`5Jig(mKh)S**7IV9hl zfbUL`YM;O)X>~p$lP?@&u6B&2I{n(VwfzI-53p+AQT~D(4BEd+{(9uk*3bMOk=9U^ zAYDN$s^B3qq$gldu{oN(1=5OCj|~eI!kRRE>&W8?3V4B8I5abR6m^%fGKl}eI!BS# MBB%22MYQ+-01%_0ZvX%Q literal 0 HcmV?d00001 diff --git a/Main.java b/Main.java new file mode 100644 index 0000000..6d6b5ca --- /dev/null +++ b/Main.java @@ -0,0 +1,25 @@ +public class Main { + public static void main(String[] args) { + // 测试多态 + Animal animal1 = new Dog(); + Animal animal2 = new Cat(); + + System.out.println("Testing polymorphism:"); + animal1.makeSound(); // 应该输出 Woof! + animal2.makeSound(); // 应该输出 Meow! + + // 测试 Dog 的 swim 方法 + System.out.println("\nTesting Dog's swim method:"); + if (animal1 instanceof Swimmable) { + ((Swimmable) animal1).swim(); + } + + // 测试 Cat 是否实现了 Swimmable + System.out.println("\nTesting if Cat is Swimmable:"); + if (animal2 instanceof Swimmable) { + ((Swimmable) animal2).swim(); + } else { + System.out.println("Cat is not Swimmable."); + } + } +} \ No newline at end of file diff --git a/Swimmable.class b/Swimmable.class new file mode 100644 index 0000000000000000000000000000000000000000..56507729f3da7ea2cb9d3c0e836ebda27478beff GIT binary patch literal 120 zcmX^0Z`VEs1_oCKPId++Mh4E{^32@a#H5^5b_Nzk27#=^vPAuy#JqHU|D>$c