Identity type mode
Defines an interface that does not contain any methods, and uses it only to represent an abstract type. All classes that implement the interface imply that this type belongs.
For example, define a food interface, which does not contain any methods:
public interface Food{}//实现该接口的类都是食物类型
Fish:
public class Fish implements Food{...}
How to Eat:
public void eat(Food food){...}
Eating:
new Fish();//Fish实现了Food接口,标识其食物类型eat(fish);//合法Book book = new Book();//Book未实现Food接口eat(book);//编译错误
The so-called identity type pattern is a semantic constraint on the food parameters passed to the Eat () method with the help of the Java compiler. The food interface is known as the identity type interface, which has no methods and represents only an abstract type. In the JDK, there are two typical representations of type interfaces.
- Java.io.Serializable interface: An instance of a class that implements the interface can be serialized
- Java.io.Remote interface: An instance of a class that implements the interface can be used as a remote object
Identity type mode