Java programming multiplexing class code detail

  • 2021-01-03 20:55:01
  • OfStack

This paper mainly studies the multiplexed classes in Java programming, so what exactly is the multiplexed class, and what usage, the following detailed introduction.

After reading the interview with Luo Shengyang, I couldn't help admiring him. He was very young. Before, I thought he was one level old as Luo Yonghao, but he was also one of the people I had seen in junior high school programming. Don't say what is difficult to do, can not do, you did not try, did not adhere to.

[

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 keep moving forward -- Martin Luther King.

]

In fact, the title is reusing classes, reuse class, in fact, reuse means "use of off-the-shelf 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{ 
} 

Mention the toString method, when you need String and you are an object, the compiler calls the object's toString method.

Show in TV, show is not initialized, is null, can not call show method.

Composition is powerful, object-oriented, and if you're building an Car class, you can easily combine Glass, Light, Engine, Car, and so on.

(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 = f.i;
	Son son = new Son();
	son.get();
}
public void get(){
	super.get();
	System.out.println("son");
}
}

There is a problem of package access, if public is not added, the default is package member access, different package access, that is, Father member access get method is not visible. While public's words are visible, so i's access is available.

The private part is not inherited and belongs to the parent class private, while the public part will inherit and the methods that need to be modified can be overridden. Attributes to add can be added separately.

And inherited methods, if the original father public method is overwritten without adding public, will have Cannot reduce the visibility of the method from Father, which cannot reduce the visibility of inherited methods in the parent class. super refers to the parent class, Father.

Another point is that virtually all the classes in java inherit from Object implicitly. Object is the parent class and the other classes are subclasses
Foreigners like to base class. Subclasses are also called derived classes or derived classes.

(3) agent

There is a more difficult design pattern - the agency model, the author said very interesting, the agency is the combination and inheritance of the golden mean.


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(); 
  } 
} 

As father as the Father as directly to the member, the method of exposure to this class, then we can use FatherProxy such proxy class, I defined the good get method is how to get, I know is to call father get method, but using this agent I didn't know, I just tell him that you want to use it with the method of get is ok. Encapsulation comes through. The above is just a simple example of random typing.

(4) Overwriting and overloading


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 overridden  
  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); 
  } 
} 

Overrides are methods that are overridden by a parent class. If there is no overwrite or overloading, a subclass that calls a method that a subclass does not have is actually calling the parent class.

Overloading is the same method name, but with a different parameter name. To prevent you from overloading incorrectly add the @Override tag, which will indicate that you did not override the method.

(5)protected

Java programming access control code details

I wrote it in advance in the first one, because I didn't talk about inheritance before.

You can simply view protected as an inheritance from a parent class to its son, and no other non-inherited classes can access it.

(6) final keyword

This, plus the basic type of the final keyword, means that this variable will not change after it is initialized. Like c, define, you want one variable in this program to be this value that doesn't have to change. You can use final.


public class Son{ 
  int age = 2; 
  public static void main(String[] args) { 
     
    final int i = 1; 
    // i = 2;  The value can't be changed  
    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 
    //final Modifies a local variable son Cannot be allocated, must be null or do not allocate again  
     
    son.age = 4; 
    // Although the reference remains constant, the object itself can change.  
  } 
   
  void change(final int c){ 
    // c= this.age;  Cannot assign a new value   Because the value is determined only by the method pass argument    Object references are similar to this  
    //age ++;     Can't change  
  } 
} 

static is originally statically initialized, and when used with final1, it occupies a block of storage space that cannot be changed.

static final is a compile-time constant. The name of the constant follows the naming tradition of c, with all uppercase letters and underscores separating the words.


static final VALUE_ONE = 1 ;  

When final modifies the method


public class Print { 
  final void cannotprint(){ 
    System.out.println(1); 
  } 
} 
 
public class PrintSon extends Print{ 
  //void cannotprint(){} 
  // Unable to rewrite   Because the final Modify the  
   
  public static void main(String[] args) { 
    PrintSon ps = new PrintSon(); 
    ps.cannotprint(); 
  } 
} 

Think of it as the unmodifiable property (ancestral) that a parent class requires a child to inherit. private is implicitly designated as final because private does not give you inheritance at all. This is more private than giving you inheritance without being able to modify it.

By the way, clear up the permissions.

public, public property, not just subclasses, but other classes as well. final, ancestral treasure, left to subclasses, but not allowed to be modified. private, parent class private property, will not be inherited by subclasses. protected, the property of the parent class reserved exclusively for subclasses, which no one else can use.

When final modifies a class, it does so that the class will not be inherited.

(7) Inheritance and initialization

The sequential problem here is an interesting one. See the 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"); 
  } 
} 

Does it print first? Wrong.

Although the main method is executed, you can see that son, which requires static initialization of i, does not have the result s, first?

There is also the issue of initialization, son inherits father, then the compiler will load father and initialize i, then father inherits grandfather, then the compiler will load grandfather, similar recursion.

So the last one that gets initialized first is grandfather's i.

So the final result is: g f, s, first.

conclusion

That is the end of this article on Java programming reuse class code detail, I hope to help you. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: