diff --git a/.gitignore b/.gitignore index 8c9e901..775deb8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,95 @@ +### Compiled class files +*.class + +### Log files +*.log + +### BlueJ files +*.ctxt + +### Mobile Tools for Java (J2ME) +.mtj.tmp/ + +### Package Files +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +### VM crash logs +hs_err_pid* +replay_pid* + +# ======================== +# Maven +# ======================== +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +.mvn/wrapper/maven-wrapper.jar + +# ======================== +# Gradle +# ======================== +.gradle/ +build/ +!gradle-wrapper.jar +!*/src/main/**/build/ +!*/src/test/**/build/ + +### Avoid ignoring Gradle wrapper +gradle-app.setting + +# ======================== +# IntelliJ IDEA +# ======================== +.idea/ +*.iml +*.ipr +*.iws +out/ + +# ======================== +# Eclipse +# ======================== +.project +.classpath +.cproject +.settings/ +bin/ +tmp/ + +# ======================== +# NetBeans +# ======================== +nbproject/private/ +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ + +# ======================== +# VS Code +# ======================== +.vscode/ + +# ======================== +# OS files +# ======================== +# macOS +.DS_Store + +# Windows +Thumbs.db + +#local com/study/test/ diff --git a/com/study/demo/test3_1.class b/com/study/demo/test3_1.class deleted file mode 100644 index 8a09b03..0000000 Binary files a/com/study/demo/test3_1.class and /dev/null differ diff --git a/com/study/demo/test3_2.class b/com/study/demo/test3_2.class deleted file mode 100644 index 5ba259a..0000000 Binary files a/com/study/demo/test3_2.class and /dev/null differ diff --git a/com/study/demo/test3_3.class b/com/study/demo/test3_3.class deleted file mode 100644 index 0f74550..0000000 Binary files a/com/study/demo/test3_3.class and /dev/null differ diff --git a/com/study/demo/test3_4.class b/com/study/demo/test3_4.class deleted file mode 100644 index 04b1e71..0000000 Binary files a/com/study/demo/test3_4.class and /dev/null differ diff --git a/com/study/demo/test3_5.class b/com/study/demo/test3_5.class deleted file mode 100644 index 596c796..0000000 Binary files a/com/study/demo/test3_5.class and /dev/null differ diff --git a/com/study/demo3/Bicycle.class b/com/study/demo3/Bicycle.class deleted file mode 100644 index 8cb0107..0000000 Binary files a/com/study/demo3/Bicycle.class and /dev/null differ diff --git a/com/study/demo3/Book.class b/com/study/demo3/Book.class deleted file mode 100644 index 3c8d5f2..0000000 Binary files a/com/study/demo3/Book.class and /dev/null differ diff --git a/com/study/demo3/Car.class b/com/study/demo3/Car.class deleted file mode 100644 index 7a34b27..0000000 Binary files a/com/study/demo3/Car.class and /dev/null differ diff --git a/com/study/demo3/EBook.class b/com/study/demo3/EBook.class deleted file mode 100644 index 4800637..0000000 Binary files a/com/study/demo3/EBook.class and /dev/null differ diff --git a/com/study/demo3/Library.class b/com/study/demo3/Library.class deleted file mode 100644 index ce16e21..0000000 Binary files a/com/study/demo3/Library.class and /dev/null differ diff --git a/com/study/demo3/LibraryTest.class b/com/study/demo3/LibraryTest.class deleted file mode 100644 index 25a3772..0000000 Binary files a/com/study/demo3/LibraryTest.class and /dev/null differ diff --git a/com/study/demo3/Main2.class b/com/study/demo3/Main2.class deleted file mode 100644 index fadc973..0000000 Binary files a/com/study/demo3/Main2.class and /dev/null differ diff --git a/com/study/demo3/PrintedBook.class b/com/study/demo3/PrintedBook.class deleted file mode 100644 index 2583d90..0000000 Binary files a/com/study/demo3/PrintedBook.class and /dev/null differ diff --git a/com/study/demo3/StudentManager.class b/com/study/demo3/StudentManager.class deleted file mode 100644 index 4e06966..0000000 Binary files a/com/study/demo3/StudentManager.class and /dev/null differ diff --git a/com/study/demo3/Train.class b/com/study/demo3/Train.class deleted file mode 100644 index 2405a80..0000000 Binary files a/com/study/demo3/Train.class and /dev/null differ diff --git a/com/study/demo3/Vehicle.class b/com/study/demo3/Vehicle.class deleted file mode 100644 index fba2811..0000000 Binary files a/com/study/demo3/Vehicle.class and /dev/null differ diff --git a/com/study/demo4/AlipayPayment.java b/com/study/demo4/AlipayPayment.java new file mode 100644 index 0000000..b5193fd --- /dev/null +++ b/com/study/demo4/AlipayPayment.java @@ -0,0 +1,9 @@ +package com.study.demo4; + +class AlipayPayment implements PaymentProcessor { + @Override + public boolean pay(double amount) { + System.out.println("支付宝支付: " + amount); + return true; + } +} diff --git a/com/study/demo4/BikeDeliveryCalculator.java b/com/study/demo4/BikeDeliveryCalculator.java new file mode 100644 index 0000000..a77218a --- /dev/null +++ b/com/study/demo4/BikeDeliveryCalculator.java @@ -0,0 +1,11 @@ +package com.study.demo4; + +class BikeDeliveryCalculator implements DeliveryFeeCalculator { + @Override + public double calculateFee(double distance) { + if (distance <= 5) { + return 5.0; // 起步价 + } + return 5.0 + (distance - 5) * 1.5; + } +} diff --git a/com/study/demo4/Cardeliverycalculator.java b/com/study/demo4/Cardeliverycalculator.java new file mode 100644 index 0000000..c677781 --- /dev/null +++ b/com/study/demo4/Cardeliverycalculator.java @@ -0,0 +1,12 @@ +package com.study.demo4; + +class CarDeliveryCalculator implements DeliveryFeeCalculator { + @Override + public double calculateFee(double distance) { + if (distance <= 5) { + return 10.0; // 起步价 + } + return 10.0 + (distance - 5) * 2.0; + } +} + diff --git a/com/study/demo4/CreditCardPayment.java b/com/study/demo4/CreditCardPayment.java new file mode 100644 index 0000000..e0727d0 --- /dev/null +++ b/com/study/demo4/CreditCardPayment.java @@ -0,0 +1,9 @@ +package com.study.demo4; + +class CreditCardPayment implements PaymentProcessor { + @Override + public boolean pay(double amount) { + System.out.println("银行卡支付: " + amount); + return true; + } +} diff --git a/com/study/demo4/DeliveryFeeCalculator.java b/com/study/demo4/DeliveryFeeCalculator.java new file mode 100644 index 0000000..3718cf4 --- /dev/null +++ b/com/study/demo4/DeliveryFeeCalculator.java @@ -0,0 +1,5 @@ +package com.study.demo4; + +interface DeliveryFeeCalculator { + double calculateFee(double distance); +} diff --git a/com/study/demo4/HomeAppliance.java b/com/study/demo4/HomeAppliance.java new file mode 100644 index 0000000..b5ff203 --- /dev/null +++ b/com/study/demo4/HomeAppliance.java @@ -0,0 +1,26 @@ +package com.study.demo4; + +abstract class HomeAppliance implements SmartDevice { + protected String name; + protected boolean isOn; + + public HomeAppliance(String name) { + this.name = name; + this.isOn = false; + } + + @Override + public void turnOn() { + isOn = true; + } + + @Override + public void turnOff() { + isOn = false; + } + + @Override + public String getStatus() { + return name + " is " + (isOn ? "ON" : "OFF"); + } +} diff --git a/com/study/demo4/HomeController.java b/com/study/demo4/HomeController.java new file mode 100644 index 0000000..f6c89f7 --- /dev/null +++ b/com/study/demo4/HomeController.java @@ -0,0 +1,21 @@ +package com.study.demo4; + +import java.util.ArrayList; +import java.util.List; + +class HomeController { + private List devices = new ArrayList<>(); + + public void addDevice(SmartDevice device) { + devices.add(device); + } + + public String getDeviceStatus(String name) { + for (SmartDevice device : devices) { + if (device.getStatus().contains(name)) { + return device.getStatus(); + } + } + return "Device not found"; + } +} diff --git a/com/study/demo4/PaymentFactory.java b/com/study/demo4/PaymentFactory.java new file mode 100644 index 0000000..be757a8 --- /dev/null +++ b/com/study/demo4/PaymentFactory.java @@ -0,0 +1,16 @@ +package com.study.demo4; + +class PaymentFactory { + public static PaymentProcessor getProcessor(String type) { + switch (type.toLowerCase()) { + case "wechat": + return new WechatPayment(); + case "alipay": + return new AlipayPayment(); + case "card": + return new CreditCardPayment(); + default: + throw new IllegalArgumentException("不支持的支付方式"); + } + } +} diff --git a/com/study/demo4/PaymentProcessor.java b/com/study/demo4/PaymentProcessor.java new file mode 100644 index 0000000..67bb08f --- /dev/null +++ b/com/study/demo4/PaymentProcessor.java @@ -0,0 +1,6 @@ +package com.study.demo4; + +interface PaymentProcessor { + boolean pay(double amount); +} + diff --git a/com/study/demo4/SmartAirConditioner.java b/com/study/demo4/SmartAirConditioner.java new file mode 100644 index 0000000..450b968 --- /dev/null +++ b/com/study/demo4/SmartAirConditioner.java @@ -0,0 +1,7 @@ +package com.study.demo4; + +class SmartAirConditioner extends HomeAppliance { + public SmartAirConditioner(String name) { + super(name); + } +} diff --git a/com/study/demo4/SmartDevice.java b/com/study/demo4/SmartDevice.java new file mode 100644 index 0000000..d13ce91 --- /dev/null +++ b/com/study/demo4/SmartDevice.java @@ -0,0 +1,7 @@ +package com.study.demo4; + +interface SmartDevice { + void turnOn(); + void turnOff(); + String getStatus(); +} diff --git a/com/study/demo4/SmartLight.java b/com/study/demo4/SmartLight.java new file mode 100644 index 0000000..05ed4c9 --- /dev/null +++ b/com/study/demo4/SmartLight.java @@ -0,0 +1,7 @@ +package com.study.demo4; + +class SmartLight extends HomeAppliance { + public SmartLight(String name) { + super(name); + } +} diff --git a/com/study/demo4/SmartMasher.java b/com/study/demo4/SmartMasher.java new file mode 100644 index 0000000..313b8c1 --- /dev/null +++ b/com/study/demo4/SmartMasher.java @@ -0,0 +1,7 @@ +package com.study.demo4; + +class SmartMasher extends HomeAppliance { + public SmartMasher(String name) { + super(name); + } +} diff --git a/com/study/demo4/TestControl.java b/com/study/demo4/TestControl.java new file mode 100644 index 0000000..032bc29 --- /dev/null +++ b/com/study/demo4/TestControl.java @@ -0,0 +1,31 @@ +package com.study.demo4; + +import java.util.ArrayList; +import java.util.List; + +public class TestControl { + public static void main(String[] args) { + HomeController controller = new HomeController(); + + // 创建设备 + SmartLight light = new SmartLight("Living Room Light"); + SmartAirConditioner ac = new SmartAirConditioner("Bedroom AC"); + SmartMasher masher = new SmartMasher("Kitchen Masher"); + + // 添加到控制器 + controller.addDevice(light); + controller.addDevice(ac); + controller.addDevice(masher); + + // 测试设备状态 + light.turnOff(); + ac.turnOff(); + masher.turnOn(); + + // 查询状态 + System.out.println(controller.getDeviceStatus("Living Room Light")); + System.out.println(controller.getDeviceStatus("Bedroom AC")); + System.out.println(controller.getDeviceStatus("Kitchen Masher")); + System.out.println(controller.getDeviceStatus("Non-existent Device")); + } +} diff --git a/com/study/demo4/TestPay.java b/com/study/demo4/TestPay.java new file mode 100644 index 0000000..b93a43d --- /dev/null +++ b/com/study/demo4/TestPay.java @@ -0,0 +1,8 @@ +package com.study.demo4; + +public class TestPay { + public static void main(String[] args) { + PaymentProcessor processor = PaymentFactory.getProcessor("alipay"); + processor.pay(666.0); + } +} diff --git a/com/study/demo4/TestTransport.java b/com/study/demo4/TestTransport.java new file mode 100644 index 0000000..f57bd13 --- /dev/null +++ b/com/study/demo4/TestTransport.java @@ -0,0 +1,22 @@ +package com.study.demo4; + +public class TestTransport { + public static void main(String[] args) { + // 测试自行车配送 + DeliveryFeeCalculator bike = new BikeDeliveryCalculator(); + System.out.println("自行车配送 3km: ¥" + bike.calculateFee(3)); + System.out.println("自行车配送 8km: ¥" + bike.calculateFee(8)); + + // 测试汽车配送 + DeliveryFeeCalculator car = new CarDeliveryCalculator(); + System.out.println("汽车配送 3km: ¥" + car.calculateFee(3)); + System.out.println("汽车配送 8km: ¥" + car.calculateFee(8)); + + // 对比不同距离的费用 + System.out.println("\n费用对比:"); + for (int km = 1; km <= 10; km++) { + System.out.printf("%dkm: 自行车¥%.1f, 汽车¥%.1f%n", + km, bike.calculateFee(km), car.calculateFee(km)); + } + } +} diff --git a/com/study/demo4/WechatPayment.java b/com/study/demo4/WechatPayment.java new file mode 100644 index 0000000..cb63334 --- /dev/null +++ b/com/study/demo4/WechatPayment.java @@ -0,0 +1,9 @@ +package com.study.demo4; + +class WechatPayment implements PaymentProcessor { + @Override + public boolean pay(double amount) { + System.out.println("微信支付: " + amount); + return true; + } +}