試驗2類的封裝性和繼承性設計試驗_第1頁
試驗2類的封裝性和繼承性設計試驗_第2頁
試驗2類的封裝性和繼承性設計試驗_第3頁
試驗2類的封裝性和繼承性設計試驗_第4頁
試驗2類的封裝性和繼承性設計試驗_第5頁
已閱讀5頁,還剩1頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、實驗2類的封裝性和繼承性設計實驗一、實驗目的理解面向?qū)ο蠓庋b性、繼承性和多態(tài)性的基本概念,掌握聲明類、封裝類、 繼承類以及運行時多態(tài)性的實現(xiàn)方法。二、實驗內(nèi)容1.程序理解:1)設計復數(shù)類/復數(shù)類。public class Complex (private double real,im;實音B,虛夸Bpublic Complex(double real, double im) / 構(gòu)造方法 ( this.real = real;this.im = im;public Complex(double real)構(gòu)造方法重載( this(real,0);public Complex()( this(0

2、,0);public Complex(Complex c)/拷貝構(gòu)造方法(this(c.real,c.im);比較兩個對象是否相等public boolean equals(Complex c) (return this.real=c.real & this.im=c.im;)public String toString() (return (+this.real+this.im+i);)兩個對象相加改變當前對象,沒有返回新對象兩個對象相加,與add()方法參數(shù)一返回新創(chuàng)建對象,沒有改變當前public void add(Complex c) (this.real += c.real;this

3、.im += c.im;)public Complex plus(Complex c) 樣不能重載( 對象return new Complex(this.real+c.real, this.im+c.im);)兩個對象相減改變當前對象,沒有返回新對象兩個對象相減,與 subtract。方法返回新創(chuàng)建的對象,沒有改變當public void subtract(Complex c) (this.real -= c.real;this.im -= c.im;)public Complex minus(Complex c) 參數(shù)一樣不能重載(前對象return new Complex(this.rea

4、l-c.real, this.im-c.im);class Complex_ex (public static void main(String args) (Complex a = new Complex(1,2);Complex b = new Complex(3,5);Complex c = a.plus(b);返回新創(chuàng)建對象System.out.println(a+ + +b+ = +c); /*程序運行結(jié)果如下:(1.0+2.0i) + (3.0+5.0i) = (40.0+7.0i)*/2)在構(gòu)造方法中使用thispublic class Flower int petalCount

5、 = ; String s = new String (nullJi i public Flower(int petals) petalCount = petals;System, otic, print In ( Constructor w/ int arg only, petalCou!nt= + petalCounit) public Flower (String5 ss) System, out. print In ( Constructor w/ String arg only, s=rr + ssj ;3 ss;) public Flcnffer, (String si, Inc

6、petals)( this(petals);/! Chis (si; / Can11 call two1 this.s - s;Systeinoucpi:lntin (String int arga)1public Flower() Chis47);System.out.print In(fpdeauli: constructor (no 曰匚守a) rrJ ) public void print() /! this(11); / Not inside non-constructor!Sy3temb out.-printin ( fPpetalCoimt = + petalCo-unt + s

7、 = tr+ s); i public static void main(String args Flower k = new Flower(); x print ():3) Static關(guān)鍵字的使用class Chinesestatic String 二口嗔口七uy=中國上 聲明一個靜態(tài)的成員變量 Stirliig name;inis agfe;vo id s ingOurCory (類中的成員方法也可以直接訪問靜態(tài)成員變量System, dux pr int In (rr|3H !, 親愛的+ country) jpublic class Test.Ch.inesepublic st ax

8、 ic void main (Spring argis) 部態(tài)的成員變量可以通過類名為訪問它System., outr print In (ffPChxne3e country is fP + Chinese . country);Chinese chi = new Chinese(J;System, out. pr int In (*pChines country is + chi. country); chlsingOurCountry();2.編程題:請按照以下要求設計一個 Student類,并進行測試。要求如下:Student類中包含姓名、成績兩個屬性。2)分別給這兩個屬性定義兩個方法

9、,一個方法用于設置值,另一個方法用于獲取值。Student類中定義一個無參的構(gòu)造方法和一個接受兩個參數(shù)的構(gòu)造 方法,兩個參數(shù)分別為姓名和成績屬性賦值。4)在測試類中創(chuàng)建兩個 Student對象,一個使用無參構(gòu)造方法,然 后調(diào)用方法給姓名和成績賦值,另一個使用有參的構(gòu)造方法,在構(gòu)造 方法中給姓名和成績賦值。(提示:注意成員變量和成員方法的訪問權(quán)限設置,使用 this解決成 員變量與局部變量名稱沖突的問題)1)設計一個類,設計它的一個無參數(shù)構(gòu)造方法,構(gòu)造方法中打印一條 消息。main方法創(chuàng)建這個類的一個對象,觀察打印結(jié)果。2)在練習1)的基礎上增加一個重載的構(gòu)造方法,采用一個 String 參數(shù),

10、并在構(gòu)造方法中把這個 String消息打印出來。創(chuàng)建這個類的 對象,觀察打印結(jié)果。3)以練習2)創(chuàng)建的類為基礎上,創(chuàng)建屬于它的實例對象的一個數(shù)組, 但不要實際創(chuàng)建對象并分配到數(shù)組里。運行程序時,注意是否打印出 來自構(gòu)建方法調(diào)用的初始化消息?為數(shù)組中每個變量創(chuàng)建對象,觀察打印結(jié)果?(3)請按照以下要求設計一個 Outer類。要求如下:1)定義一個外部類Outer,并在該類中定義一個靜態(tài)內(nèi)部類Inner。2)在內(nèi)部類中定義一個靜態(tài)變量staticField 和一個靜態(tài) staticMethod(),并將該變量的值設置為靜態(tài)內(nèi)部類的靜態(tài)變量,該 方法中輸出”靜態(tài)內(nèi)部類的靜態(tài)方法”。3)定義一個測試類,在 main()方法中輸出staticField 的值,并且調(diào) 用靜態(tài)

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論