Introduction to Java (17) - Interface

Introduction to Java (17) - Interface

interface

      - The interface is also a "reference data type" and is also a class bytecode file after compilation
      - The interface is completely abstract (abstract classes are semi-abstract)
      * Definition:
           [modifier list] interface interface name {            }       - supports multiple inheritance       - interface Contains only two parts: constants and abstract methods       - All elements are public modified       - When defining abstract methods, the public abstract modifier can be omitted       - The public static final of constants can be omitted       - Classes and classes are called inheritance extends, classes and interfaces The relationship between implementations is called implementations. They can be inherited and implemented at the same time, but they must be inherited first and then implemented.       If a non-abstract class implements an interface card, all abstract methods of the interface must be implemented [covered]       - The interface cannot have a constructor method       - Make up It eliminates the defect that only single inheritance can be supported between classes. It is equivalent to a plug-in. It can be called when needed, which improves scalability       - polymorphism can be used.










is a ; has a ; like a

       - is a: anything that satisfies is a represents an "inheritance relationship"
       - has a: anything that satisfies has a represents an "association relationship"
       - like a: anything that satisfies like a represents an "implementation relationship"

Sample code


interface Intf01{
	public static final String A="动物:";
	String B="有翅膀的";//public static final省略
	
	//public Intf() {}
	//接口不能具有构造函数
	
	public abstract void AI();
}
interface Intf02{
	public abstract void AI(String i);
}
abstract class AC implements Intf01,Intf02{ //多继承
	public AC() {//抽象类可以接构造方法
		System.out.println(A);
	}
}
class Test01 extends AC implements Intf01 {//Intf02被间接继承
	public Test01(){
        System.out.print("这是一只");
	}
	public void AI() {
		System.out.print(B);
	}
	public void AI(String i) {
		System.out.println(i);
	}
}

public class Interface {
	public static void main(String[] args) {
		Test01 t=new Test01();
		t.AI();
		t.AI("老鹰");
		
		Intf01 tt=new Test01();//类型转换
		Intf02 ttt=(Intf02)tt;
		//接口与接口之间可以进行强制类型转换,但是可能会出错
		ttt.AI("鸡");
	}

}

Guess you like

Origin blog.csdn.net/qq_61562251/article/details/135047150