彦舜原创,CSDN首发:接口代理设计(重要),详解代码原义,思路清晰直观,易于学习,适合有基础的人亦适合初学者(那天,夕阳西下)

/**
 * 
 */
package cm.interfaces.factory;

/**
 * @author 彦舜
 *
 */
public class ModelFsixtyNine {

	/**
	 * 
	 */
	public ModelFsixtyNine() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Network net = null;
		net = new Proxy(new Real());
		net.browse();
	}
}

//定义network接口
interface Network{
	//定义浏览的抽象方法
	public void browse();
}

class Real implements Network{
	@Override
	public void browse() {
		System.out.println("Real.browse()");
	}
}

class Proxy implements Network{
	//Proxy this = new Proxy();    this.network = Proxy.network;
	private Network network;
	
	//Proxy this = new Proxy();    this.network = Proxy.network;
	public Proxy(Network network) {
		network = new Real();
		
		//this.network = Proxy.Network.network2
		//Network是接口类,Real是接口子类,即Network network = new Real();
		this.network = network;  
		//Type mismatch: cannot convert from Real to Proxy:同一个等级的两个类之间,无法进行类型转换,不能用一个类去实例化另一个类。故,此处,实例化的是向上或向下一级的类,父接口、父类、子类。
		this.network = new Real();  
		
		//Proxy network1 = new Real();
		/**	Type mismatch: cannot convert from Real to Proxy:
		  		很显然,此处的this.network指的是Network.network,
		  	即	Network this = new Network(); 但,接口只能实例化转型才能创建对象,故此处的this还是指Proxy的this:Proxy this = new Proxy();
		  		this.network = Network.network;		*/
		
		//底层:
		//Duplicate local variable network:对象名、对象、局部变量、变量,Network类型,不能与其他变量同名
		//日常小黄线:The value of the local variable network2 is not used
		Network network2 = new Real();  
		network2.browse();
	}
	
	@Override
	public void browse() {
		//底层程序操作this:Proxy this = new Proxy();
		this.check();
		/**	底层:Proxy.Network.network = Network.browse(); 
		  	network是接口对象,是Network接口对象名,是Real类实例化对象向上转型创建,因此this.network.browse()执行输出的是被Real覆盖过的方法体*/
		this.network.browse();
	}
	
	public void check() {
		System.out.println("Proxy.check():检查用户是否合法");
	}
	那天,夕阳西下:https://mp.weixin.qq.com/s/p0xbajLKMdXNN3j1iQ5MaA
}

彦舜原创,CSDN首发

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值