/**
*
*/
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首发