Three common ways of Java code reuse: inheritance, composition and proxy

Reusing code is one of the most striking features of Java. This sentence is very smooth, no problem, but the problem is that many people don't know what "reuse" is. It's like I said, "silent King II is a programmer who can not only write code". Alas, who is silent King II?

We need to define "reuse". Reuse, to put it bluntly, is reuse.

For example, many famous people say many famous words. When we speak and write, we often repeat these famous words intentionally or unintentionally. For example, I especially like to repeat Wang Xiaobo's famous saying: "from words, you rarely learn human nature, but from silence. If you want to learn more, you should continue to keep silent."

The above example can only be said to be a low-level application of "reuse", which is actually copy and paste. Are there any advanced reuse methods?

Yes, of course. As an excellent object-oriented design language, Java is much more advanced in reusable applications.

The most common reuse method is inheritance - use the extends keyword to create a new class based on the base class. The new class can directly reuse the non private attributes and methods of the base class; Just like listing 1-1. Program listing 1-1: from the program listing 1-1, we can see that although the getname () and setname () methods are created in the base class Wangsan, they can be used in the new class wangxiaosan. The reuse of code is thus easily completed.

Another common reuse method is composition - create an object of an existing class in a new class and call the non private properties and methods of the existing class through the object; Just like listing 2-1. Procedure list 2-1: from procedure list 2-1, we can see that although sunflower acupoint pointing hand is a unique skill of Bai zhantang, as shopkeeper Tong's boyfriend, shopkeeper Tong wants zhantang to point a acupoint, and zhantang dare not refuse. You see, although shopkeeper Tong is a weak woman, no one dares not to be obedient - a powerful combination since he has zhantang, the best martial arts boyfriend. It should be noted that how to choose between inheritance and composition? If the new class and the existing class need to have some similar methods and properties, the form of inheritance is adopted; If the new class is just to borrow some methods and properties of the existing class, and the two do not have many similarities, it needs to adopt the form of combination.

Another reuse method is proxy - create a proxy in a new class to operate the non private properties and methods of an existing class through the proxy; Just like listing 3-1. Program listing 3-1: from program listing 3-1, we can see that the mode and combination of agents are somewhat similar, But there is another difference - the agent successfully separates the direct relationship between the new class (member) and the existing class (store), so that the methods of the existing class are not directly exposed to the new class (the combination method will directly expose the non private methods and attributes of the existing class to the new class); at the same time, the agent gets enough benefits.

As code producers, we sometimes want the code to be reused, and sometimes we want the code not to be reused. The final keyword comes in handy when we don't want the code to be reused. The keyword final is very vivid. It itself explains everything - the last and final; Decisive; Cannot be changed. There are three scenarios for using final: data, method and class. Let's explain a little. 1) Final data the most common final data is constants. For example, for constants, they are visible to all classes in the entire application, so they are public; It can be directly through the class name Constant name access, so it is static; It is not modifiable, so it is final. Another common final data is parameters. Refer to program listing 4-1. Program listing 4-1: system. Method that is modified again inside the method out. println(content); } 2) the final method is in the Java class, All private methods are implicitly specified as final (in other words, if you add the final modifier to the private method, it is meaningless). When introducing inheritance, you should note that I emphasize that the new class can directly reuse the non private properties and methods of the base class, that is, the private method cannot be modified by the inheritor, because the private method is final. See Listing 4 -2. You will find that the San reference of Wangsan type cannot call the say (string words) method, because the private method cannot be modified by the inheritor, although the say (string words) method is redefined in wangxiaosan. Listing 4-2:3) final class when we think that a class is the final form, it is perfect and should not be inherited, we can use the final keyword to modify it; Refer to procedure list 4-3. Procedure list 4-3:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>