Java Tour (6) Interface

xiaoxiao2021-03-06  23

The interface keyword further enhances the concept of abstract. You can think of "pure" Abstract class. It allows developers to define the form of classes: method name, parameter list, and the type of return value, but there is no method of the body. Interface can also include data members, but it is natural and final for Static and Final. Interface only provides forms, do not talk.

To create a class that implements a (or a group) Interface, you must use the imports keyword. It means, "Interte wants to tell you what kind of" ", IMPLEMENTS wants you to tell you how it works" "

You can declare the method in the interface into public, but even if you don't talk, they are also public (this is automatic). So when you type, you must define this Interface method to be expressed as public. (Unless you have special purposes to define other)

Java's "Multiple Inheritance":

Java doesn't force you to inherit the Abstract or "specific" class, but you can only inherit a non-Interface class. But you can implement any multiple interfaces based on the need, so you can create a class similar to "multiple inheritance" in C .

The true purpose of the interface: can upload multiple basic types. The order of the interface is identical to the "use as a base class for the Abstact class": It is to ban the client programmer to create this class object, and reiterate this is an interface.

Is it used in Interface or a Abstract class?

Interface gives you the benefits of your Abstract class, gave your intelligence, so as long as the base class is designed to not include methods and member variables, you should use Interface.

Interface inheritance:

Normally, Extends can only be used for classes, but because an interface can be spliced ​​by multiple interfaces, you can use Extends to indicate multiple "base interfaces" when creating a new interface.

//

Interface Monster

{

Void meNace ();

}

Interface Dangerouster Extends Monster

{

Void destroy ();

}

Interface Lethal

{

Void kill ();

}

Interface Vampire Extends Dangerousmonster, Lethal

{

Void DrinkBlood ();

}

///

Subject to constant: Since the Interface data member is automatically static and final, Interface is a tool that creates a set of constant values, which is very similar to the ENU in C and C . E.g:

///

Public Interface Months

{

int

January = 1, February = 2, march = 3,

April = 4, May = 5, June = 6, July = 7,

August = 8, september = 9, october = 10,

November = 11, decEmber = 12;

}

For safety period, you can block the above to a package, but finally you get just an int, it is still not as safe as ENUM. However, we will leave Interface, we can create a final class, define some of the objects of STATIC Final, no problem. Tip: 1. There's also a month field in java.util.calendar.

2, Apache's Jakarta Commons project contains some tools to create enumeration types, see http://jakarta.apache.org/commons, below "LANG," in org.apache.commons.lang.enum. This package in. There are still some other useful class libraries in this project.

转载请注明原文地址:https://www.9cbs.com/read-81057.html

New Post(0)