本文目錄一覽:
Java類的繼承
一個類不能直接繼承多個類,java是單繼承語言。比如說這樣:class A extends B,C 不能這樣寫,因為java不支持多繼承。但是可以像下面這樣實現繼承多個類:class A extends B,class C extends A,這樣C就同時繼承了B和A兩個類了。
java繼承
繼承是面向對象編程技術的一塊基石,因為它允許創建分等級層次的類。運用繼承,你能夠創建一個通用類,它定義了一系列相關項目的一般特性。該類可以被更具體的類繼承,每個具體的類都增加一些自己特有的東西。在Java 術語學中,被繼承的類叫超類(superclass ),繼承超類的類叫子類(subclass )。因此,子類是超類的一個專門用途的版本,它繼承了超類定義的所有實例變數和方法,並且為它自己增添了獨特的元素。
繼承一個類,只要用extends 關鍵字把一個類的定義合併到另一個中就可以了。為了理解怎樣繼承,讓我們從簡短的程序開始。下面的例子創建了一個超類A和一個名為B的子類。注意怎樣用關鍵字extends 來創建A的一個子類。
// A simple example of inheritance.
// Create a superclass.
class A {
int i, j;
void showij() {
System.out.println(“i and j: ” + i + ” ” + j);
}
}
class B extends A {
int k;
void showk() {
System.out.println(“k: ” + k);
}
void sum() {
System.out.println(“i+j+k: ” + (i+j+k));
}
}
class SimpleInheritance {
public static void main(String args[]) {
A superOb = new A();
B subOb = new B();
System.out.println(“Contents of superOb: “);
superOb.showij();
System.out.println();
subOb.i = 7;
subOb.j = 8;
subOb.k = 9;
System.out.println(“Contents of subOb: “);
subOb.showij();
subOb.showk();
System.out.println();
System.out.println(“Sum of i, j and k in subOb:”);
subOb.sum();
}
}
該程序的輸出如下:
Contents of superOb:
i and j: 10 20
Contents of subOb:
i and j: 7 8
k: 9
Sum of i, j and k in subOb:
i+j+k: 24
像你所看到的,子類B包括它的超類A中的所有成員。這是為什麼subOb 可以獲取i和j 以及調用showij( ) 方法的原因。同樣,sum( ) 內部,i和j可以被直接引用,就像它們是B的一部分。
儘管A是B的超類,它也是一個完全獨立的類。作為一個子類的超類並不意味著超類不能被自己使用。而且,一個子類可以是另一個類的超類。聲明一個繼承超類的類的通常形式如下:
class subclass-name extends superclass-name {
// body of class
}
你只能給你所創建的每個子類定義一個超類。Java 不支持多超類的繼承(這與C++ 不同,在C++中,你可以繼承多個基礎類)。你可以按照規定創建一個繼承的層次。該層次中,一個子類成為另一個子類的超類。然而,沒有類可以成為它自己的超類。
成員的訪問和繼承
儘管子類包括超類的所有成員,它不能訪問超類中被聲明成private 的成員。例如,考慮下面簡單的類層次結構:
/* In a class hierarchy, private members remain private to their class.
This program contains an error and will not compile.
*/
// Create a superclass.
class A {
int i;
private int j; // private to A
void setij(int x, int y) {
i = x; j = y;
}
}
// A”s j is not accessible here.
class B extends A {
int total; void sum() {
total = i + j; // ERROR, j is not accessible here
}
}
class Access {
public static void main(String args[]) {
B subOb = new B();
subOb.setij(10, 12);
subOb.sum();
System.out.println(“Total is ” + subOb.total);
}
}
該程序不會編譯,因為B中sum( ) 方法內部對j的引用是不合法的。既然j被聲明成private,它只能被它自己類中的其他成員訪問。子類沒權訪問它。
注意:一個被定義成private 的類成員為此類私有,它不能被該類外的所有代碼訪問,包括子類。
更實際的例子
讓我們看一個更實際的例子,該例子有助於闡述繼承的作用。新的類將包含一個盒子的寬度、高度、深度。
// This program uses inheritance to extend Box.
class Box {
double width; double height; double depth;
// construct clone of an object
Box(Box ob) { // pass object to constructor
width = ob.width;
height = ob.height;
depth = ob.depth;
}
// constructor used when all dimensions specified
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// constructor used when no dimensions specified
Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}
// constructor used when cube is created
Box(double len) {
width = height = depth = len;
}
// compute and return volume double
volume() {
return width * height * depth;
}
}
BoxWeight extends Box {
double weight; // weight of box
// constructor for BoxWeight
BoxWeight(double w, double h, double d, double m) {
width = w;
height = h;
depth = d;
weight = m;
}
}
class DemoBoxWeight {
public static void main(String args[]) {
BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3);
BoxWeight mybox2 = new BoxWeight(2, 3, 4, 0.076);
double vol;
vol = mybox1.volume();
System.out.println(“Volume of mybox1 is ” + vol);
System.out.println(“Weight of mybox1 is ” + mybox1.weight);
System.out.println();
vol = mybox2.volume();
System.out.println(“Volume of mybox2 is ” + vol);
System.out.println(“Weight of mybox2 is ” + mybox2.weight);
}
}
該程序的輸出顯示如下:
Volume of mybox1 is 3000.0
Weight of mybox1 is 34.3
Volume of mybox2 is 24.0
Weight of mybox2 is 0.076
BoxWeight 繼承了Box 的所有特徵並為自己增添了一個weight 成員。沒有必要讓BoxWeight 重新創建Box 中的所有特徵。為滿足需要我們只要擴展Box就可以了。
繼承的一個主要優勢在於一旦你已經創建了一個超類,而該超類定義了適用於一組對象的屬性,它可用來創建任何數量的說明更多細節的子類。每一個子類能夠正好製作它自己的分類。例如,下面的類繼承了Box並增加了一個顏色屬性:
// Here, Box is extended to include color.
class ColorBox extends Box {
int color; // color of box
ColorBox(double w, double h, double d, int c) {
width = w;
height = h;
depth = d;
color = c;
}
}
記住,一旦你已經創建了一個定義了對象一般屬性的超類,該超類可以被繼承以生成特殊用途的類。每一個子類只增添它自己獨特的屬性。這是繼承的本質。
超類變數可以引用子類對象
超類的一個引用變數可以被任何從該超類派生的子類的引用賦值。你將發現繼承的這個方面在很多條件下是很有用的。例如,考慮下面的程序:
class RefDemo {
public static void main(String args[]) {
BoxWeight weightbox = new BoxWeight(3, 5, 7, 8.37);
Box plainbox = new Box(); double vol;
vol = weightbox.volume();
System.out.println(“Volume of weightbox is ” + vol);
System.out.println(“Weight of weightbox is ” + weightbox.weight);
System.out.println();
// assign BoxWeight reference to Box reference
plainbox = weightbox;
vol = plainbox.volume(); // OK, volume() defined in Box
System.out.println(“Volume of plainbox is ” + vol);
/* The following statement is invalid because plainbox does not define a weight member. */
// System.out.println(“Weight of plainbox is ” + plainbox.weight);
}
}
這裡,weightbox 是BoxWeight 對象的一個引用,plainbox 是Box對象的一個引用。既然BoxWeight 是Box的一個子類,允許用一個weightbox 對象的引用給plainbox 賦值。
當一個子類對象的引用被賦給一個超類引用變數時,你只能訪問超類定義的對象的那一部分。這是為什麼plainbox 不能訪問weight 的原因,甚至是它引用了一個BoxWeight 對象也不行。仔細想一想,這是有道理的,因為超類不知道子類增加的屬性。這就是本程序中的最後一行被注釋掉的原因。Box的引用訪問weight 域是不可能的,因為它沒有定義。
是否可以解決您的問題?
Java實現繼承
js繼承有5種實現方式:
1、繼承第一種方式:對象冒充
function Parent(username){
this.username = username;
this.hello = function(){
alert(this.username);
}
}
function Child(username,password){
//通過以下3行實現將Parent的屬性和方法追加到Child中,從而實現繼承
//第一步:this.method是作為一個臨時的屬性,並且指向Parent所指向的對象,
//第二步:執行this.method方法,即執行Parent所指向的對象函數
//第三步:銷毀this.method屬性,即此時Child就已經擁有了Parent的所有屬性和方法
this.method = Parent;
this.method(username);//最關鍵的一行
delete this.method;
this.password = password;
this.world = function(){
alert(this.password);
}
}
var parent = new Parent(“zhangsan”);
var child = new Child(“lisi”,”123456″);
parent.hello();
child.hello();
child.world();
2、繼承第二種方式:call()方法方式
call方法是Function類中的方法
call方法的第一個參數的值賦值給類(即方法)中出現的this
call方法的第二個參數開始依次賦值給類(即方法)所接受的參數
function test(str){
alert(this.name + ” ” + str);
}
var object = new Object();
object.name = “zhangsan”;
test.call(object,”langsin”);//此時,第一個參數值object傳遞給了test類(即方法)中出現的this,而第二個參數”langsin”則賦值給了test類(即方法)的str
function Parent(username){
this.username = username;
this.hello = function(){
alert(this.username);
}
}
function Child(username,password){
Parent.call(this,username);
this.password = password;
this.world = function(){
alert(this.password);
}
}
var parent = new Parent(“zhangsan”);
var child = new Child(“lisi”,”123456″);
parent.hello();
child.hello();
child.world();
3、繼承的第三種方式:apply()方法方式
apply方法接受2個參數,
A、第一個參數與call方法的第一個參數一樣,即賦值給類(即方法)中出現的this
B、第二個參數為數組類型,這個數組中的每個元素依次賦值給類(即方法)所接受的參數
function Parent(username){
this.username = username;
this.hello = function(){
alert(this.username);
}
}
function Child(username,password){
Parent.apply(this,new Array(username));
this.password = password;
this.world = function(){
alert(this.password);
}
}
var parent = new Parent(“zhangsan”);
var child = new Child(“lisi”,”123456″);
parent.hello();
child.hello();
child.world();
4、繼承的第四種方式:原型鏈方式,即子類通過prototype將所有在父類中通過prototype追加的屬性和方法都追加到Child,從而實現了繼承
function Person(){
}
Person.prototype.hello = “hello”;
Person.prototype.sayHello = function(){
alert(this.hello);
}
function Child(){
}
Child.prototype = new Person();//這行的作用是:將Parent中將所有通過prototype追加的屬性和方法都追加到Child,從而實現了繼承
Child.prototype.world = “world”;
Child.prototype.sayWorld = function(){
alert(this.world);
}
var c = new Child();
c.sayHello();
c.sayWorld();
5、繼承的第五種方式:混合方式
混合了call方式、原型鏈方式
function Parent(hello){
this.hello = hello;
}
Parent.prototype.sayHello = function(){
alert(this.hello);
}
function Child(hello,world){
Parent.call(this,hello);//將父類的屬性繼承過來
this.world = world;//新增一些屬性
}
Child.prototype = new Parent();//將父類的方法繼承過來
Child.prototype.sayWorld = function(){//新增一些方法
alert(this.world);
}
var c = new Child(“zhangsan”,”lisi”);
c.sayHello();
c.sayWorld();
java什麼是繼承?什麼是父類?什麼是子類?
1、繼承是面向對象最顯著的一個特性。繼承是從已有的類中派生出新的類,新的類能吸收已有類的數據屬性和行為,並能擴展新的能力。
2、有繼承關係的類才能分出哪個是父類,哪個是子類,繼承用extends關鍵字,extends後面的類則表示父類,extends前面的類則是子類。在java中所有的類都默認繼承Object類,Object類是所有類的父類。
3、直接繼承Object的類可以稱之為Object的子類,間接繼承Object的類可以稱之為object的間接子類,object則是它的間接父類或者超類。
示例:
public class Parent{
}
public class Child extends Parent{
}
對Parent、Child來說有繼承關係,則Parent是Child的父類,Child是Parent的子類。由於Parent、Child都默認繼承Object類,所有Parent、Child都是Object的子類。
在java中,什麼叫繼承?為什麼要有繼承?
java是面向對象的編程語言,是類構成了java語言,而繼承又是java必不可少的,繼承就是當你寫一個類時,雖然這個類滿足了某些功能但是你又想拓展它的功能,此時你就可以使用繼承機制在寫一個該類的子類,來完成你需要的功能,一旦子類繼承了父類就擁有了父類的方法與域(除私有方法和私有域),在子類中也可以重寫父類的方法,覆蓋父類中同名的方法,定義與父類相同的域,隱藏父類同名的域,實現子類需要的功能。繼承的優點是提高了代碼的效率,避免了代碼重寫。
JAVA中什麼是繼承?
繼承在本職上是特殊一般的關係,即常說的is-a關係。子類繼承父類,表明子類是一種特殊的父類,並且具有父類所不具有的 一些屬性或方法。
1.Java繼承的語法格式:
Java繼承的關鍵字是:extends
public class 子類名 extends 父類名{…}
如: public class UNStudent extends Student {…} 註:1.子類又稱超類,拓展類 ;父類又稱基類。
2.Java中類的繼承只能是單繼承(單根繼承),即一個類只能繼承一個父類,但是一個類可以由多個類來繼承它。
3.Java會給每一個沒有設置父類的類,自動添加一個父類就是Object 。
擴展資料:
一、子類繼承父類的結果
1.子類繼承父類後,繼承到了父類所有的屬性和方法。 註:是所有。
2.子類可調用的方法也要看情況而定:
子類和父類在同一個包下時 「子類和子類的對象」可以調用父類的默認的,受保護的,公有的屬性以及方法。
子類和父類在不同的包下時,在子類中可以調用受保護的,公有的屬性以及方法,而子類的對象可以調用受保護的,公有的屬性以及方法。
二、方法的重寫
1.當子類和父類都有某種方法,而子類的方法更加要求細緻,或者實現功能不同,就需要方法的重寫。
2.重寫條件
①必須要存在繼承關係;只有繼承之間的關係才能有方法的重寫
②方法的返回值類型,方法名,參數個數,參數類型,參數順序,必須要完全一致;
如:父類中方法 public void play (int n ; String s){方法體1…}
重寫後的子類方法public void play(int n ; String s){方法體2…}
3.子類重寫方法時的訪問修飾符可以大於或者等於父類方法的訪問修飾符。
4.重寫後的方法會被優先調用。
三、自動轉型
自動轉型的實現要求有繼承關係
格式如下:父類名 對象名 = new 子類構造方法;
如:Student stu = new UNStudent;
而強制轉型格式如下:子類名 對象名 = (子類名)父類對象名
如:UNStudent un = (Student)stu;
自動轉型可以拓寬方法的作用訪問域
在使用自動轉型後,子類自己定義的方法是不能在自動轉型後執行;
原因是因為Java的編譯機制,它會優先判斷父類中是否存在該方法,如果存在則通過編譯,如果不存在則報錯。
第二種自動轉型:轉型後只需要不同類的不同對象調用想吐的方法,很方便!
/**訪問修飾符 返回值數據類型 方法名(父類類型 參數名,…){
調用方法。
}
父類名 對象名 = new 子類名;
子類名 對象名 = new 子類名;
方法名(對象名);
*/
比如拿到駕駛證為A2的人,可以駕駛重型貨車,當然也可以駕駛大型貨車,中型貨車,小型貨車,小客車,小轎車,摩托車… 可以選擇第二種自動轉型的方法,便於調用同一個方法。
自動轉型好處:1.減少冗餘代碼;2.在方法設置參數時,擴大訪問範圍。
四、多態
多態是由方法重載,繼承,方法重寫,自動轉型等技術的組合。
五、為什麼需要繼承?
1.提高代碼的重用性。
2.提高程序的擴展性。
參考資料:
JAVA繼承總結
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/276034.html