??xml version="1.0" encoding="utf-8" standalone="yes"?>
问题Q如何知道一个方法的q行旉Q?br />引出代理----------------------
1.直接在原来类上修?br />利用System.currentTimeMillis()
•public void Move() {
long start=System.currentTimeMillis();
System.out.println("Tank Moving");
try {
Thread.sleep(new Random().nextInt(10000));
} catch (InterruptedException e) {
e.printStackTrace();
}
long end=System.currentTimeMillis();
System.out.println("time:"+(end-start));
}
2.利用l承
如果不能在原来类上添加函数。则可以利用新徏cȝ承extends原来c,重写ҎQ在super.Ҏ前后加入此函数System.currentTimeMillis()
public class Tank2 extends Tank{
@Override
public void Move() {
long start=System.currentTimeMillis();
super.Move();
long end=System.currentTimeMillis();
System.out.println("time2:"+(end-start));
}
}
3.利用聚合。新Z个类B实现接口函数QBc里面有需要测试的cA的对象。相当于B是A的一个代理。实际上Q第二种Ҏ也算是一个代?br />public class Tank3 implements Moveable{
public Tank3(Tank myt) {
super();
this.myt=myt;
}
Tank myt;
@Override
public void Move() {
long start=System.currentTimeMillis();
myt.Move();
long end=System.currentTimeMillis();
System.out.println("time3:"+(end-start));
}
}
4、利用聚合实现多个代理。下面写旉代理Q运行的旉Q和日志代理Q打华ͼ,可以通过改变clientcM代理调用序来改变出现的序
------------- Moveable .java-------------
package mypro.cn;
public interface Moveable {
public void Move();
}
---------------tank.java-----------
package mypro.cn;
import java.util.Random;
public class Tank implements Moveable{
public void Move() {
System.out.println("Tank Moving");
try {
Thread.sleep(new Random().nextInt(10000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
----------- TankTimeProxy.java-------------------------
package mypro.cn;
import java.text.DateFormat;
public class TankTimeProxy implements Moveable{
public TankTimeProxy(Moveable myt) {
super();
this.myt=myt;
}
Moveable myt;
@Override
public void Move() {
long start=System.currentTimeMillis();
java.util.Date date = new java.util.Date();
String currTime = DateFormat.getDateTimeInstance().format(date);
System.out.println("starttime:"+currTime);
myt.Move();
long end=System.currentTimeMillis();
System.out.println("time:"+(end-start));
}
}
------------------------- TankLogProxy .java-------------------------------
package mypro.cn;
public class TankLogProxy implements Moveable{
public TankLogProxy(Moveable myt) {
super();
this.myt=myt;
}
Moveable myt;
@Override
public void Move() {//日志代理
System.out.println("tank start");
myt.Move();
System.out.println("tank stop"); }
}
--------------------- Client .java----------------------------
package mypro.cn;
public class Client {
public static void main(String[] args) {
Tank t=new Tank();
TankTimeProxy ttp=new TankTimeProxy(t);//先时间代理,卛_包装一层时?/span>
TankLogProxy tlp=new TankLogProxy(ttp);//再日志代理,最外层包装日志
Moveable m=tlp;
m.Move();
}
}
改变包装序Q先包装日志Q再包装旉Q?br />
]]>
<U> Class<? extends
U>
asSubclass(Class<U> clazz)
T
cast(Object obj)
boolean
desiredAssertionStatus()
static Class<?>
forName(String className)
Class
对象?/td>static Class<?>
forName(String name,
boolean initialize, ClassLoader loader)
Class
<A extends Annotation>
A
getAnnotation(Class<A> annotationClass)
Annotation[]
getAnnotations()
String
getCanonicalName()
Class<?>[]
getClasses()
Class
对象的数l,q些对象表示属于?Class
ClassLoader
getClassLoader()
Class<?>
getComponentType()
Class
?/td> Constructor<T>
getConstructor(Class<?>... parameterTypes)
Constructor
对象Q它反映?Class
Constructor<?>[]
getConstructors()
Constructor
对象的数l,q些对象反映?Class
Annotation[]
getDeclaredAnnotations()
Class<?>[]
getDeclaredClasses()
Class
对象的一个数l,q些对象反映声明为此 Class
Constructor<T>
getDeclaredConstructor(Class<?>... parameterTypes)
Constructor
对象Q该对象反映?Class
Constructor<?>[]
getDeclaredConstructors()
Constructor
对象的一个数l,q些对象反映?Class
Field
getDeclaredField(String name)
Field
对象Q该对象反映?Class
Field[]
getDeclaredFields()
Field
对象的一个数l,q些对象反映?Class
Method
getDeclaredMethod(String name, Class<?>... parameterTypes)
Method
对象Q该对象反映?Class
Method[]
getDeclaredMethods()
Method
对象的一个数l,q些对象反映?Class
Class<?>
getDeclaringClass()
Class
对象所表示的类或接口是另一个类的成员,则返回的 Class
Class<?>
getEnclosingClass()
Constructor<?>
getEnclosingConstructor()
Constructor
Method
getEnclosingMethod()
Method
T[]
getEnumConstants()
Field
getField(String name)
Field
对象Q它反映?Class
Field[]
getFields()
Field
对象的数l,q些对象反映?Class
Type[]
getGenericInterfaces()
Type
getGenericSuperclass()
Class<?>[]
getInterfaces()
Method
getMethod(String name, Class<?>... parameterTypes)
Method
对象Q它反映?Class
Method[]
getMethods()
Method
对象的数l,q些对象反映?Class
int
getModifiers()
String
getName()
Package
getPackage()
ProtectionDomain
getProtectionDomain()
ProtectionDomain
?/td> URL
getResource(String name)
InputStream
getResourceAsStream(String name)
Object[]
getSigners()
String
getSimpleName()
Class<? super T>
getSuperclass()
Class
所表示的实体(cR接口、基本类型或 voidQ的类?Class
?/td> TypeVariable<Class<T>>[]
getTypeParameters()
boolean
isAnnotation()
boolean
isAnnotationPresent(Class<? extends Annotation> annotationClass)
boolean
isAnonymousClass()
boolean
isArray()
Class
对象是否表示一个数l类?/td> boolean
isAssignableFrom(Class<?> cls)
Class
Class
参数所表示的类或接口是否相同,或是否是其超cL接口?/td> boolean
isEnum()
boolean
isInstance(Object obj)
Object
是否与此 Class
boolean
isInterface()
Class
对象是否表示一个接口类型?/td> boolean
isLocalClass()
boolean
isMemberClass()
boolean
isPrimitive()
Class
对象是否表示一个基本类型?/td> boolean
isSynthetic()
T
newInstance()
String
toString()