Interface schematic java

Interface is very important knowledge in Java, we need to understand the concept of a preliminary understanding of the "interface" from life, what is the interface?

1. life Interface

Life in the common interfaces:

USB, GB socket, Type-C, 3.5MM, ARJ45, Lighting Interface, HDMI, VGA, SATA, M.2, DisplayPort, lightning port, PCI-E

These interfaces have any effect?
USB A-Interface USB
1. mouse connector, keyboard, sound card, microphone, camera, fill light, U disk, mobile hard disk
2. specifications are USB device
3. The device itself determines, do something.

2. Java interface in use

Format:
interface interface name {
member variables
member method
}

[Compliance Interface Class
implements
class name of the class implements the interface {}

Interface member variables and member methods default attribute reasons

From the point of view of life

USB interface specifies the size and connectivity, but this interface to do what is determined by the equipment! ! !
Size is fixed ==> member variables default attribute is public static final

Equipment to do something, but regardless of the provisions connection ==> member methods, equipment needed to complete their own
default attribute public abstract modification

3. The interface summary

  1. Interface
    member variables default attributes public static final
    members of the public abstract method default properties

  2. A non-abstract class interface compliant interfaces, all interfaces required to complete the forced default attribute as a member of the public abstract method

  3. Between the interface and the interface that allows the extends keyword inheritance, and allowing an interface, a plurality of interfaces inherited
    interface A extends B, C
    Life: direct downward compatibility protocol

  4. You can use the default keyword interface modification methods, default method has a method body, it may be considered non-compulsory implementation is not required to enforce compliance with non-abstract interface, JDK1.8 new features

4. Interface Life demo

USB interface mapping from the life of
interface USB

USB devices must complete the prescribed method
void connect ();

Mouse class implements USB
mouse is a USB device, connect method must complete
keyboard class implements USB
keyboard is a USB device, connect method must be completed

USB port on the computer, we need to use the USB interface

Code Display

/*
从生活中映射USB接口
	interface USB
		规定USB设备必须完成的方法
		void connect();

鼠标类 implements USB 
	鼠标是一个USB设备,必须完成connect方法
键盘类 implements USB
	键盘是一个USB设备,必须完成connect方法
 */
/**
 * USB接口
 * @author Anonymous
 *
 */
interface USB {
	/**
	 * 要求所有的USB设备,必须完成的方法,告知USB接口连接之后完成的
	 * 功能是什么
	 */
	void connect();
}

/**
 * 鼠标类,遵从USB接口,实现connect方法
 * 
 * @author Anonymous
 */
class Mouse implements USB {
	@Override
	public void connect() {
		System.out.println("鼠标连接USB接口,控制光标");
	}
}

/**
 * Logi类,继承Mouse鼠标类
 * 		1. 鼠标设备
 * 		2. Logi间接遵从USB接口,是一个USB设备
 * @author Anonymous
 *
 */
class Logi extends Mouse {
	@Override
	public void connect() {
		System.out.println("Logi Master 2S");
	}
}

/**
 * 键盘类,遵从USB接口,实现connect方法
 *
 * @author Anonymous
 */
class Keyboard implements USB {
	@Override
	public void connect() {
		System.out.println("键盘连接USB接口,输入设备");
	}
}

/**
 * IKBC继承Keyboard类
 * 		1. 键盘设备
 * 		2. 间接遵从USB接口,也是一个USB设备
 * @author Anonymous
 *
 */
class IKBC extends Keyboard {
	@Override
	public void connect() {
		System.out.println("IKBC C87 静音红轴");
	}
}

/**
 * PC电脑类,使用USB接口,这里需要通过USB接口连接一个USB设备
 * 
 * @author Anonymous
 */
class PC {
	/**
	 * 电脑类连接USB接口连接方法,这里需要的是一个USB设备
	 * 
	 * @param usb USB接口对应的设备
	 */
	public void usbConnect(USB usb) {
		// usb设备执行connect方法
		usb.connect();
	}
}

public class Demo1 {
	public static void main(String[] args) {
		
		// 电脑中有一个方法是使用USB接口连接USB设备
		PC pc = new PC();
		
		// 鼠标和键盘都是USB设备
		Mouse mouse = new Mouse();
		Keyboard keyboard = new Keyboard();
		
		// 电脑需要通过USB接口连接USB设备,mouse鼠标就是USB设备
		pc.usbConnect(mouse);
		// keyboard键盘也是USB设备
		pc.usbConnect(keyboard);
		
		// 传入一个Logi类对象
		pc.usbConnect(new Logi());
		// 传入一个IKBC类对象
		pc.usbConnect(new IKBC());
	}
}
Released five original articles · won praise 8 · views 1270

Guess you like

Origin blog.csdn.net/qq_42581682/article/details/104523393