1、主测试类 hello.java
package gensoku;
import java.lang.reflect.*;
import java.util.*;
public class hello
{
public static void main(String[] args)
{
try
{
System.out.println("Hello");
youkai uuz = new youkai("uuz", "dead sakuya", "All die, none can escape");
//接口及内部类、深拷贝
if(uuz instanceof Cloneable && uuz instanceof Comparable)
{
uuz.show();
youkai uuz2 = uuz.clone();
uuz2.name = "youkali's wife";
uuz2.ownMagic.magicEffect = "Only yokali can escape";
//经过深拷贝后,二者的赋值是独立的
uuz.show();
uuz2.show();
}
//实现回调函数接口、挂载
environment.start(2000, "Rumia Rush", "Darkness swallow everyone!");
environment.start(3000, "UUZ Rush", "everyone dies because of the sakuya");
Scanner sc = new Scanner(System.in);
while(true)
{
String inbuf = sc.nextLine();
if(inbuf.length() >= 0)
break;
}
sc.close();
//动态代理
miko reimu = new miko("Reimu");
Object reimuEX = ProxyFactory.getProxy(reimu);
godWill reimuGod = (godWill)reimuEX;
evilWill reimuEvil = (evilWill)reimuEX;
reimuGod.speakGodWill();
reimuEvil.speakEvilWill();
yokali yoka = new yokali("Yokali");
youkaiWill yokaEX = (youkaiWill) ProxyFactory.getProxy(yoka);
yokaEX.speakYoukaiWill();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
//工厂模式生成代理对象
class ProxyFactory
{
public static Object getProxy(Object target)
{
try
{
objProxy mid = new objProxy(target);
Class cls = target.getClass();
return Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), mid);
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}
}
2、接口实现、内部类、拷贝 youkai.java
package gensoku;
//实现接口声明,注意接口模板的使用
public class youkai implements Cloneable, Comparable<youkai>
{
public int atk;
public int HP;
public String name;
public magic ownMagic;
public youkai(String aName, String mn, String me) throws Exception
{
this.name = aName;
this.atk = 10;
this.HP = 100;
this.ownMagic = new magic(mn, me);
}
//比较接口实现函数
public int compareTo(youkai wait)
{
return this.HP - wait.HP;
}
//克隆接口实现函数
public youkai clone() throws CloneNotSupportedException
{
//调用父级默认clone进行浅拷贝
youkai ret = (youkai)super.clone();
//利用域对象的clone进行深拷贝
ret.ownMagic = this.ownMagic.clone();
return ret;
}
public void show()
{
System.out.println(name + "\tMagic-> " + ownMagic.magicName + "::" + ownMagic.magicEffect);
System.out.println("HP: " + HP + " " + "ATK: " + atk);
}
//内部类magic
public class magic implements Cloneable
{
public String magicName;
public String magicEffect;
public magic(String name, String effect)
{
this.magicName = name;
this.magicEffect = effect;
//引用外围类对象的域
atk = atk + 20;
}
public magic clone() throws CloneNotSupportedException
{
return (magic)super.clone();
}
}
}
3、目标类 miko.java
package gensoku;
//目标类接口
interface godWill
{
void speakGodWill();
}
interface evilWill
{
void speakEvilWill();
}
//目标类
public class miko implements godWill, evilWill
{
private String name;
public miko(String n)
{
name = n;
}
@Override
public void speakGodWill()
{
System.out.println(name + " : " + " god loves you");
}
@Override
public void speakEvilWill()
{
System.out.println(name + " : " + " devil hates you");
}
}
4、目标类2 youkali.java
package gensoku;
interface youkaiWill
{
void speakYoukaiWill();
}
public class yokali implements youkaiWill
{
private String name;
public yokali(String n)
{
name = n;
}
@Override
public void speakYoukaiWill()
{
System.out.println(name + " : " + " youkai scares you");
}
}
5、代理类 objProxy.java
package gensoku;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
//代理类
public class objProxy implements InvocationHandler
{
private Object target;
public objProxy(Object tar)
{
this.target = tar;
}
@Override
public Object invoke(Object proxy, Method m, Object[] args) throws Throwable
{
System.out.println("Exec ->" + m.getName());
m.invoke(target, args);
return null;
}
}
6、输出结果
Hello
uuz Magic-> dead sakuya::All die, none can escape
HP: 100 ATK: 30
uuz Magic-> dead sakuya::All die, none can escape
HP: 100 ATK: 30
youkali's wife Magic-> dead sakuya::Only yokali can escape
HP: 100 ATK: 30
Mon Jul 13 20:52:39 CST 2020# Rumia Rush : Darkness swallow everyone!
Mon Jul 13 20:52:40 CST 2020# UUZ Rush : everyone dies because of the sakuya
Mon Jul 13 20:52:41 CST 2020# Rumia Rush : Darkness swallow everyone!
Exec ->speakGodWill
Reimu : god loves you
Exec ->speakEvilWill
Reimu : devil hates you
Exec ->speakYoukaiWill
Reimu : youkai scares you