Konjac's Java entry journey-facing the object (supplement)

One, combination and news

1. Combination
Combination is simply to define references to other classes in a certain class, in this way to achieve the combination of different classes of objects. Combination can also provide method reuse between different types of objects and improve programming efficiency.

2. Combination and inheritance
Combination and inheritance can achieve code reuse by calling methods or properties of other classes in a certain class. But the difference between the two is that the combination only defines the references of other classes in one class, and cannot completely inherit all the data of other classes, but the combination can avoid messing up the program structure. When using inheritance, if it is abused, it will affect the code structure, reduce maintainability and even cause security problems. Generally, inheritance is only used when most members of other classes are needed, otherwise composition is more suitable.
For example:

package zz;
class Moven{
    
    
	Heven l;
	public Moven(Heven m) {
    
    
		
		if(l==null)
			System.out.println("this is Heven");
		else
			System.out.println("Heven has been changed");
	l=m;}
	public Moven() {
    
    
		// TODO Auto-generated constructor stub
	}
}

class Heven{
    
    
	Moven z;
	public Heven(Moven m) {
    
    
		
		if(z==null)
			System.out.println("this is Moven");
		else
			System.out.println("Moven has been changed");
	z=m;}
	public Heven() {
    
    
		// TODO Auto-generated constructor stub
	}
	
}
public class Test{
    
    
	public static void main(String[] args) {
    
    
		Moven a1=new Moven();
		Heven b1=new Heven();
		Moven a2=new Moven(b1);
		Heven b2=new Heven(a1);
	}
	
}

3. Runtime polymorphism In
java, polymorphism is divided into runtime polymorphism and compile-time polymorphism. Method overloading in Java is polymorphism at compile time, and method coverage is divided into two types. Simply put, when an object refers to an instance of this class, it is compile-time polymorphism, otherwise it is run-time polymorphism. For example, when the parent class refers to the call of the overloaded method implemented by the subclass instance, the specific method called cannot be determined at compile time, and the same is true for the interface reference implementation class.
For example:

package ht;
class AddClass1{
    
    
	public int a;
	public void prinA () {
    
    
		System.out.println("father's a="+a);
	}
	
}

public class SonAdd extends AddClass1{
    
    
	public int a=0;
	public void prinA (){
    
    
		System.out.println("son's a="+a);
	}
	
	public static void main(String[] args) {
    
    
		SonAdd p1=new SonAdd();
		AddClass1 p2=p1;
		p1.prinA();
		
	}
}

4.instanceof
instanceof is a binary operator in Java, used to test whether an object is an instance of a class. How to use: obj instanceof Class. It should be noted that when using instanceof, obj must be a reference type, not a basic type. While detecting the instance, it can also detect whether the object is an implementation class of the interface or a subclass of the parent class.

Guess you like

Origin blog.csdn.net/HiphopHarris/article/details/109276844
Recommended