Java 多态 polymorphic 枚举 练习
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package ersatz; public class Car { private double temperature; public Car( double temperature) { this .temperature = temperature; } private class Air { public void tune() { if (temperature >= 40) { System. out .println( "cold air" ); } else if (temperature < 0) { System. out .println( "warm air" ); } else { System. out .println( "nothing" ); } } } public Air getAir() { return new Air(); } public static void main(String[] args) { final Car car = new Car(50); car.getAir().tune(); final Car car1 = new Car(-5); car1.getAir().tune(); final Car car2 = new Car(33); car2.getAir().tune(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | package ersatz; public class Car { private double temperature; public Car( double temperature) { this .temperature = temperature; } private class Air { public void tune() { if (temperature >= 40) { System. out .println( "cold air" ); } else if (temperature < 0) { System. out .println( "warm air" ); } else { System. out .println( "nothing" ); } } } public void regulate() { new Air().tune(); } public static void main(String[] args) { final Car car = new Car(50); car.regulate(); final Car car1 = new Car(-5); car1.regulate(); final Car car2 = new Car(33); car2.regulate(); } } |
Vehicle.java
1 2 3 4 5 | package com.gibe; public interface Vehicle { public abstract void work(); } |
Horse.java
1 2 3 4 5 6 7 8 | package com.gibe; public class Horse implements Vehicle{ @Override public void work(){ System. out .println( "use horse" ); } } |
Boat.java
1 2 3 4 5 6 7 8 | package com.gibe; public class Boat implements Vehicle{ @Override public void work(){ System. out .println( "use boat" ); } } |
Plane.java
1 2 3 4 5 6 7 8 | package com.gibe; public class Plane implements Vehicle{ @Override public void work() { System. out .println( "use plane" ); } } |
VehicleFactory.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | package com.gibe; public class VehicleFactory { private static Horse horse = null ; private static final Boat boat = new Boat(); private VehicleFactory() { } public static Horse getHorse() { if (horse == null ) { horse = new Horse(); } return horse; } public static Boat getBoat() { return boat; } public static Plane getPlane() { return new Plane(); } } |
Person.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | package com.gibe; public class Person { private String name; private Vehicle vehicle; public Person(String name, Vehicle vehicle) { this .name = name; this .vehicle = vehicle; } public void river() { if (!( this .vehicle instanceof Boat)) { vehicle = VehicleFactory.getBoat(); } vehicle.work(); } public void land() { if (!(vehicle instanceof Horse)) { vehicle = VehicleFactory.getHorse(); } vehicle.work(); } public void fire() { if (!(vehicle instanceof Plane)) { vehicle = VehicleFactory.getPlane(); } vehicle.work(); } } |
Test
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package ersatz; import com.gibe.Person; import com.gibe.VehicleFactory; public class Ersatz { @SuppressWarnings({ "SpellCheckingInspection" }) public static void main(String[] args) { final Person bb = new Person( "bb" , VehicleFactory.getHorse()); bb.land(); bb.river(); bb.fire(); bb.river(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | package com.gibe; public class Ersatz { public static void main(String[] args) { Color green = Color.GREEN; green.show(); switch (green) { case YELLOW: System. out .println( "yellow" ); break ; case BLACK: System. out .println( "black" ); break ; default : System. out .println( "none" ); break ; } } } enum Color implements Interface { RED(255, 0, 0), BLUE(0, 0, 255), BLACK(0, 0, 0), YELLOW(255, 255, 0), GREEN(0, 255, 0); private final int red; private final int green; private final int blue; private Color( int red, int green, int blue) { this .red = red; this .green = green; this .blue = blue; } @Override public void show() { System. out .println( "red=" + red + ", green=" + green + ", blue=" + blue); } } interface Interface { public abstract void show(); } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律