2022.01.10接口排错题、代理设计模式和工厂方法初识
每日总结2022.01.10
1.排错题1
//修改前 public interface A { int x = 0; } class B{ int x = 1; } class C extends B implements A{ public void pX(){ System.out.println(super.x); System.out.println(A.x); } public static void main(String[] args) { new C().pX(); } } //修改后 public interface A { int x = 0; } class B{ int x = 1; } class C extends B implements A{ public void pX(){ System.out.println(super.x); System.out.println(A.x); } public static void main(String[] args) { new C().pX(); } }
2.排错题2
//修改前 interface Playable { void play(); } interface Bounceable { void play(); } interface Rollable extends Playable, Bounceable { Ball ball = new Ball("PingPang"); } class Ball implements Rollable { private String name; public String getName() { return name; } public Ball(String name) { this.name = name; } public void play() { ball = new Ball("Football"); System.out.println(ball.getName()); } } //修改后 interface Playable { void play(); } interface Bounceable { void play(); } interface Rollable extends Playable, Bounceable { Ball ball = new Ball("PingPang"); } class Ball implements Rollable { private String name; public String getName() { return name; } public Ball(){ } public Ball(String name) { this.name = name; } public void play() { System.out.println(new Ball("Football").getName()); } } class ballTest { public static void main(String[] args) { new Ball().play(); } }
3.代理设计模式
概述: 代理模式是Java开发中使用较多的一种设计模式。代理设计就是为其 他对象提供一种代理以控制对这个对象的访问。
public class NetWorkTest { public static void main(String[] args) { Server server = new Server(); ProxyServer proxyServer = new ProxyServer(server); proxyServer.browse(); } } interface Network { public void browse(); } class Server implements Network { @Override public void browse() { System.out.println("link net"); } } class ProxyServer implements Network { private Network work; public ProxyServer(Network work) { this.work = work; } public void check() { System.out.println("check"); } @Override public void browse() { check(); work.browse(); } }
4.工厂设计模式初识
接口的应用:工厂模式
工厂模式:实现了创建者与调用者的分离,即将创建对象的具体过程屏蔽隔离 起来,达到提高灵活性的目的。 其实设计模式和面向对象设计原则都是为了使得开发项目更加容易扩展和维 护,解决方式就是一个“分工”。
核心本质: 实例化对象,用工厂方法代替 new 操作。 将选择实现类、创建对象统一管理和控制。从而将调用者跟我们的实现类解耦。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南