Java Tour (10) Polymorphism (Continued) & Interface (Interface)

xiaoxiao2021-03-06  67

1> After understanding some concepts of polymorphism, let's take a look at the design of the class.

There is a way in the inheritance mode of the class called "pure inheritance" is like the picture below.

Because of the same interface, the base class can accept any messages sent to the derived class. What you have to do, just upload the object of the derived class, then you will no longer need to know what type of this object is. All objects are handled by polymorphism. It seems to be a good way, but the fact is that your derived class will have more interfaces or methods than the base class, such as the following.

It seems that this is the case that is often used in actual, but it also brings a shortcomings that cannot pass the base class to access the extension method. If such an error occurs in the program, the system will throw a ClassCastException. The exception, this runtime type check is called "Runtime Type Identification Run-Time Type Identification (RTTI)" About RTTI's later learning is more detailed.

Finally, there is an important concept to reiterate: People often confuse the characteristics of those non-objective objects of Java, such as overloading of the method, often being introduced to everyone as an object-oriented feature. Millions Remember "Not binding, it is not polymorphism". (I also thought that overloading is an embodiment of polymorphism. Khan ~)

Interface

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.

The next time you will look at the internal class.

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

New Post(0)