The essence of Java object-oriented thought (6)

1. Interface

1. Definition of interface

⑴ can be regarded as a special kind of abstract class;
⑵ and only contains abstract methods.

2. The syntax of the interface

(1) The definition is defined through the interface keyword;
(2) Member variables can not be defined in the interface, but constants can be defined;
(3) Only methods without method bodies can be defined (default is abstract methods).
public abstract void methodName();
public abstract can be omitted.

interface Inter12{
    
    
//	int a;    //不能定义成员变量
	static final int A1 = 1;
	public abstract void Aoo();//接口只包含抽象方法,抽象方法没有方法体实现
	void Boo();//接口中抽象方法可忽略不写public abstract
}

3. Implementation of the interface

A class can implement multiple interfaces, which are separated by commas.
3.1. Implement the interface through the implements keyword;
3.2. After a class implements the interface, it must implement the interface defined in the interfaceAll methods
3.3. The interface cannot be instantiated;
3.4. The interface can be used as a type to declare a variable. A variable of the interface type
can refer to the class object that implements the interface (upward modeling).
Interface name declaration variable = new class name();

public class InterfaceDemo2 {
    
    
	public static void main(String[] args) {
    
    
//		Inter12 o1 = new Inter12();接口不能被实例化
		Inter12 o2 = new Coo1();//向上造型 
	}
}

interface Inter12{
    
    
//	int a;    //不能定义成员变量
	static final int A1 = 1;
	public abstract void Aoo();//接口只包含抽象方法,抽象方法没有方法体实现
	void Boo();//接口中抽象方法可忽略不写public abstract
}

class Coo1 implements Inter12{
    
    
	public void Aoo() {
    
    }
	public void Boo() {
    
    }
}

interface Inter13{
    
    
	public void a1();
}

class Doo1 implements Inter12,Inter13{
    
    

	public void a1() {
    
    }
	public void Aoo() {
    
    }
	public void Boo() {
    
    }	
}

4. Interface inheritance and implementation

(1) The interface can inherit the interface (extends keyword).
The child interface inherits all the methods defined in the parent interface (that is, the methods that implement the parent interface).
(2) If you inherit and implement an interface, you should inherit first and then implement.
※ After a certain class implements an interface, it must implement all the methods defined in the interface (override).

interface Inter12{
    
    
//	int a;    //不能定义成员变量
	static final int A1 = 1;
	public abstract void Aoo();//接口只包含抽象方法,抽象方法没有方法体实现
	void Boo();//接口中抽象方法可忽略不写public abstract
}
class Coo1 implements Inter12{
    
    
	public void Aoo() {
    
    }
	public void Boo() {
    
    }
}
interface Inter13{
    
    
	public void a1();
}
class Doo1 implements Inter12,Inter13{
    
    

	public void a1() {
    
    }
	public void Aoo() {
    
    }
	public void Boo() {
    
    }	
}
interface Inter14 extends Inter13{
    
    
	public void Aoo();//继承了接口后,必须实现父类中定义的所有方法(抽想方法)
	public void Boo();
}
abstract class Eoo1{
    
    
	void b() {
    
    }
}
/*
 * 又继承又实现,则先继承父类,再实现接口
 */
class Foo extends Eoo1 implements Inter13{
    
     
	public void b() {
    
    }
	public void a1() {
    
    }
}

Two, polymorphism:

1. Meaning:

1) When references of the same type point to different objects, there are different implementations
—polymorphism of behavior: cut(), run(), sleep()...
2) The same object is shaped into different types At times, there are different functions
-polymorphism of objects: me, you, tree...

2.Upward modeling:

2.1. The reference of the super type points to the object of the derived class
2.2. The types that can be modeled are: super class + the implemented interface
2.3. What can be pointed out depends on the type of reference

3. There are only two conditions for successful reference type coercion:

3.1. The object pointed to by the reference is the type
3.2. The object pointed to by the reference implements the interface or inherits the class.
For example, the following example:

人 p1 = new 理发师();
人 p2 = new 医生();
人 p3 = new 演员();

p1.cut(); //剪发
p2.cut(); //看病
p3.cut(); //表演

abstract class{
    
    
  abstract void cut();
}
class 理发师 extends{
    
    
  void cut(){
    
     剪发 }
}
class 医生 extends{
    
    
  void cut(){
    
     看病 }
}
class 演员 extends{
    
    
  void cut(){
    
     表演 }
}

If the above two conditions are not met
during the forced conversion, a ClassCastException type conversion exception will occur. Recommendation: Before the forced conversion, use instanceof to determine whether the object pointed to by the reference is of this type.

public class MultiTypeDemo {
    
    
	public static void main(String[] args) {
    
    
		Aoo o = new Boo(); //向上造型
		Boo o1 = (Boo)o; //o所指向的对象,就是Boo类型
		Inter1 o2 = (Inter1)o; //o所指向的对象,实现了Inter1接口
		o2.a();
		//Coo o3 = (Coo)o; //ClassCastException类型转换异常
		if(o instanceof Coo) {
    
    
			Coo o4 = (Coo)o;
		}else {
    
    
			System.out.println(123456);
		}
	}
}

interface Inter1{
    
    
	public void a();
}
class Aoo{
    
    
}
class Boo extends Aoo implements Inter1{
    
    
	public void a() {
    
    
		System.out.println("实现Inter的方法");
	}
}
class Coo extends Aoo{
    
    
}

Please be patient for the follow-up content and come soon! Writing is not easy, please give a thumbs up 3Q
If there are errors, please comment

The catalog of all chapters is here: https://blog.csdn.net/qq_41254299/article/details/106638651

Please indicate the source for reprinting: https://blog.csdn.net/qq_41254299

This article is from [Superclover_'s blog]

Guess you like

Origin blog.csdn.net/qq_41254299/article/details/108265297