Interfaces and Inheritance 接口与继承
?
一、接口
In the Java programming language, an interface is a reference type, similar to a class, that can contain only constants, method signatures, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods. Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces.
?
接口定義
類似:public interface DoIt {void doSomething(int i, double x);int doSomethingElse(String s);boolean didItWork(int i, double x, String s);}
接口實現
?
public class RectanglePlus implements DoIt{ public int width = 0; public int height = 0; public Point origin;
....
}
接口更改
如果要在接口中增加某個方法的辦法為:
1、繼承原來的類創建一個新的接口,在新的類中定義新的方法.
If you want to add additional methods to an interface, you have several options. You could create a DoItPlus interface that extends DoIt
2、you can define your new methods as default methods.
3、You could also define new static methods to existing interfaces.
?
接口的其他要求:
1、An interface declaration can contain method signatures, default methods, static methods and constant definitions. The only methods that have implementations are default and static methods.
2、A class that implements an interface must implement all the methods declared in the interface.
3、An interface name can be used anywhere a type can be used.
二、繼承
A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).
?A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.
三、接口與繼承的區別
1、One significant difference between classes and interfaces is that classes can have fields whereas interfaces cannot.
2、In addition, you can instantiate a class to create an object, which you cannot do with interfaces.
3、Inherited instance methods from classes can override abstract interface methods
四、多態
?Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class.
雖然各子類繼承自同一父類的同一個功能,但是各子類可以定義各自獨一無二的行為或者功能。
Note the overridden printDescription method. In addition to the information provided before, additional data about the suspension is included to the output.
多態在子類中重新編寫父類中的某個函數,除了使用前面父類中功能為,額外增加自己獨有的功能。
五、在子類中如何調用父類方法和變量
Using the Keyword super Accessing Superclass Members;
super.superclass method or filed;
super(); super(parameter list); 調用父類的構造函數。
六、公共類或者方法
The? Object class, in the java.lang package, sits at the top of the class hierarchy tree.
Every class you use or write inherits the instance methods of Object. You need not use any of these methods, but, if you choose to do so, you may need to override them with code that is specific to your class.
有很多方法已經寫好,可以供我們調用,超過50個。下面是一些常見的方法:
protected Object clone() throws CloneNotSupportedException ????? Creates and returns a copy of this object.
- public boolean equals(Object obj) ????? Indicates whether some other object is "equal to" this one.
- protected void finalize() throws Throwable ????? Called by the garbage collector on an object when garbage ??????collection determines that there are no more references to the object
- public final Class getClass() ????? Returns the runtime class of an object.
- public int hashCode() ????? Returns a hash code value for the object.
- public String toString() ????? Returns a string representation of the object.
七、final
如果我們寫的類或方法不想讓覆蓋或者重寫,那么可以加 final 關鍵字。
You can declare some or all of a class's methods final. You use the final keyword in a method declaration to indicate that the method cannot be overridden by subclasses. The Object class does this—a number of its methods are final.
八、抽象類
父類只聲明方法,不可以實例化。子類必須要進行方法實例化,除非使用abstract進行聲明。
?
Summary of Inheritance
Except for the Object class, a class has exactly one direct superclass. A class inherits fields and methods from all its superclasses, whether direct or indirect. A subclass can override methods that it inherits, or it can hide fields or methods that it inherits. (Note that hiding fields is generally bad programming practice.)
The table in?Overriding and Hiding Methods ?section?shows the effect of declaring a method with the same signature as a method in the superclass.
The Object class is the top of the class hierarchy. All classes are descendants from this class and inherit methods from it. Useful methods inherited from Object include toString(), equals(), clone(), and getClass().
You can prevent a class from being subclassed by using the final keyword in the class's declaration. Similarly, you can prevent a method from being overridden by subclasses by declaring it as a final method.
An abstract class can only be subclassed; it cannot be instantiated. An abstract class can contain abstract methods—methods that are declared but not implemented. Subclasses then provide the implementations for the abstract methods
轉載于:https://www.cnblogs.com/clebean/p/7592273.html
總結
以上是生活随笔為你收集整理的Interfaces and Inheritance 接口与继承的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CentOS 7配置静态IP地址 解决了
- 下一篇: Tomcat部署Web应用