From 65ff699bbb04f5be165dba0aac83a132aa3c727b Mon Sep 17 00:00:00 2001 From: KN321136 <1280568951@qq.com> Date: Mon, 27 Oct 2025 18:54:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ruan-jian-main/LICENSE | 194 ++++++++++++++++++ ruan-jian-main/advanced/Animal.java | 18 ++ ruan-jian-main/advanced/Bird.java | 10 + ruan-jian-main/advanced/Cat.java | 10 + ruan-jian-main/advanced/Dog.java | 10 + ruan-jian-main/advanced/Main.java | 11 + ruan-jian-main/basic/Animal.java | 15 ++ ruan-jian-main/basic/Fish.java | 16 ++ ruan-jian-main/basic/Main.java | 7 + ruan-jian-main/challenge/Animal.java | 15 ++ ruan-jian-main/challenge/Bird.java | 10 + ruan-jian-main/challenge/Cat.java | 10 + ruan-jian-main/challenge/Dog.java | 10 + ruan-jian-main/challenge/Main.java | 22 ++ ruan-jian-main/challenge/Pet.java | 3 + .../src/main/java/ConcreteProductA.java | 6 + .../src/main/java/ConcreteProductB.java | 6 + ruan-jian-main/src/main/java/Factory.java | 13 ++ ruan-jian-main/src/main/java/FactoryTest.java | 17 ++ ruan-jian-main/src/main/java/Product.java | 3 + ruan-jian-main/基础题/Rectangle.java | 26 +++ ruan-jian-main/基础题/Shape.java | 7 + ruan-jian-main/基础题/ShapeCalculator.java | 21 ++ ruan-jian-main/基础题/Square.java | 19 ++ ruan-jian-main/挑战题/ListInterface.java | 8 + ruan-jian-main/挑战题/MapInterface.java | 8 + .../挑战题/QuickSortStrategy.java | 13 ++ ruan-jian-main/挑战题/SetInterface.java | 8 + ruan-jian-main/挑战题/SimpleArrayList.java | 24 +++ ruan-jian-main/挑战题/SimpleHashMap.java | 21 ++ ruan-jian-main/挑战题/SimpleHashSet.java | 21 ++ ruan-jian-main/挑战题/SortStrategy.java | 8 + ruan-jian-main/挑战题/Sorter.java | 20 ++ ruan-jian-main/进阶题/Bird.java | 13 ++ .../进阶题/CreditCardPayment.java | 11 + ruan-jian-main/进阶题/Flyable.java | 6 + ruan-jian-main/进阶题/FlyingBird.java | 9 + ruan-jian-main/进阶题/PayPalPayment.java | 11 + .../进阶题/PaymentProcessor.java | 7 + ruan-jian-main/进阶题/Penguin.java | 13 ++ 40 files changed, 680 insertions(+) create mode 100644 ruan-jian-main/LICENSE create mode 100644 ruan-jian-main/advanced/Animal.java create mode 100644 ruan-jian-main/advanced/Bird.java create mode 100644 ruan-jian-main/advanced/Cat.java create mode 100644 ruan-jian-main/advanced/Dog.java create mode 100644 ruan-jian-main/advanced/Main.java create mode 100644 ruan-jian-main/basic/Animal.java create mode 100644 ruan-jian-main/basic/Fish.java create mode 100644 ruan-jian-main/basic/Main.java create mode 100644 ruan-jian-main/challenge/Animal.java create mode 100644 ruan-jian-main/challenge/Bird.java create mode 100644 ruan-jian-main/challenge/Cat.java create mode 100644 ruan-jian-main/challenge/Dog.java create mode 100644 ruan-jian-main/challenge/Main.java create mode 100644 ruan-jian-main/challenge/Pet.java create mode 100644 ruan-jian-main/src/main/java/ConcreteProductA.java create mode 100644 ruan-jian-main/src/main/java/ConcreteProductB.java create mode 100644 ruan-jian-main/src/main/java/Factory.java create mode 100644 ruan-jian-main/src/main/java/FactoryTest.java create mode 100644 ruan-jian-main/src/main/java/Product.java create mode 100644 ruan-jian-main/基础题/Rectangle.java create mode 100644 ruan-jian-main/基础题/Shape.java create mode 100644 ruan-jian-main/基础题/ShapeCalculator.java create mode 100644 ruan-jian-main/基础题/Square.java create mode 100644 ruan-jian-main/挑战题/ListInterface.java create mode 100644 ruan-jian-main/挑战题/MapInterface.java create mode 100644 ruan-jian-main/挑战题/QuickSortStrategy.java create mode 100644 ruan-jian-main/挑战题/SetInterface.java create mode 100644 ruan-jian-main/挑战题/SimpleArrayList.java create mode 100644 ruan-jian-main/挑战题/SimpleHashMap.java create mode 100644 ruan-jian-main/挑战题/SimpleHashSet.java create mode 100644 ruan-jian-main/挑战题/SortStrategy.java create mode 100644 ruan-jian-main/挑战题/Sorter.java create mode 100644 ruan-jian-main/进阶题/Bird.java create mode 100644 ruan-jian-main/进阶题/CreditCardPayment.java create mode 100644 ruan-jian-main/进阶题/Flyable.java create mode 100644 ruan-jian-main/进阶题/FlyingBird.java create mode 100644 ruan-jian-main/进阶题/PayPalPayment.java create mode 100644 ruan-jian-main/进阶题/PaymentProcessor.java create mode 100644 ruan-jian-main/进阶题/Penguin.java diff --git a/ruan-jian-main/LICENSE b/ruan-jian-main/LICENSE new file mode 100644 index 0000000..f63f5a9 --- /dev/null +++ b/ruan-jian-main/LICENSE @@ -0,0 +1,194 @@ +木兰宽松许可证,第2版 + +木兰宽松许可证,第2版 + +2020年1月 http://license.coscl.org.cn/MulanPSL2 + +您对“软件”的复制、使用、修改及分发受木兰宽松许可证,第2版(“本许可证”)的如下条款的约束: + +0. 定义 + +“软件” 是指由“贡献”构成的许可在“本许可证”下的程序和相关文档的集合。 + +“贡献” 是指由任一“贡献者”许可在“本许可证”下的受版权法保护的作品。 + +“贡献者” 是指将受版权法保护的作品许可在“本许可证”下的自然人或“法人实体”。 + +“法人实体” 是指提交贡献的机构及其“关联实体”。 + +“关联实体” 是指,对“本许可证”下的行为方而言,控制、受控制或与其共同受控制的机构,此处的控制是 +指有受控方或共同受控方至少50%直接或间接的投票权、资金或其他有价证券。 + +1. 授予版权许可 + +每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的版权许可,您可 +以复制、使用、修改、分发其“贡献”,不论修改与否。 + +2. 授予专利许可 + +每个“贡献者”根据“本许可证”授予您永久性的、全球性的、免费的、非独占的、不可撤销的(根据本条规定 +撤销除外)专利许可,供您制造、委托制造、使用、许诺销售、销售、进口其“贡献”或以其他方式转移其“贡 +献”。前述专利许可仅限于“贡献者”现在或将来拥有或控制的其“贡献”本身或其“贡献”与许可“贡献”时的“软 +件”结合而将必然会侵犯的专利权利要求,不包括对“贡献”的修改或包含“贡献”的其他结合。如果您或您的“ +关联实体”直接或间接地,就“软件”或其中的“贡献”对任何人发起专利侵权诉讼(包括反诉或交叉诉讼)或 +其他专利维权行动,指控其侵犯专利权,则“本许可证”授予您对“软件”的专利许可自您提起诉讼或发起维权 +行动之日终止。 + +3. 无商标许可 + +“本许可证”不提供对“贡献者”的商品名称、商标、服务标志或产品名称的商标许可,但您为满足第4条规定 +的声明义务而必须使用除外。 + +4. 分发限制 + +您可以在任何媒介中将“软件”以源程序形式或可执行形式重新分发,不论修改与否,但您必须向接收者提供“ +本许可证”的副本,并保留“软件”中的版权、商标、专利及免责声明。 + +5. 免责声明与责任限制 + +“软件”及其中的“贡献”在提供时不带任何明示或默示的担保。在任何情况下,“贡献者”或版权所有者不对 +任何人因使用“软件”或其中的“贡献”而引发的任何直接或间接损失承担责任,不论因何种原因导致或者基于 +何种法律理论,即使其曾被建议有此种损失的可能性。 + +6. 语言 + +“本许可证”以中英文双语表述,中英文版本具有同等法律效力。如果中英文版本存在任何冲突不一致,以中文 +版为准。 + +条款结束 + +如何将木兰宽松许可证,第2版,应用到您的软件 + +如果您希望将木兰宽松许可证,第2版,应用到您的新软件,为了方便接收者查阅,建议您完成如下三步: + +1, 请您补充如下声明中的空白,包括软件名、软件的首次发表年份以及您作为版权人的名字; + +2, 请您在软件包的一级目录下创建以“LICENSE”为名的文件,将整个许可证文本放入该文件中; + +3, 请将如下声明文本放入每个源文件的头部注释中。 + +Copyright (c) [Year] [name of copyright holder] +[Software Name] is licensed under Mulan PSL v2. +You can use this software according to the terms and conditions of the Mulan +PSL v2. +You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 +THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY +KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO +NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +See the Mulan PSL v2 for more details. + +Mulan Permissive Software License,Version 2 + +Mulan Permissive Software License,Version 2 (Mulan PSL v2) + +January 2020 http://license.coscl.org.cn/MulanPSL2 + +Your reproduction, use, modification and distribution of the Software shall +be subject to Mulan PSL v2 (this License) with the following terms and +conditions: + +0. Definition + +Software means the program and related documents which are licensed under +this License and comprise all Contribution(s). + +Contribution means the copyrightable work licensed by a particular +Contributor under this License. + +Contributor means the Individual or Legal Entity who licenses its +copyrightable work under this License. + +Legal Entity means the entity making a Contribution and all its +Affiliates. + +Affiliates means entities that control, are controlled by, or are under +common control with the acting entity under this License, ‘control’ means +direct or indirect ownership of at least fifty percent (50%) of the voting +power, capital or other securities of controlled or commonly controlled +entity. + +1. Grant of Copyright License + +Subject to the terms and conditions of this License, each Contributor hereby +grants to you a perpetual, worldwide, royalty-free, non-exclusive, +irrevocable copyright license to reproduce, use, modify, or distribute its +Contribution, with modification or not. + +2. Grant of Patent License + +Subject to the terms and conditions of this License, each Contributor hereby +grants to you a perpetual, worldwide, royalty-free, non-exclusive, +irrevocable (except for revocation under this Section) patent license to +make, have made, use, offer for sale, sell, import or otherwise transfer its +Contribution, where such patent license is only limited to the patent claims +owned or controlled by such Contributor now or in future which will be +necessarily infringed by its Contribution alone, or by combination of the +Contribution with the Software to which the Contribution was contributed. +The patent license shall not apply to any modification of the Contribution, +and any other combination which includes the Contribution. If you or your +Affiliates directly or indirectly institute patent litigation (including a +cross claim or counterclaim in a litigation) or other patent enforcement +activities against any individual or entity by alleging that the Software or +any Contribution in it infringes patents, then any patent license granted to +you under this License for the Software shall terminate as of the date such +litigation or activity is filed or taken. + +3. No Trademark License + +No trademark license is granted to use the trade names, trademarks, service +marks, or product names of Contributor, except as required to fulfill notice +requirements in section 4. + +4. Distribution Restriction + +You may distribute the Software in any medium with or without modification, +whether in source or executable forms, provided that you provide recipients +with a copy of this License and retain copyright, patent, trademark and +disclaimer statements in the Software. + +5. Disclaimer of Warranty and Limitation of Liability + +THE SOFTWARE AND CONTRIBUTION IN IT ARE PROVIDED WITHOUT WARRANTIES OF ANY +KIND, EITHER EXPRESS OR IMPLIED. IN NO EVENT SHALL ANY CONTRIBUTOR OR +COPYRIGHT HOLDER BE LIABLE TO YOU FOR ANY DAMAGES, INCLUDING, BUT NOT +LIMITED TO ANY DIRECT, OR INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING +FROM YOUR USE OR INABILITY TO USE THE SOFTWARE OR THE CONTRIBUTION IN IT, NO +MATTER HOW IT’S CAUSED OR BASED ON WHICH LEGAL THEORY, EVEN IF ADVISED OF +THE POSSIBILITY OF SUCH DAMAGES. + +6. Language + +THIS LICENSE IS WRITTEN IN BOTH CHINESE AND ENGLISH, AND THE CHINESE VERSION +AND ENGLISH VERSION SHALL HAVE THE SAME LEGAL EFFECT. IN THE CASE OF +DIVERGENCE BETWEEN THE CHINESE AND ENGLISH VERSIONS, THE CHINESE VERSION +SHALL PREVAIL. + +END OF THE TERMS AND CONDITIONS + +How to Apply the Mulan Permissive Software License,Version 2 +(Mulan PSL v2) to Your Software + +To apply the Mulan PSL v2 to your work, for easy identification by +recipients, you are suggested to complete following three steps: + +i. Fill in the blanks in following statement, including insert your software +name, the year of the first publication of your software, and your name +identified as the copyright owner; + +ii. Create a file named "LICENSE" which contains the whole context of this +License in the first directory of your software package; + +iii. Attach the statement to the appropriate annotated syntax at the +beginning of each source file. + +Copyright (c) [Year] [name of copyright holder] +[Software Name] is licensed under Mulan PSL v2. +You can use this software according to the terms and conditions of the Mulan +PSL v2. +You may obtain a copy of Mulan PSL v2 at: + http://license.coscl.org.cn/MulanPSL2 +THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY +KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO +NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +See the Mulan PSL v2 for more details. diff --git a/ruan-jian-main/advanced/Animal.java b/ruan-jian-main/advanced/Animal.java new file mode 100644 index 0000000..19fb966 --- /dev/null +++ b/ruan-jian-main/advanced/Animal.java @@ -0,0 +1,18 @@ +public abstract class Animal { + protected String name; + + public Animal(String name) { + this.name = name; + } + + // 进阶题要求:抽象方法 makeSound + public abstract void makeSound(); + + public void eat() { + System.out.println(name + " is eating."); + } + + public String getName() { + return name; + } +} diff --git a/ruan-jian-main/advanced/Bird.java b/ruan-jian-main/advanced/Bird.java new file mode 100644 index 0000000..a4ce6e4 --- /dev/null +++ b/ruan-jian-main/advanced/Bird.java @@ -0,0 +1,10 @@ +public class Bird extends Animal { + public Bird(String name) { + super(name); + } + + @Override + public void makeSound() { + System.out.println(getName() + " says: Chirp!"); + } +} diff --git a/ruan-jian-main/advanced/Cat.java b/ruan-jian-main/advanced/Cat.java new file mode 100644 index 0000000..dc12a1a --- /dev/null +++ b/ruan-jian-main/advanced/Cat.java @@ -0,0 +1,10 @@ +public class Cat extends Animal { + public Cat(String name) { + super(name); + } + + @Override + public void makeSound() { + System.out.println(getName() + " says: Meow!"); + } +} diff --git a/ruan-jian-main/advanced/Dog.java b/ruan-jian-main/advanced/Dog.java new file mode 100644 index 0000000..6330b30 --- /dev/null +++ b/ruan-jian-main/advanced/Dog.java @@ -0,0 +1,10 @@ +public class Dog extends Animal { + public Dog(String name) { + super(name); + } + + @Override + public void makeSound() { + System.out.println(getName() + " says: Woof!"); + } +} diff --git a/ruan-jian-main/advanced/Main.java b/ruan-jian-main/advanced/Main.java new file mode 100644 index 0000000..76592c2 --- /dev/null +++ b/ruan-jian-main/advanced/Main.java @@ -0,0 +1,11 @@ +public class Main { + public static void main(String[] args) { + Animal dog = new Dog("Buddy"); + Animal cat = new Cat("Kitty"); + Animal bird = new Bird("Tweety"); + + dog.makeSound(); + cat.makeSound(); + bird.makeSound(); + } +} diff --git a/ruan-jian-main/basic/Animal.java b/ruan-jian-main/basic/Animal.java new file mode 100644 index 0000000..928237f --- /dev/null +++ b/ruan-jian-main/basic/Animal.java @@ -0,0 +1,15 @@ +public class Animal { + protected String name; + + public Animal(String name) { + this.name = name; + } + + public void eat() { + System.out.println(name + " is eating."); + } + + public String getName() { + return name; + } +} diff --git a/ruan-jian-main/basic/Fish.java b/ruan-jian-main/basic/Fish.java new file mode 100644 index 0000000..5b4b165 --- /dev/null +++ b/ruan-jian-main/basic/Fish.java @@ -0,0 +1,16 @@ +public class Fish extends Animal { + private double swimSpeed; // m/s + + public Fish(String name, double swimSpeed) { + super(name); + this.swimSpeed = swimSpeed; + } + + public void swim() { + System.out.println(getName() + " is swimming at " + swimSpeed + " m/s."); + } + + public double getSwimSpeed() { + return swimSpeed; + } +} diff --git a/ruan-jian-main/basic/Main.java b/ruan-jian-main/basic/Main.java new file mode 100644 index 0000000..dc216ca --- /dev/null +++ b/ruan-jian-main/basic/Main.java @@ -0,0 +1,7 @@ +public class Main { + public static void main(String[] args) { + Fish nemo = new Fish("Nemo", 1.5); + nemo.eat(); + nemo.swim(); + } +} diff --git a/ruan-jian-main/challenge/Animal.java b/ruan-jian-main/challenge/Animal.java new file mode 100644 index 0000000..928237f --- /dev/null +++ b/ruan-jian-main/challenge/Animal.java @@ -0,0 +1,15 @@ +public class Animal { + protected String name; + + public Animal(String name) { + this.name = name; + } + + public void eat() { + System.out.println(name + " is eating."); + } + + public String getName() { + return name; + } +} diff --git a/ruan-jian-main/challenge/Bird.java b/ruan-jian-main/challenge/Bird.java new file mode 100644 index 0000000..14c3f48 --- /dev/null +++ b/ruan-jian-main/challenge/Bird.java @@ -0,0 +1,10 @@ +public class Bird extends Animal { + public Bird(String name) { + super(name); + } + + // Bird 不实现 Pet + public void fly() { + System.out.println(getName() + " is flying."); + } +} diff --git a/ruan-jian-main/challenge/Cat.java b/ruan-jian-main/challenge/Cat.java new file mode 100644 index 0000000..3083e81 --- /dev/null +++ b/ruan-jian-main/challenge/Cat.java @@ -0,0 +1,10 @@ +public class Cat extends Animal implements Pet { + public Cat(String name) { + super(name); + } + + @Override + public void play() { + System.out.println(getName() + " plays with a ball of yarn."); + } +} diff --git a/ruan-jian-main/challenge/Dog.java b/ruan-jian-main/challenge/Dog.java new file mode 100644 index 0000000..d458a16 --- /dev/null +++ b/ruan-jian-main/challenge/Dog.java @@ -0,0 +1,10 @@ +public class Dog extends Animal implements Pet { + public Dog(String name) { + super(name); + } + + @Override + public void play() { + System.out.println(getName() + " plays fetch."); + } +} diff --git a/ruan-jian-main/challenge/Main.java b/ruan-jian-main/challenge/Main.java new file mode 100644 index 0000000..4cdbcdc --- /dev/null +++ b/ruan-jian-main/challenge/Main.java @@ -0,0 +1,22 @@ +public class Main { + public static void main(String[] args) { + Dog dog = new Dog("Buddy"); + Cat cat = new Cat("Kitty"); + Bird bird = new Bird("Tweety"); + + // 调用 Pet 接口方法 + dog.play(); + cat.play(); + + // Bird 没有实现 Pet,所以不能调用 play() + bird.fly(); + + // 也可以通过 instanceof 检查 + if (dog instanceof Pet) { + System.out.println(dog.getName() + " is a pet."); + } + if (!(bird instanceof Pet)) { + System.out.println(bird.getName() + " is not a Pet implementation."); + } + } +} diff --git a/ruan-jian-main/challenge/Pet.java b/ruan-jian-main/challenge/Pet.java new file mode 100644 index 0000000..2120b95 --- /dev/null +++ b/ruan-jian-main/challenge/Pet.java @@ -0,0 +1,3 @@ +public interface Pet { + void play(); +} diff --git a/ruan-jian-main/src/main/java/ConcreteProductA.java b/ruan-jian-main/src/main/java/ConcreteProductA.java new file mode 100644 index 0000000..3c3c0c4 --- /dev/null +++ b/ruan-jian-main/src/main/java/ConcreteProductA.java @@ -0,0 +1,6 @@ +public class ConcreteProductA implements Product { + @Override + public void operation() { + System.out.println("ConcreteProductA operation"); + } +} \ No newline at end of file diff --git a/ruan-jian-main/src/main/java/ConcreteProductB.java b/ruan-jian-main/src/main/java/ConcreteProductB.java new file mode 100644 index 0000000..0f17fd9 --- /dev/null +++ b/ruan-jian-main/src/main/java/ConcreteProductB.java @@ -0,0 +1,6 @@ +public class ConcreteProductB implements Product { + @Override + public void operation() { + System.out.println("ConcreteProductB operation"); + } +} \ No newline at end of file diff --git a/ruan-jian-main/src/main/java/Factory.java b/ruan-jian-main/src/main/java/Factory.java new file mode 100644 index 0000000..159f8c0 --- /dev/null +++ b/ruan-jian-main/src/main/java/Factory.java @@ -0,0 +1,13 @@ +public class Factory { + public static Product createProduct(String type) { + if (type == null) { + return null; + } + if (type.equalsIgnoreCase("A")) { + return new ConcreteProductA(); + } else if (type.equalsIgnoreCase("B")) { + return new ConcreteProductB(); + } + return null; + } +} \ No newline at end of file diff --git a/ruan-jian-main/src/main/java/FactoryTest.java b/ruan-jian-main/src/main/java/FactoryTest.java new file mode 100644 index 0000000..5960b43 --- /dev/null +++ b/ruan-jian-main/src/main/java/FactoryTest.java @@ -0,0 +1,17 @@ +public class FactoryTest { + public static void main(String[] args) { + // 测试创建ConcreteProductA + Product productA = Factory.createProduct("A"); + System.out.println("创建产品A:"); + productA.operation(); + + // 测试创建ConcreteProductB + Product productB = Factory.createProduct("B"); + System.out.println("\n创建产品B:"); + productB.operation(); + + // 测试无效类型 + Product invalidProduct = Factory.createProduct("C"); + System.out.println("\n创建无效产品:" + (invalidProduct == null ? "返回null" : "失败")); + } +} \ No newline at end of file diff --git a/ruan-jian-main/src/main/java/Product.java b/ruan-jian-main/src/main/java/Product.java new file mode 100644 index 0000000..466a88e --- /dev/null +++ b/ruan-jian-main/src/main/java/Product.java @@ -0,0 +1,3 @@ +public interface Product { + void operation(); +} \ No newline at end of file diff --git a/ruan-jian-main/基础题/Rectangle.java b/ruan-jian-main/基础题/Rectangle.java new file mode 100644 index 0000000..100e9ab --- /dev/null +++ b/ruan-jian-main/基础题/Rectangle.java @@ -0,0 +1,26 @@ +/** + * 矩形类:有宽和高。没有提供会破坏子类的可变 setter(例如 Square) + * 因此我们把 Rectangle 设计为独立的 Shape 子类(而不是让 Square 继承 Rectangle)。 + */ +public class Rectangle extends Shape { + private final double width; + private final double height; + + public Rectangle(double width, double height) { + this.width = width; + this.height = height; + } + + public double getWidth() { + return width; + } + + public double getHeight() { + return height; + } + + @Override + public double getArea() { + return width * height; + } +} diff --git a/ruan-jian-main/基础题/Shape.java b/ruan-jian-main/基础题/Shape.java new file mode 100644 index 0000000..9081379 --- /dev/null +++ b/ruan-jian-main/基础题/Shape.java @@ -0,0 +1,7 @@ +/** + * 抽象形状基类,定义计算面积的契约,子类应实现 getArea() + * 使用抽象基类/接口可以确保不同形状可以互换而不破坏行为——满足里氏替换原则。 + */ +public abstract class Shape { + public abstract double getArea(); +} diff --git a/ruan-jian-main/基础题/ShapeCalculator.java b/ruan-jian-main/基础题/ShapeCalculator.java new file mode 100644 index 0000000..8e2733f --- /dev/null +++ b/ruan-jian-main/基础题/ShapeCalculator.java @@ -0,0 +1,21 @@ +import java.util.List; + +/** + * ShapeCalculator: 接受 Shape 引用并计算面积,演示多态与里氏替换原则。 + */ +public class ShapeCalculator { + + // 计算单个形状的面积 + public double calculateArea(Shape s) { + return s.getArea(); + } + + // 计算形状列表的总面积 + public double totalArea(List shapes) { + double total = 0.0; + for (Shape s : shapes) { + total += s.getArea(); + } + return total; + } +} diff --git a/ruan-jian-main/基础题/Square.java b/ruan-jian-main/基础题/Square.java new file mode 100644 index 0000000..ab4b847 --- /dev/null +++ b/ruan-jian-main/基础题/Square.java @@ -0,0 +1,19 @@ +/** + * 正方形类:实现 Shape,但不继承自 Rectangle,避免因 setter 语义不同而违反里氏替换原则。 + */ +public class Square extends Shape { + private final double side; + + public Square(double side) { + this.side = side; + } + + public double getSide() { + return side; + } + + @Override + public double getArea() { + return side * side; + } +} diff --git a/ruan-jian-main/挑战题/ListInterface.java b/ruan-jian-main/挑战题/ListInterface.java new file mode 100644 index 0000000..195c49f --- /dev/null +++ b/ruan-jian-main/挑战题/ListInterface.java @@ -0,0 +1,8 @@ +/** + * 简单的 List 接口示例,仅包含少量方法以演示设计与替换。 + */ +public interface ListInterface { + void add(E e); + E get(int index); + int size(); +} diff --git a/ruan-jian-main/挑战题/MapInterface.java b/ruan-jian-main/挑战题/MapInterface.java new file mode 100644 index 0000000..10ac721 --- /dev/null +++ b/ruan-jian-main/挑战题/MapInterface.java @@ -0,0 +1,8 @@ +/** + * 简单的 Map 接口示例 + */ +public interface MapInterface { + void put(K key, V value); + V get(K key); + int size(); +} diff --git a/ruan-jian-main/挑战题/QuickSortStrategy.java b/ruan-jian-main/挑战题/QuickSortStrategy.java new file mode 100644 index 0000000..fb38703 --- /dev/null +++ b/ruan-jian-main/挑战题/QuickSortStrategy.java @@ -0,0 +1,13 @@ +import java.util.Collections; +import java.util.List; + +/** + * 示例排序策略:内部使用 Collections.sort(TimSort),作为占位的快速替换实现。 + */ +public class QuickSortStrategy> implements SortStrategy { + @Override + public void sort(List list) { + // 为了示例简单直接使用 Collections.sort + Collections.sort(list); + } +} diff --git a/ruan-jian-main/挑战题/SetInterface.java b/ruan-jian-main/挑战题/SetInterface.java new file mode 100644 index 0000000..4a585c5 --- /dev/null +++ b/ruan-jian-main/挑战题/SetInterface.java @@ -0,0 +1,8 @@ +/** + * 简单的 Set 接口示例 + */ +public interface SetInterface { + void add(E e); + boolean contains(E e); + int size(); +} diff --git a/ruan-jian-main/挑战题/SimpleArrayList.java b/ruan-jian-main/挑战题/SimpleArrayList.java new file mode 100644 index 0000000..07a9e43 --- /dev/null +++ b/ruan-jian-main/挑战题/SimpleArrayList.java @@ -0,0 +1,24 @@ +import java.util.ArrayList; +import java.util.List; + +/** + * 基于 java.util.ArrayList 的简单实现,遵守 ListInterface 的契约,因而可以被替换。 + */ +public class SimpleArrayList implements ListInterface { + private final List delegate = new ArrayList<>(); + + @Override + public void add(E e) { + delegate.add(e); + } + + @Override + public E get(int index) { + return delegate.get(index); + } + + @Override + public int size() { + return delegate.size(); + } +} diff --git a/ruan-jian-main/挑战题/SimpleHashMap.java b/ruan-jian-main/挑战题/SimpleHashMap.java new file mode 100644 index 0000000..dce3091 --- /dev/null +++ b/ruan-jian-main/挑战题/SimpleHashMap.java @@ -0,0 +1,21 @@ +import java.util.HashMap; +import java.util.Map; + +public class SimpleHashMap implements MapInterface { + private final Map delegate = new HashMap<>(); + + @Override + public void put(K key, V value) { + delegate.put(key, value); + } + + @Override + public V get(K key) { + return delegate.get(key); + } + + @Override + public int size() { + return delegate.size(); + } +} diff --git a/ruan-jian-main/挑战题/SimpleHashSet.java b/ruan-jian-main/挑战题/SimpleHashSet.java new file mode 100644 index 0000000..714387c --- /dev/null +++ b/ruan-jian-main/挑战题/SimpleHashSet.java @@ -0,0 +1,21 @@ +import java.util.HashSet; +import java.util.Set; + +public class SimpleHashSet implements SetInterface { + private final Set delegate = new HashSet<>(); + + @Override + public void add(E e) { + delegate.add(e); + } + + @Override + public boolean contains(E e) { + return delegate.contains(e); + } + + @Override + public int size() { + return delegate.size(); + } +} diff --git a/ruan-jian-main/挑战题/SortStrategy.java b/ruan-jian-main/挑战题/SortStrategy.java new file mode 100644 index 0000000..1407f0a --- /dev/null +++ b/ruan-jian-main/挑战题/SortStrategy.java @@ -0,0 +1,8 @@ +import java.util.List; + +/** + * 排序策略接口:任何排序算法都实现该接口,从而可以互相替换 + */ +public interface SortStrategy> { + void sort(List list); +} diff --git a/ruan-jian-main/挑战题/Sorter.java b/ruan-jian-main/挑战题/Sorter.java new file mode 100644 index 0000000..18f2aab --- /dev/null +++ b/ruan-jian-main/挑战题/Sorter.java @@ -0,0 +1,20 @@ +import java.util.List; + +/** + * 排序器:接受任意 SortStrategy,使得不同策略可以互换,满足里氏替换原则。 + */ +public class Sorter> { + private SortStrategy strategy; + + public Sorter(SortStrategy strategy) { + this.strategy = strategy; + } + + public void setStrategy(SortStrategy strategy) { + this.strategy = strategy; + } + + public void sort(List list) { + strategy.sort(list); + } +} diff --git a/ruan-jian-main/进阶题/Bird.java b/ruan-jian-main/进阶题/Bird.java new file mode 100644 index 0000000..0fc0003 --- /dev/null +++ b/ruan-jian-main/进阶题/Bird.java @@ -0,0 +1,13 @@ +/** + * Bird:鸟的共同基类,只包含对所有鸟类都适用的方法(例如 layEggs、walk) + * 不把 fly() 放在 Bird 中,以免不会飞的鸟(如 Penguin)违反里氏替换原则。 + */ +public abstract class Bird { + public void layEggs() { + System.out.println("Laying eggs"); + } + + public void walk() { + System.out.println("Walking"); + } +} diff --git a/ruan-jian-main/进阶题/CreditCardPayment.java b/ruan-jian-main/进阶题/CreditCardPayment.java new file mode 100644 index 0000000..634a809 --- /dev/null +++ b/ruan-jian-main/进阶题/CreditCardPayment.java @@ -0,0 +1,11 @@ +/** + * 信用卡支付实现 + */ +public class CreditCardPayment implements PaymentProcessor { + @Override + public boolean process(double amount) { + System.out.println("Processing credit card payment: " + amount); + // 模拟处理逻辑 + return true; + } +} diff --git a/ruan-jian-main/进阶题/Flyable.java b/ruan-jian-main/进阶题/Flyable.java new file mode 100644 index 0000000..35c7870 --- /dev/null +++ b/ruan-jian-main/进阶题/Flyable.java @@ -0,0 +1,6 @@ +/** + * Flyable 接口:定义会飞行为的契约,只有能飞的鸟实现它。 + */ +public interface Flyable { + void fly(); +} diff --git a/ruan-jian-main/进阶题/FlyingBird.java b/ruan-jian-main/进阶题/FlyingBird.java new file mode 100644 index 0000000..8ebbf1d --- /dev/null +++ b/ruan-jian-main/进阶题/FlyingBird.java @@ -0,0 +1,9 @@ +/** + * 可以飞的鸟:从 Bird 继承并实现 Flyable + */ +public class FlyingBird extends Bird implements Flyable { + @Override + public void fly() { + System.out.println("Flying"); + } +} diff --git a/ruan-jian-main/进阶题/PayPalPayment.java b/ruan-jian-main/进阶题/PayPalPayment.java new file mode 100644 index 0000000..9100598 --- /dev/null +++ b/ruan-jian-main/进阶题/PayPalPayment.java @@ -0,0 +1,11 @@ +/** + * PayPal 支付实现 + */ +public class PayPalPayment implements PaymentProcessor { + @Override + public boolean process(double amount) { + System.out.println("Processing PayPal payment: " + amount); + // 模拟处理逻辑 + return true; + } +} diff --git a/ruan-jian-main/进阶题/PaymentProcessor.java b/ruan-jian-main/进阶题/PaymentProcessor.java new file mode 100644 index 0000000..d589de1 --- /dev/null +++ b/ruan-jian-main/进阶题/PaymentProcessor.java @@ -0,0 +1,7 @@ +/** + * 支付处理器接口:定义 process 方法,不同支付方式实现该接口。 + * 使用接口可以确保所有支付方式都可以互换——满足里氏替换原则。 + */ +public interface PaymentProcessor { + boolean process(double amount); +} diff --git a/ruan-jian-main/进阶题/Penguin.java b/ruan-jian-main/进阶题/Penguin.java new file mode 100644 index 0000000..01ae169 --- /dev/null +++ b/ruan-jian-main/进阶题/Penguin.java @@ -0,0 +1,13 @@ +/** + * Penguin:企鹅继承自 Bird,但不实现 Flyable,避免调用 fly() 导致的行为不一致。 + */ +public class Penguin extends Bird { + public void swim() { + System.out.println("Swimming"); + } + + @Override + public void walk() { + System.out.println("Waddling"); + } +}