Java learning summary: 14

interface

Basic definition of interface

Strictly speaking, the interface belongs to a special class, and there are only abstract methods and global variables in this class.
You can use the interface keyword to define the interface in java.
Example: Define the interface

interface A{	//定义接口
	public static final String MSG="Hello World";	//全局变量
	public abstract void print();	//抽象方法
}

Principles of using the interface:
1. The interface must have subclasses, but at this time a subclass can use the implements keyword to implement multiple interfaces to avoid the single inheritance limitation;
2. The subclass of the interface (if it is not an abstract class), you must Overwrite all abstract methods in the
interface ; 3. The objects of the interface can be instantiated by the upward transformation of subclass objects.
Example: Implement the interface

interface A9{
    public static final String MSG="Hello World";
    public abstract void print();
}
interface B9{
    public abstract void get();
}
class X1 implements A9,B9{
    public void print(){
        System.out.println("A接口的抽象方法");
    }
    public void get(){
        System.out.println("B接口的抽象方法");
    }
}
public class Test1_1_4_3 {
    public static void main(String args[]){
        X1 x=new X1();
        A9 a=x;
        B9 b=x;
        a.print();
        b.get();
        System.out.println(A9.MSG);
    }
}
//结果
//A接口的抽象方法
//B接口的抽象方法
//Hello World

If a subclass inherits both the abstract class and the interface, it should be completed in the order of inheritance and then implementation of the interface.

Example: Subclasses inherit abstract classes and implement interfaces

interface A9{	//定义接口
    public abstract void print();	//抽象方法
}
interface B9{
    public abstract void get();
}
abstract class C9{
    public abstract void change();
}
class X1 extends C9 implements A9,B9{	//X1类继承了抽象类C9,实现了A9和B9两个接口
    public void print(){	//覆写A9中的方法
        System.out.println("A接口的抽象方法");
    }
    public void get(){
        System.out.println("B接口的抽象方法");
    }
    public void change(){	//覆写抽象类C9的方法
        System.out.println("C类的抽象方法");
    }
}
public class Test1_1_4_3 {
    public static void main(String args[]){
        X1 x=new X1();
        A9 a=x;
        B9 b=x;
        C9 c=x;
        a.print();
        b.get();
        c.change();
    }
}
//结果
//A接口的抽象方法
//B接口的抽象方法
//C类的抽象方法

Example: Multiple inheritance of interfaces

interface A{	//定义父接口
	public void funA();
}
interface B{	//定义父接口
	public void funB();
}
interface C extends A,B{	//利用extends,实现接口多继承
	public void funC();
}
class X implements C{	//实现C接口子类要覆写全部抽象方法
	public void funA(){}	//A接口定义的方法
	public void funB(){}	//B接口定义的方法
	public void funC(){}	//C接口定义的方法
}

Although the concept of the interface itself can only be composed of abstract methods and global variables, all internal structures are not restricted by these requirements, that is to say, ordinary internal classes, abstract internal classes, and internal interfaces can be defined in the interface .

Example: Define an abstract class in the interface

interface A{
   public void funA();
   abstract class B{	//定义接口中的抽象类
		public abstract void B();
	}
}
class X implements A{	//X实现了A接口
    public void funA(){
        System.out.println("Hello World");
    }
    class Y extends B{	//内部抽象类的子类,可以选择型继承
		public void funB(){}
	}
}

If you use static to define an internal interface within an interface, the interface represents an external interface.

Example:

interface A{
   public void funA();
   static interface B{	//外部接口
		public void funB();
	}
}
class X implements A.B{	//X实现了A接口
    public void funB(){}
}

The practical application of the interface-standard

Example: Define USB standard

interface USB{	//定义标准一定就是接口
	public void start();	//USB设备开始工作
	public void stop();		//USB设备停止工作
}

Example: Define a computer class

class Computer{
	public void plugin(USB usb){	//插入USB接口设备(子类对象)
		usb.start();	//开始工作
		usb.stop();		//停止工作
	}
}

Example: Define U plate

class Flash implement USB{
	public void start(){
		System.out.println("U盘开始使用");
	}
	public void stop(){
		System.out.println("U盘停止使用");
	}
}
public class Test{
	public static void main(String args[]){
		Computer com=new Computer();	//实例化计算机类
		com.plugin(new Flash);		//插入USB接口设备
	}
}

Application of interface-factory design pattern

interface Fruit{
    public void eat();
}
class Apple implements Fruit{
    public void eat(){
        System.out.println("吃苹果");
    }
}
class Orange implements Fruit{
    public void eat(){
        System.out.println("吃橘子");
    }
}
class Factory{	//定义工厂类,此类不提供属性
    public static Fruit getInstance(String className){
        if ("apple".equals(className)){
            return new Apple();
        }
        else if("orange".equals(className)){
            return new Orange();
        }
        else{
            return null;
        }
    }
}
public class Test1_1_4_4 {
    public static void main(String args[]){
        Fruit f=Factory.getInstance("orange");	//通过工厂类取得指定标记的对象
        f.eat();	//调用接口方法
    }
}

Insert picture description here
Insert picture description here
With this factory class, you don't have to modify the subclass of the instantiated interface every time.

The difference between abstract classes and interfaces

Insert picture description here
Note: If you write your own interface, you should never use the keyword new to directly instantiate the interface subclass, you should use the factory class.

49 original articles published · Liked 25 · Visits 1534

Guess you like

Origin blog.csdn.net/weixin_45784666/article/details/104318554