This article mainly studies multiplexed classes in Java programming. So what is a multiplexed class and what is its usage? Here is a detailed introduction.
After reading the interview with Luo Luo Shengyang, I couldn't help but admire him. He was very young. I thought that I was at the same age as Luo Yonghao, and I had met one of the big guys who were not a junior and senior high school programming. After the interview, I found that Luo Luo was also a person who took one step at a time. Don’t say that it’s hard to do or you can’t do it. You didn’t try it or persist.
If you can't fly then run,if you can't run then walk, if you can't walk then crawl, but
whatever you do,you have to keep moving forward--Martin Luther King.
The title of reusing class was difficult to understand at the beginning. Later I went to read the original English version of the book. In fact, the title was reusing classes, and reusing classes meant "using ready-made things". In fact, the two methods of implementation are often heard in Java - combination and inheritance.
(1) Combination
The role of has-a.
public class TV { Show show; public String toString(){ return "showgirl"; } } class Show{ } Let's mention the toString method. When you need String and you are an object, the compiler will call the object's toString method.
There is a Show in TV. The current show is not initialized and is null. The show method cannot be called.
The combination has a powerful effect. From an object-oriented perspective, if you are creating a Car class, you can easily combine Glass, Light, Engine and other Car components using the combination.
(2) Inheritance
is-a
package com.myown.iaiti;public class Father {public int i;void get(){System.out.println("father");}}package son;import com.myown.iaiti.*;public class Son extends Father{Father f = new Father();int j = fi;Son son = new Son();son.get();}public void get(){super.get();System.out.println("son");}}There is a problem with package access permissions here. If public is not added, the default is to access the member within the package. Different package accesses, that is, the access method of Father member in Son is invisible. And public words are visible, so i access is obtained.
The private part cannot be inherited and belongs to the parent class private, while the public part will be inherited and the methods that need to be modified can be rewritten. The attributes to be added can be added separately.
Moreover, if the original parent public method is rewrite, if the original father's public method does not add public, it will not reduce the visibility of the inherited method from Father, which means that the visibility of the inherited method in the parent class cannot be reduced. super refers to the parent class, namely Father.
Another point is that in fact, all classes in Java implicitly inherit the Object class. Object is the parent class, and other classes are subclasses. Foreigners like to talk about it as the base class. Subclasses are also called exported classes or derived classes.
(3) Agent
There is a relatively difficult way to understand in the design model - the agent model. The author talks about it very interestingly. Agent is the doctrine of the mean of combination and inheritance.
package son; class Father{ public void get(){ System.out.println("father"); } } public class Son extends Father{ public static void main(String[] args) { Father f = new Father(); f.get(); } } class FatherProxy{ private Father f = new Father(); public void get(){ f.get(); } }If you directly treat Father as a member, then the father method will be exposed to this class. Then we can use a proxy class like FatherProxy. I define how to get the get method myself. I know that it is calling the father's get method, but the person who uses my proxy doesn't know. I just tell him that if you want to use it, just use the proxy's get method. Encapsulation is reflected. The above is just a simple example of random knocking.
(4) Rewrite and reload
class Father{ public void get(String s){ System.out.println("father"); } public void get(boolean b){ System.out.println("boolean"); } } public class Son extends Father{ @Override public void get(String s){ System.out.println("father"); } // @Override //There will be an error message because the parent class does not have this method, it is not a rewrite of public void get(int i ){ System.out.println("sonint"); } public static void main(String[] args) { Son s = new Son(); s.get("d"); s.get(false); s.get(1); } }Rewriting is to re-overwrite the method of the parent class. If there is no rewriting or overloading, then when the subclass calls a method that the subclass does not have, it is actually calling the parent class.
Overloading is the same method name, but the parameter name is different. To prevent you from overloading incorrectly, you can add the @Override tag, which will prompt you that you have not rewrite the method.
(5) protected
Detailed explanation of the control code for Java programming access permissions
I wrote the previous article in advance because I didn’t talk about inheritance before.
You can simply regard protected as the inheritance of the parent class to the son, and other non-inherited classes cannot be accessed.
(6) Final keyword
Adding the basic type of the final keyword means that this variable will not change after initialization. Similar to C definition, you want a variable in this program to be this value without changing. You can use final.
public class Son{ int age = 2; public static void main(String[] args) { final int i = 1; // i = 2; The value cannot be changed anymore final Son son = new Son(); // son = new Son(); // The final local variable son cannot be assigned. //It must be blank and not using a compound assignment // The local variable son modified by final cannot be assigned, it must be empty or not allocated again son.age = 4; // Although the reference is constant, the object itself can be changed. } void change(final int c){ // c= this.age; Cannot assign a new value because the value only determines the object reference when the method passes the parameter and determines the object reference is similar to this //age ++; Cannot be changed} } Static is originally static initialization, and it is used with final to occupy a piece of storage space that cannot be changed.
static final is a constant during the compilation period. The constant names are traditionally named according to the constants of c. All are capital letters and words are separated by underscores.
static final VALUE_ONE = 1;
When modifying the final method
public class Print { final void cannotprint(){ System.out.println(1); } } public class PrintSon extends Print{ //void cannotprint(){} //Cannot be rewrite because it is modified by final public static void main(String[] args) { PrintSon ps = new PrintSon(); ps.cannotprint(); } }It can be regarded as an unmodified property (ancestral inheritance) that the parent class requires that the child class must inherit. private is implicitly specified as final, because private does not give you inheritance at all. This is more private than giving you inheritance but not modifying it.
By the way, clear the permissions.
When final modifys a class, it is to prevent this class from being inherited.
(7) Inheritance and initialization
The order issue here is an interesting question. See examples.
class GrandFather{ private static int i = print(); private static int print(){ System.out.println("g"); return 1; } } class Father extends GrandFather{ private static int i = print(); private static int print(){ System.out.println("f"); return 1; } } public class Son extends Father{ private static int i = print(); private static int print(){ System.out.println("s"); return 1; } public static void main(String[] args) { System.out.println("first"); } } Is the print result first? Wrong.
Although the main method is executed, I see that son, which requires static initialization, does not have it, but the result is s, first?
There is also the problem of initialization. Son inherits father, so the compiler will load father and initialize i. If father inherits grandfather, then the compiler will load grandfather, similar to recursion.
The last one initialized is grandfather's i.
So the final result is: g, f, s, first.
Summarize
The above is all the detailed explanation of Java programming multiplexing code in this article, I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!