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

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.