日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

课后作业4

發布時間:2024/1/17 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 课后作业4 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

1.繼承條件下的構造方法調用

class Grandparent {public Grandparent(){System.out.println("GrandParent Created.");}public Grandparent(String string) {System.out.println("GrandParent Created.String:" + string);}}class Parent extends Grandparent {public Parent(){//super("Hello.Grandparent."); System.out.println("Parent Created");// super("Hello.Grandparent."); }}class Child extends Parent {public Child(){System.out.println("Child Created");}}public class TestInherits {public static void main(String args[]){Child c = new Child();}}

運行結果:

先調用父類的構造函數,后調用子類的構造函數,因為子類繼承的父類所以,調用子類的構造函數時父類已經存在,所以時先調用父類的構造函數初始化父類,后調用子類的構造函數。

class Grandparent {public Grandparent(){System.out.println("GrandParent Created.");}public Grandparent(String string) {System.out.println("GrandParent Created.String:" + string);}}class Parent extends Grandparent {public Parent(){super("Hello.Grandparent.");System.out.println("Parent Created");// super("Hello.Grandparent."); }}class Child extends Parent {public Child(){System.out.println("Child Created");}}public class TestInherits {public static void main(String args[]){Child c = new Child();}}

運行結果:

用super調用父類構造函數,而且super必須是子類的第一行代碼。

思考:構造函數用于初始化,如果基類沒有初始化是無法進行任何操作的,所以必須在第一行代碼就進行初始化。

2.“方法覆蓋(override)”的要點

class Fuigai { public void coutFuigai() {System.out.println("fulei"); } } class Son extends Fuigai{public void coutFuigai() {super.coutFuigai();System.out.println("zilei");} } public class Test {public static void main(String[] args) {Fuigai a=new Fuigai();Son b=new Son();b.coutFuigai();a.coutFuigai();} }

運行結果:

同名時Java會覆蓋前一個方法的內容。

3.動手動腦3

class Parent{public int myValue=100;public void printValue() {System.out.println("Parent.printValue(),myValue="+myValue);} } class Child extends Parent{public int myValue=200;public void printValue() {System.out.println("Child.printValue(),myValue="+myValue);} } public class ParentChildTest { public static void main(String[] args) {Parent parent=new Parent();parent.printValue();Child child=new Child();child.printValue();parent=child;parent.printValue();parent.myValue++;parent.printValue();((Child)parent).myValue++;parent.printValue(); } }

運行結果

?第一條輸出父類的myValue;

第二條輸出子類的myValue;

第三條子類的對象child賦給parent,最后調用對象parent相當調用child;

第四條修改parent的myValue,但對象child已經賦給了parent,所以最后調用對象parent輸出的還是child的myValue的值;

第五條修改child的myValue,并最終同第四條一樣,輸出child的myValue的值。

轉載于:https://www.cnblogs.com/my---world/p/9890536.html

總結

以上是生活随笔為你收集整理的课后作业4的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。