Java basics (5)

Abstract class
. Ordinary class is a complete functional class that can produce instantiated objects, and the ordinary class can contain content such as construction methods, ordinary methods, static methods, constants, and variables.
·Abstract class refers to the component that adds abstract method to the structure of ordinary class.

Abstract method
·Abstract method refers to the method without method body ({}), and the abstract method must also be modified with the keyword abstract.
The class with abstract method is the abstract class, and the abstract class must use the abstract keyword to declare
ex

abstract class A{
    
    
	public void fun(){
    
    //普通方法有方法体
		System.out.println("存在方法体的普通方法")
	}
	public abstract void print();//抽象方法
}

Note on abstract classes
· Instantiation cannot be performed directly (when a class is instantiated, this class can call the properties or methods in the class, but the abstract class has abstract methods, but there is no method body, and the call cannot be made without the method body , The object cannot be instantiated if it cannot be called)

Principles for the use of abstract classes
· Abstract methods must be public or protected (no private, otherwise they cannot be inherited by subclasses, and subclasses cannot implement this method), default public
· Abstract classes cannot be instantiated directly, and you need to rely on subclasses to adopt upward transformation Ways to deal with
· Abstract classes must have subclasses, use extends inheritance, a subclass can only inherit one abstract class
· Subclass (if not an abstract class) must override all abstract methods in the abstract class, if the subclass does not implement the parent For the abstract method of the class, the subclass must also be defined as an abstract class (the parameters of the abstract method in the Java abstract class correspond to the parameters of the subclass method must be the same, if inconsistent, it means that the subclass has overloaded the abstract method)

ex:

abstract class A{
    
    
	public void fun(){
    
    
		System.out.println("普通方法");
	}
	public void abstract void print();//抽象方法,无方法体,有关键字abstract
}

//Single inheritance

class B extends A{
    
    //B是抽象类A的子类,是一个普通类
	@override
	public void print(){
    
    //必须复写抽象类A中的抽象方法
		System.out.println("Hello");
	}
	public void print(int a int b){
    
    
		System.out.println("抽象类子类重载抽象类方法");
	}
}

public class Test{
    
    
	public static void main(String[] args){
    
    
		A a=new B();//向上转型
		a.print();//被子类A所复写过的方法
	}
}

Restrictions on the use
of abstract classes · There are construction methods in abstract classes, whose purpose is to initialize properties, and when subclass objects are instantiated, it still satisfies to perform parent class construction first, and then subclass construction.
ex:

abstract class A{
    
    
	public A(){
    
    
	    System.out.println("A类构造方法");
	}
	public abstract void print();
}
class B extends A{
    
    
	public B(){
    
    
		System.out.println("B类构造方法");
	}
	@override
	public void print(){
    
    //必须实现抽象类中的方法
		System.out.print("Hello");
	}
}
public class TestDemo{
    
    
	public static void main(String[] args){
    
    
		A a=new B();
	}
}

Class A construction method
Class B construction method
· Abstract classes cannot use final declarations, because abstract classes must have subclasses, and final defined classes cannot have subclasses
· External abstract classes are not allowed to use static declarations, and internal abstract classes use static for operation Declaration
·The internal abstract class declared using static is equivalent to an external abstract class. When inheriting, use the form of "external class. internal class" to express the class name
ex:

abstract class A{
    
    
	static abstract class B{
    
    //static定义的内部类属于外部类
		public abstract void print();
	}
}
class C extends A.B{
    
    
	public void print(){
    
    
		System.out.println("ok")	
	}
}
public class Test{
    
    
	public static void main(String[] args){
    
    
		A.B ab=new C();
		ab.print();
	}
}

·You can directly call the static method declared in the abstract class. If you want to execute the static method in the class, you can call it directly without an object
. The same is true for the abstract class.
ex:

abstract class A{
    
    
	public static void print(){
    
    
		System.out.println("Hello");
	}	
}
public class Test{
    
    
	public static void main(String[] args){
    
    
		A.print();//直接调用抽象类中的static声明的方法print
	}
}

·Sometimes because the abstract class only needs a specific system subclass operation, so you can ignore the external subclass, which is more common in the system library, the purpose is to hide the subclass
ex that users don't need to know :

abstract class A{
    
    //抽象类
	public abstract void print();//抽象方法
	private static  class B extends A{
    
    //内部抽象类
		public void print(){
    
    //复写抽象方法
			System.out.println("Hello");
		}
	}

}
public static A instance(){
    
    

	return new B();	
}
public class Test{
    
    
	public static void main(String[] args){
    
    
		A a=A.instance();	
		a.print();
	}
}
fun(new P());//A a=new P();
a.command(A.EAT)//new P().command(1)    
abstract class A{
    
    
	public static final int EAT=1;
	public static final int WORK=1;
	public static final int SLEEP=1;
	public abstract eat();
	public abstract sleep();
	public abstract work();
	public void command(int flags){
    
    
		switch(flags){
    
    
		case EAT:
			this.sleep();
			break;
		case SLEEP:
	
	        case WORK:
	
		default:
			break;
		}
	}
}

class R extends A{
    
    
	eat(){
    
    }
	sleep(){
    
    }
	work(){
    
    }
}


class M extends A{
    
    
	eat(){
    
    }
	sleep(){
    
    }
	work(){
    
    }
}

class P extends A{
    
    
	eat(){
    
    }
	sleep(){
    
    }
	work(){
    
    }
}

public class Test{
    
    
	public static void fun(A a){
    
    
		a.command(A.EAT)
		a.command(A.SLEEP)
		a.command(A.WORK)

	}
	public static void main(String[] args){
    
    
		fun(new R());// A a=new R();
		fun(new M());
		fun(new P());	
	}
}


interface

When to use abstract classes and when to use interfaces
1. Is there a relationship between abstract classes and their subclasses (programmers and project managers are both employees).
2. Interfaces and their subclasses are related to whether they have or are there (both birds and airplanes have the characteristic of flying).

Difference
1. There are abstract methods and non-abstract methods in an abstract class, but all methods of the interface are abstract methods.
2. Abstract classes can only be inherited by single, and interfaces can be inherited by multiple.
3. The abstract class may contain non-final variables, and the variables that exist in the interface must be final, public, and static.

Concept
An interface has only the characteristics of methods and no implementation of methods, so these methods can be implemented by different classes in different places, and these implementations can have different behaviors (functions). The
interface can be a special class, composed of global variables and public The interface is a means to solve Java’s inability to use multiple inheritance. The
interface is a 100% abstract class, and the methods of the interface must all be abstract methods. The
interface has only constants and methods. Constants are decorated with public static final, and methods are decorated with public abstract.

Characteristics of interfaces
Like classes, interfaces can also have methods and properties, but the methods declared in the interface are abstract by default (only method identifiers, no method bodies), and support multiple inheritance

Why use interfaces
·Java does not support multiple inheritance, and make up for this defect through interfaces
·Realize decoupling

Syntax implementation
Use interface keyword to declare interface, implements keyword
ex:

interface Fly
{
    
    
	final int a = 10;
	void display();
}
class Test implements Fly
{
    
    
	public void display(){
    
    
		System.out.println("实现接口的抽象方法");
	}
}
class testclass{
    
    
	public static void main(String[] args){
    
    
		Test test=new Test();
		test.display();
		System.out.println(a);
	}
}

Key point
: You cannot directly instantiate an interface. The methods in the interface are abstract, and there is no method body.
· Use the reference of the interface type to point to an object that implements the interface, and you can call the methods in this interface.
(If a certain class implements a certain interface, then this class has the function of the interface, then we can use the reference of the interface type to point to this class without the reference of this class.) (A reference of a parent class can refer to an object of a subclass)
Fly test =new Test();//The Fly interface class reference points to an object that implements the Fly interface
. A class can implement more than one interface
. An interface can inherit another interface , Or other interfaces, interfaces can also be inherited, and multiple inheritance is possible
. To implement a certain interface for a class, name must implement all methods in this interface. All methods
in the interface are abstract and public, and all attributes Both are public static and final.
The interface is used to make up for the limitation
of the class’s inability to achieve multiple inheritance . Realize decoupling

Guess you like

Origin blog.csdn.net/baidu_41191295/article/details/113925967