Introduction and use of interface keyword

1. Definition and use of interface

format:

 

( 1 ) Abstract methods and constants in the interface body

Before JDK8 , there were only abstract methods in the interface body, and the access permissions must be public (it is allowed to omit public and abstract modifiers) . All of static constants are given access to public (allows you to omit public , Final and static modifier, the interface does not have variable )

( 2 ) The default instance method in the interface body

From JDK8 release, allowing the use of default keyword, the interface is defined in the body called default Examples of the method (not defined default the static method), default example of a method and a conventional common method is to use than the keyword default modified Instance method with method body. The access permission of the default instance method must be public (the public modifier is allowed to be omitted ).

( 3 ) The static method in the interface body

Starting from the JDK8 version, static methods are allowed to be defined in the interface body .

A class declares that it implements one or more interfaces by using the implements keyword . If you implement multiple interfaces, separate the interface names with commas. For example, Type A implements Printable and Addable interfaces:

class A  implements  Printable ,  Addable {

}

A class implements an interface, then this class will naturally have a constant interface, default method (to remove the default keyword), the class interface can also override the default method (Note that you need to remove the rewriting default Keywords). If a non- abstract class implements an interface, then this class must rewrite all abstract methods of the interface , that is, remove the abstract modification to give the method body .

 Two, interface callback

You can assign a reference to an object created by a class that implements an interface to the interface variable declared by the interface , then the interface variable can call the interface method overridden by the class and the default method in the interface

public class InterfaceTest {

	public static void main(String[] args) {
		ShowMessage sm = null;   //声明接口变量
		sm = new TV();  //接口变量中存放对象的引用
		sm.show("长城牌电视机");  //接口回调
		sm.f();  //接口回调
		
		sm = new PC();
		sm.show("惠普笔记本");  //接口回调
		sm.f();  //接口回调
	}

}
public interface ShowMessage {
	public abstract void show(String s);
	default void f(){
		System.out.println("default方法");
	}
}

 

public class TV implements ShowMessage{

	@Override
	public void show(String s) {
		System.out.println(s);
	}
	
	//重写default方法
	public void f(){
		System.out.println("重写了default方法");
	}
	
}
public class PC implements ShowMessage {

	@Override
	public void show(String s) {
		System.out.println(s);
	}

}

Great Wall TV
rewrites the default method
HP notebook
default method
 

Three, understand the interface

( 1 ) Interfaces can abstract important behavior standards, which are expressed in abstract methods.

( 2 ) The reference of the object of the class that implements the interface can be assigned to the interface variable, and the interface variable can call the interface method implemented by the class , which reflects the specific behavior of the class according to the behavior standard in the interface.

The idea of the interface is that it can require certain classes to have methods with the same name, but the specific content of the methods can be different, that is, these classes are required to implement the interface to ensure that these classes must have the methods declared in the interface (the so-called method binding ). While the interface requires some classes to have methods with the same name , it does not force these classes to have the same parent class .

abstract class MotorVehicles{
	abstract void brake();
}

interface MoneyFare{
	void charge();
}

interface ControlTemperature{
	void controlAirTemperature();
}

class Bus extends MotorVehicles implements MoneyFare{

	@Override
	public void charge() {
		System.out.println("公共汽车:一元/一张,不计公里数。");
	}

	@Override
	void brake() {
		System.out.println("公共汽车刹车。");
	}
	
}

class Taxi extends MotorVehicles implements MoneyFare,ControlTemperature{

	@Override
	public void charge() {
		System.out.println("出租车刹车。");
	}

	@Override
	void brake() {
		System.out.println("出租车:两元/一张,起价3公里。");
	}

	@Override
	public void controlAirTemperature() {
		System.out.println("出租车安装了空调。");
	}
	
}

class Cinema implements MoneyFare,ControlTemperature{

	@Override
	public void controlAirTemperature() {
		System.out.println("电影院安装了空调。");
	}

	@Override
	public void charge() {
		System.out.println("电影院:门票,十元/张");
	}
	
}

public class Motor {

	public static void main(String[] args) {
		Bus bus101 = new Bus();
		Taxi blueTaxi = new Taxi();
		Cinema cinema = new Cinema();
		
		MoneyFare fare;
		ControlTemperature temperature;
		
		fare = bus101;
		bus101.brake();
		fare.charge();
		
		fare = blueTaxi;
		temperature = blueTaxi;
		blueTaxi.brake();
		fare.charge();
		temperature.controlAirTemperature();
		
		fare = cinema;
		temperature = cinema;
		fare.charge();
		temperature.controlAirTemperature();
		
	}

}

 The bus brakes.
Bus: one yuan per one, not counting kilometers.
Taxi: two yuan/one, starting at 3 kilometers.
The taxi brakes.
The taxi is air-conditioned.
Cinema: Tickets, ten yuan/piece The
cinema is equipped with air conditioning.

Four, interface and polymorphism

The polymorphism generated by the interface means that different classes may have different implementation methods when implementing the same interface, so interface variables may have multiple forms when calling back interface methods

The core idea of ​​programming using interfaces is to use interface callbacks , that is, interface variables store references to objects of the class that implements the interface, so that interface variables can call back the interface methods implemented by the class

Using the interface can also reflect the open - close principle of program design , that is, open for extension and closed for modification.

public class Adver {

	public static void main(String[] args) {
		AdvertisementBoard board = new AdvertisementBoard();
		board.setAdver(new PhilipsCorp());
		board.show();
	}

}
public interface Advertisement {

	public void showAdvertisement();
	public String getCorpName();
}
public class AdvertisementBoard {

	Advertisement adver;

	public void setAdver(Advertisement adver) {
		this.adver = adver;
	}
	
	public void show(){
		if(adver != null){
			System.out.println("广告牌显示" + adver.getCorpName() + "公司的广告词:");
			adver.showAdvertisement();
		}else{
			System.out.println("广告牌无广告。");
		}
	}
}

 

public class PhilipsCorp implements Advertisement {

	@Override
	public void showAdvertisement() {
		System.out.println("只有最好的,只有更好。");
	}

	@Override
	public String getCorpName() {
		return "飞利浦";
	}

}

 The billboard shows Philips’ ad slogan:
Only the best, only better.

Five, abstract and interface comparison

 1 . Both abstract classes and interfaces can have abstract methods.

2 . There can only be constants in the interface, not variables; and the abstract class can have constants or variables.

3 . The abstract class can also have non- abstract methods, but there can be no default instance methods. Interfaces cannot have non- abstract methods (not default methods, but also methods with method bodies), but can have default instance methods.

When designing a program, you should determine whether to use abstract classes or interfaces based on specific analysis. abstract class in addition to providing important subclasses need to implement the abstract to the methods, but also provides a subclass can inherit variables and non- abstract methods. If a problem needs to be better solved by inheritance, for example, in addition to implementing the abstract method of the parent class, the subclass also needs to inherit some variables or inherit some important non- abstract methods from the parent class , you can consider using the abstract class. If a problem does not require inheritance, but requires several classes to give the implementation details of some important abstract methods, you can consider using interfaces.

 

Guess you like

Origin blog.csdn.net/qq_43629083/article/details/109013752