Java interface (translated from Java tutorials)

Source: Internet
Author: User

From http://www.cnblogs.com/ggjucheng/archive/2012/12/04/2802301.html

English from http://docs.oracle.com/javase/tutorial/java/IandI/interfaceAsType.html

When defining a new interface, it is to define a new reference data type. You can use the interface name anywhere, just like the names of other classes. If you define a reference variable of the interface type and assign a value to the class of the object of this variable, you must implement this interface.

For example, here is a method to find the largest object from a pair of objects. The classes corresponding to these objects have been implementedRelatable:

 
Public object findlargest (Object object1, object object2) {relatable obj1 = (relatable) object1; relatable obj2 = (relatable) object2; If (obj1 ). islargerthan (obj2)> 0) return object1; else return object2 ;}

Convert object1Relatable, you can callIslargerthanMethod.

If you useRelatableMany classes are implemented. Objects instantiated from any of these classes can be passed throughThe findlargest () method compares the size. Of course, the two objects provided are the same class. Similarly, they can all be compared using the following method:

Public object findsmallest (Object object1, object object2) {relatable obj1 = (relatable) object1; relatable obj2 = (relatable) object2; If (obj1 ). islargerthan (obj2) <0) return object1; else return object2;} public Boolean isequal (Object object1, object object2) {relatable obj1 = (relatable) object1; relatable obj2 = (relatable) object2; If (obj1 ). islargerthan (obj2) = 0) return true; else return false ;}

These methods apply to any "relatable" object, no matter which class they inherit. When they implementRelatableThey can be their own type andRelatable type. This gives some advantages of multi-inheritance, so that their behavior inherits from the parent class and interface.

Related Article

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.