java-interface

Interface definition uses interface

Define use interface, call use implements.
Give a chestnut

/这个是定义接口
public interface MyInterface {
    
    
	//属性
	int NUM = 10 ;
	//方法
	void func();
	
}

The attributes and methods are defined in the interface.

Properties and methods in the interface

Attributes are all modified by public static final and represent public constants. Initial value must be assigned during definition. The
methods are all modified by public abstract, representing public abstract methods. There can be no method body.
These keywords do not need to be typed up by us, and the system will recognize them as this type.

There can be static methods in the interface

public interface MyInterface {
    
    
	//属性
	int num = 10 ;
	//静态方法
	public static void func() {
    
    
		System.out.println("这个是接口中的静态方法,方法名前有static修饰,直接采用接口名.方法名(参数列表)调用");
	}
}

Because the static method is called by the class name

The difference between interface and abstract class

  1. Attributes: All interfaces are public static final modified by default, that is, constants; attributes in abstract classes can be defined at will.
  2. Methods: The methods in the interface are all modified by public abstract by default, that is, abstract methods; abstract classes can have both abstract methods and instance methods.
  3. Construction method: The interface must not be defined, because the methods in the interface are abstract and the construction method cannot be inherited by subclasses; the abstract class can have a construction method, but it is meaningless, because the abstract class cannot create objects.

The concept and function of the interface

The interface is to achieve multiple inheritance.
A class can implement multiple interfaces, separated by commas, to implement
a class can implement multiple interfaces, the keyword to implement the interface is implements, when the interface is implemented, all abstract methods in all interfaces must be implemented, otherwise you must This class sets the abstract class.

When a class implements an interface, it is necessary to rewrite all abstract methods of the interface.

For example, there are two interfaces, one class implements two interfaces, and the two interfaces are separated by commas.
An interface defined

public interface MyInterface {
    
    
	void m();
	void m2();
}

The second interface defined

public interface MyInterface2 {
    
    
	void n1();
	void n2();
}

Class that implements the interface

public class Demo1 implements MyInterface , MyInterface2{
    
    
	int NUM = 15 ;

	@Override
	public void m() {
    
    
		// TODO Auto-generated method stub
		System.out.println("重写接口里面的抽象方法m");
	}

	@Override
	public void m2() {
    
    
		// TODO Auto-generated method stub
		System.out.println("重写接口里面的抽象方法m2");
	}

	@Override
	public void n1() {
    
    
		// TODO Auto-generated method stub
		System.out.println("重写接口里面的抽象方法n1");
	}

	@Override
	public void n2() {
    
    
		// TODO Auto-generated method stub
		System.out.println("重写接口里面的抽象方法n2");
	}

}

Then write to the main method, the interface reference points to the object of the implementation class, and only the methods defined in the interface can be called. Because the implementation class overwrites the methods in the interface, then the overridden methods in the implementation are called.

public class DemoMain {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		MyInterface my = new Demo1();
		//可以写接口的引用创建实现类的对象。
		my.m();
		my.m2();
	}

}

Insert picture description here

Constant interface

That is, all the properties in the interface are constants, because all the properties are modified by public static final.

public interface State {
    
    
	int DONG_JIE = 1;
	int ZHONG_DU = 2;
	int ZHUO_SHAO = 3;
	int CHEN_MO = 4;
	double PI = 3.1415926;
	String FILE_PATH = "C:\\myfile\\";
}

Interface can extend (extends) other interfaces

When I looked at the original code, I found that one interface extends another interface. I was so blinded that I had never seen it before.
Conclusion The relationship between interfaces and interfaces can only be inherited extends, it is impossible to implement implements

  • Interface can inherit interface
  • One interface can inherit multiple interfaces
  • The function I think is that it seems to simplify the code in a java file, but there is no simplification process in essence, because inheritance will inherit the non-private properties and methods inside.

For example,
two interfaces, one interface inherits the other interface, and then write an implementation class to implement the methods in the interface.

Interface: MyInterface02

public interface MyInterface02 {
    
    
	//定义一些常量属性
	 Integer num = 15 ;
	 String name = "root" ;
	 
	//定义一些抽象方法,没有方法体
	 int getNum( int num) ;
	 
	 void inputName() ;
}

Interface: MyInterface01 inherits interface MyInterface02

public interface MyInterface01 extends MyInterface02 {
    
     //接口只能继承接口
	 
	 //定义一些属性
	 int id = 123456 ;
	 //定义一些方法
	 void add() ;
}

Implementation class: TestClass implements the interface, then all abstract methods in the interface must be rewritten

public class TestClass implements MyInterface01{
    
    

	@Override
	public int getNum(int num) {
    
    
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void inputName() {
    
    
		// TODO Auto-generated method stub
		
	}

	@Override
	public void add() {
    
    
		// TODO Auto-generated method stub
		
	}
	
}

Guess you like

Origin blog.csdn.net/toomemetoo/article/details/112383477