A method for enhancing the functionality of Java methods
1. By way of inheritance
The methods in which classes are enhanced can take the form of inheriting that class. By inheriting the class, you can override the method if you also need some functionality of the old method, using super call.
1 // study methods that require enhanced classes 2 Public class Person {3 Public void Study () {4 System.out.println ("Method required for expansion"); 5 }6 }
1 //inheriting classes that require methods to be enhanced, overriding methods2 Public classStudentextendsperson{3 @Override4 Public voidStudy () {5 //method can be enhanced before and after6 //useful to the original method super Call parent class7System.out.println ("overridden Method");8 }9}
2. Through decorator mode
Conditions of use: The decorator pattern is used to enhance the functionality of an interface or an overridden method in an implementation class of an abstract class.
The use of the explanation:
The first step: Find an interface for the implementation class that needs to be enhanced, or an abstract class, and then create a subclass to implement or inherit;
Step Two: Add the private attribute to the created class, which is the implementation class for the interface.
Step three: Write an implementation class with a parameter of the interface and assign the implementation class to the private property
Fourth step: Rewrite the methods that need to be enhanced, where you can enhance the various
Fifth step: Rewrite other abstract methods, directly call the method that need to enhance the class,
A simple case:
There is a cat class to achieve the Cat branch interface, it has a way to eat, now there is a need for its eating methods have been unable to meet, enhance its eating methods
1 // Interface 2 Public Interface Cat {3 // How to eat 4 void eat (); 5 // How to play 6 void play (); 7 }
1 //classes that need to be enhanced2 Public classHomecatImplementscat{3 4 @Override5 Public voideat () {6 //TODO auto-generated Method Stub7System.out.println ("The way to eat fish");8 }9 Ten @Override One Public voidPlay () { A //TODO auto-generated Method Stub - - } the -}
1 //classes that are enhanced2 Public classNewcatImplementscat{3 PrivateCat C;4 PublicNewcat (Cat c) {5 This. c=C;6 }7 //for enhanced8 @Override9 Public voideat () {Ten //TODO auto-generated Method Stub OneSYSTEM.OUT.PRINTLN ("Enhanced front"); A c.eat (); -SYSTEM.OUT.PRINTLN ("Enhanced"); - } the - @Override - Public voidPlay () { - //TODO auto-generated Method Stub + C.play (); - } + A}
1 // Test Class 2 Public class Demo1 {3 Public Static void Main (string[] args) {4 Newcat newcat=New newcat (new homecat ()); 5 newcat.eat (); 6 }7 }
3. Dynamic Agent
1 Public Interface targetinterface {23 Public void method1 (); 4 Public String method2 (); 5 Public int method3 (int x); 6 }
1 Public classProxyTest2 {2 3 Public Static voidMain (string[] args) {4 5 FinalTarget target =NewTarget ();6 7 //dynamically creating proxy objects8 9Targetinterface proxy =(Targetinterface) proxy.newproxyinstance (Ten Target.getclass (). getClassLoader (), One Target.getclass (). Getinterfaces (), A NewInvocationhandler () { - @Override - //How many times have they been executed? -------See how many proxy objects are called the //proxy Object invocation interface The appropriate method is to invoke the Invoke - /* - * Proxy: Is the agent object - * Method: The byte-code object representing the target method + * args: Represents the parameter when calling the target method - */ + PublicObject Invoke (Object proxy, Method method, object[] args)throwsThrowable { A //Reflective Knowledge points atObject invoke = Method.invoke (target, args);//the corresponding method of the target object - //Retrun Returns the value to the proxy object - returninvoke; - } - } - ); in -Proxy.method1 ();//Call Invoke---Method: args:null return value for target object method1 null toString method2 = Proxy.method2 ();//Call the Invoke---method: args:null return value for the target object method2 method2 + intmethod3 = proxy.method3 (100);////Call Invoke-----method: args:object[]{100} return value of target object method3 - the System.out.println (METHOD2); * System.out.println (method3); $ Panax Notoginseng } - the}
1 Public classTargetImplementstargetinterface{2 3 @Override4 Public voidmethod1 () {5System.out.println ("Method1 running ...");6 }7 8 @Override9 PublicString method2 () {TenSystem.out.println ("Method2 running ..."); One return"Method2"; A } - - @Override the Public intMETHOD3 (intx) { - returnx; - } - + - +}
Three ways to enhance the functionality of Java methods