




下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、面向?qū)ο蟪绦蛟O(shè)計(jì)編程上機(jī)作業(yè)題目:(1) 調(diào)試構(gòu)造函數(shù)pen()例題using system;using system.collections.generic;using system.text;namespace consoleapplication2 class pen public string color; private int price; public pen() color=black; price=5; public void setprice(int newprice) price=newprice; public int getprice() return price;
2、public void setcolor(string newcolor) color=newcolor; public string getcolor() return color; class test public static void main()pen mypen=new pen() ;console.writeline(the price is 0,mypen.getprice();console.writeline(the color is 0,mypen.color); (2) 調(diào)試構(gòu)造函數(shù)重載pen()例題using system;class pen public stri
3、ng color; private int price; public pen() color = black; price = 5; public pen(string newcolor, int newprice) color = newcolor; price = newprice; public void setprice(int newprice) price = newprice; public void setprice() price = 5; public int getprice() return price; public void setcolor(string new
4、color) color = newcolor; public string getcolor() return color; class test public static void main() pen mypen = new pen(); pen hispen = new pen(green, 8); console.writeline(the price is 0, mypen.getprice(); console.writeline(the color is 0, mypen.color); console.writeline(the price is 0, hispen.get
5、price(); console.writeline(the color is 0, hispen.getcolor(); hispen.setprice(); console.writeline(the price is 0, hispen.getprice(); (3)使用重載方法求5和5.65的平方。using system;using system.collections.generic;using system.text;namespace consoleapplication8 class program public class app public int app1(int x
6、) return x * x; public double app1(double y) return y * y; static void main(string args) app x = new app(); console.writeline(5的平方是0, x.app1(5); console.writeline(5.65的平方是0, x.app1(5.65); (4)將運(yùn)算符+和- -進(jìn)行重載,使之可對字符型數(shù)據(jù)進(jìn)行運(yùn)算,即:對一個(gè)字符型變量a,當(dāng)a=l時(shí)a+(或+a)的值為m, a- -(或- -a)的值為kusing system;using system.collection
7、s.generic;using system.text;namespace consoleapplication6 public class chartest private char ch; public chartest() this.ch = ; public chartest(char val) this.ch = val; public char c get return this.ch; set this.ch = value; static public chartest operator +(chartest orig) chartest result = new charte
8、st(); result.ch = (char)(orig.ch + 1); return result; static public chartest operator -(chartest orig) chartest result = new chartest(); result.ch = (char)(orig.ch - 1); return result; public class overloadapp public static void main() chartest a = new chartest(l); chartest b = new chartest(l); cons
9、ole.writeline(original value is :0,1, a.c, b.c); a = +a; b = -b; console.writeline(current value is :0.1, a.c, b.c); a = +a; b = -b; console.writeline(final value is:0,1, a.c, b.c); (5)編寫一個(gè)控制臺程序,完成下列功能并輸出結(jié)果:創(chuàng)建一個(gè)類test1,用無參數(shù)的構(gòu)造函數(shù)輸出該類的類名;增加一個(gè)重載的構(gòu)造函數(shù),帶有一個(gè)string類型的參數(shù),在此構(gòu)造函數(shù)中將傳遞的字符串打印出來;在main方法中創(chuàng)建屬于test1類
10、的一個(gè)對象,不傳遞參數(shù);在main方法中創(chuàng)建屬于test1類的另一個(gè)對象,傳遞一個(gè)字符串“this is a string”;using system;using system.collections.generic;using system.text;namespace consoleapplication8 class program class test1 public string test; public test1() test = test1; public test1(string s) test = s; static void main(string args) strin
11、g x = this is a string; test1 app1 = new test1(); test1 app2 = new test1(x); console.writeline(0, app1.test); console.writeline(0, app2.test); (6)編寫一個(gè)控制臺程序,完成下列功能并輸出結(jié)果:創(chuàng)建一個(gè)類a,在a中編寫一個(gè)可以被重寫的帶int類型參數(shù)的方法mymethod,并在該方法中輸出傳遞的整型值加10后的結(jié)果;再創(chuàng)建一個(gè)類b,使其繼承自類a,然后重寫a中的mymethod方法,將a中接收的整型值加50,并輸出結(jié)果; 在main方法中分別創(chuàng)建類a和類
12、b的對象,并分別調(diào)用mymethod方法;using system;class classa public static int mymethod(int i) i=i+10; return i;class classb:classapublic static int mymethod(int i)i=i+50;return i;class testpublic static void main() classa a=new classa(); classb b=new classb(); console.writeline(請輸入一個(gè)數(shù):); string s=console.readlin
13、e(); int x=int32.parse(s); console.writeline(調(diào)用a類方法:0, classa.mymethod(x); console.writeline(調(diào)用b類方法:0, classb.mymethod(x); (7)利用委托實(shí)現(xiàn):當(dāng)用戶輸入一個(gè)字符1時(shí),調(diào)用兩個(gè)整數(shù)相加的方法,計(jì)算兩整數(shù)之和;當(dāng)用戶輸入一個(gè)字符2時(shí),調(diào)用兩個(gè)整數(shù)相減的方法,計(jì)算兩整數(shù)之差。using system;class appdelegate int process(int a,int b); public static int add(int a,int b) return a+b; public static int sub(int a,int b) return a-b;static void main() string s,s1,s2; process pro; console.writeline(請輸入一個(gè)數(shù):); s1=console.readline(); int a=int32.parse(s1); console.wr
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 安徽省名校2025屆高一下化學(xué)期末監(jiān)測試題含解析
- 2025屆安徽定遠(yuǎn)示范高中高二下化學(xué)期末統(tǒng)考試題含解析
- 山東省鄒城市第一中學(xué)2025年化學(xué)高二下期末質(zhì)量跟蹤監(jiān)視模擬試題含解析
- 檔案收費(fèi)存放管理辦法
- 軍用專用倉庫管理辦法
- 混合現(xiàn)實(shí)教學(xué)應(yīng)用-洞察及研究
- 酒體色澤穩(wěn)定性研究-洞察及研究
- 殘聯(lián)物資采購管理辦法
- 出差補(bǔ)貼管理制度
- 盆地油氣勘探新思路
- 村振興產(chǎn)業(yè)融合發(fā)展示范區(qū)建設(shè)項(xiàng)目運(yùn)營管理方案
- 2025年中考物理解題方法復(fù)習(xí)專題10力學(xué)壓軸題的常見解法
- 慈利一中選拔考試題及答案
- 殘疾人護(hù)理實(shí)操考試題及答案
- DB54∕T 0296-2023 文物古建筑消防安全評估規(guī)范
- 醫(yī)共體醫(yī)保管理工作制度
- 注塑模具保養(yǎng)維修培訓(xùn)
- 商城周年慶活動方案方案
- 2025新課標(biāo)教師培訓(xùn)
- 檢驗(yàn)科實(shí)習(xí)生培訓(xùn)
- 幼兒教育畢業(yè)論文8000字
評論
0/150
提交評論