




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、實驗名稱:類與對象 一 實驗目的:(1)理解C#語言是如何體現(xiàn)面向?qū)ο缶幊袒舅枷耄唬?)掌握類對象的定義;(3)了解類的封裝方法,以及如何創(chuàng)建類和對象;(4)了解成員變量和成員方法的特性;(5)掌握靜態(tài)成員的用法;(6)掌握構造函數(shù)和析構函數(shù)的含義與作用、定義方式和實現(xiàn),能夠根據(jù)要求正確定義和重載構造函數(shù)。能夠根據(jù)給定的要求定義類并實現(xiàn)類的成員函數(shù);(7)掌握參數(shù)傳遞的用法;(8)掌握屬性的作用和使用。二上機內(nèi)容:1)創(chuàng)建MyDataTime類,熟悉構造函數(shù)、析構函數(shù)的定義方法、屬性的定義方法以及一般方法的定義過程。(2)創(chuàng)建Fraction類,掌握運算符重載、靜態(tài)方法的使用及其與實例方法的
2、區(qū)別。(3)創(chuàng)建Swap類,掌握C#方法中參數(shù)的傳遞。(4)整理上機步驟,總結經(jīng)驗和體會。(4)完成實驗報告。四上機步驟:類的創(chuàng)建與應用:創(chuàng)建一個MyDataTime類,要求如下:(1)私有字段:year,month,day;(2)屬性:Year,Month,Day。注意在定義Month和Day的settor時要檢驗設置值的有效性,其中,同時在對Day進行設置的時候要注意閏年和平年的2月的天數(shù)。(3)方法:構造函數(shù):根據(jù)需求確定不同參數(shù)列表的構造方法。析構函數(shù):提示析構對象。PrintMyDataTime:以“2011/4/24”、“2011年4月24日”、“2011.4.24”、“二一一年四
3、月二十四日”的形式輸出Year,Month和Day。 using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication1 class MyDataTime private int year; public int Year set year = value; get return year; private int month; public int Month set if (value >= 1 && value <= 12) month =
4、value; else Console.WriteLine("month的賦值范圍為1,12;您輸入的值不正確"); get return month; public int day; public int Day set if (month = 2 ) if(year%400=0|(year%100!=0&&year%4=0) if(value>=1&&value<=29) day=value; else if(value>=1&&value<=28) day=value; else if (mont
5、h = 1 | month = 3 | month = 5 | month = 7 | month = 8 | month = 10 | month = 12) if (value >= 1 && value <= 31) day = value; elseif(value>=1&&value<=30)day=value; get return day; public MyDataTime(int x, int y, int z) Year=x; Month=y; Day=z; public void show1() Console.Wr
6、iteLine("您輸入的時間是:0/1/2", year, month, day); public void show2() Console.WriteLine("您輸入的時間是:0年1月2日", year, month, day); public void show3() Console.WriteLine("您輸入的時間是:0.1.2", year, month, day); class Program static void Main(string args) Console.WriteLine("請輸入年:&quo
7、t;); int a = int.Parse(Console.ReadLine(); Console.WriteLine("請輸入月:"); int b = int.Parse(Console.ReadLine(); Console.WriteLine("請輸入日:"); int c = int.Parse(Console.ReadLine(); MyDataTime k = new MyDataTime(a,b,c); k.show1(); k.show2(); k.show3(); Console.ReadLine(); 通過類程序說明靜態(tài)變量/方法
8、與實例變量/方法的區(qū)別:創(chuàng)建一個分數(shù)類(Fraction),要求如下:私有字段:FenZi,F(xiàn)enMu構造函數(shù):Fraction(int FenZi, int FenMu),注意要校驗分母,不能為0;方法:重載運算符和-(負號),完成分數(shù)的加減乘除以及求相反數(shù)運算。注意四種運算均為靜態(tài)方法。DaoShu:求分數(shù)的倒數(shù)。GongYueShu,GongBeiShu:分別用于求兩個整數(shù)的公約數(shù)和公倍數(shù),可以用于上述分數(shù)運算結果的化簡;Display:用于在屏幕上輸出分數(shù),形式為:。ToDouble:用于將分數(shù)轉換為一個小數(shù)。注意:運算符重載、公約數(shù)、公倍數(shù)、倒數(shù)為靜態(tài)方法,其余為實例方法。在驗證程序
9、中要用到兩類方法,并體會兩類方法的區(qū)別。using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication29 class Fraction private int FenZi; public int X set FenZi = value; get return FenZi; private int FenMu; public int Y set if (value = 0) Console.WriteLine("分母不能為0!&
10、quot;); else FenMu = value; get return FenMu; public Fraction(int FenZi,int FenMu) this.FenZi = FenZi; this.FenMu = FenMu; public void display() Console.WriteLine("得到分數(shù):0/1",FenZi,FenMu); public double ToDouble() double g=Convert.ToDouble(FenZi/FenMu); return g; public static int gongyuesh
11、u( int a1, int b1) int t = 1; do if(b1!=0) t = a1 % b1; a1 = b1; b1 = t; else break; while (t != 0); t = a1; return t; public static int gongbeishu( int a2, int b2) int h = a2 * b2; int s=gongyueshu( a2, b2); h = h / s; return h; public static void daoshu( int a3, int b3) int t=gongyueshu( a3, b3);
12、a3=a3/t; b3=b3/t; Console.WriteLine("0/1",a3,b3); public static Fraction operator *(Fraction f1, Fraction f2) Fraction f = new Fraction(0, 0); f.FenZi = f1.FenZi * f2.FenZi; f.FenMu = f1.FenMu * f2.FenMu; int x1 = gongyueshu( f.FenZi, f.FenMu); f.FenZi = f.FenZi / x1; f.FenMu = f.FenMu / x
13、1; return f; public static Fraction operator /(Fraction f1, Fraction f2) Fraction f = new Fraction(0, 0); f.FenZi = f1.FenZi * f2.FenMu; f.FenMu = f1.FenMu * f2.FenZi; int x1 = gongyueshu( f.FenZi, f.FenMu); f.FenZi = f.FenZi / x1; f.FenMu = f.FenMu / x1; return f; public static Fraction operator +(
14、Fraction f1, Fraction f2) Fraction f = new Fraction(0, 0); f.FenZi = f1.FenZi*f2.FenMu+f2.FenZi* f1.FenMu; f.FenMu = f1.FenMu * f2.FenMu; int x1=gongyueshu( f.FenZi, f.FenMu); f.FenZi = f.FenZi / x1; f.FenMu = f.FenMu / x1; return f; public static Fraction operator -(Fraction f1, Fraction f2) Fracti
15、on f = new Fraction(0, 0); f.FenZi = f1.FenZi * f2.FenMu - f2.FenZi * f1.FenMu; f.FenMu = f1.FenMu * f2.FenMu; int x1 = gongyueshu( f.FenZi, f.FenMu); f.FenZi = f.FenZi / x1; f.FenMu = f.FenMu / x1; return f; class Program static void Main(string args) Console.WriteLine("請輸入第一個分數(shù):"); Conso
16、le.WriteLine("請輸入分子:"); int aa = int.Parse(Console.ReadLine(); Console.WriteLine("請輸入分母:"); int ba = int.Parse(Console.ReadLine(); if (ba = 0) Console.WriteLine("分母不能為0!請重新輸入一個不為0的數(shù):"); ba = int.Parse(Console.ReadLine(); Console.WriteLine("請輸入第二個分數(shù):"); Console
17、.WriteLine("請輸入分子:"); int ab = int.Parse(Console.ReadLine(); Console.WriteLine("請輸入分母:"); int bb = int.Parse(Console.ReadLine(); if (bb = 0) Console.WriteLine("分母不能為0!請重新輸入一個不為0的數(shù):"); bb = int.Parse(Console.ReadLine(); Fraction f1 = new Fraction(aa, ba); Fraction f2 = n
18、ew Fraction(ab, bb); Fraction fa = f1 + f2; Fraction fb = f1 - f2; Fraction fc = f1 * f2; Fraction fd = f1 / f2; fa.display(); fb.display(); fc.display(); fd.display(); Console.ReadLine(); 方法中參數(shù)的傳遞:創(chuàng)建一個Swap類,分別定一個采用值參數(shù)、引用型參數(shù)、輸出型參數(shù)、數(shù)組型參數(shù)的方法,完成兩個整型數(shù)據(jù)和整型數(shù)組的排序和輸出。using System;using System.Collections.Ge
19、neric;using System.Linq;using System.Text;namespace ConsoleApplication30 class Swap public static void zhican(int a1, int a2) int t = 0; if (a1 > a2) Console.WriteLine("0>1", a1, a2); else t = a1; a1 = a2; a2 = t; Console.WriteLine("0>1", a1, a2); public static void xing
20、can(ref int a3, ref int a4) int t; if (a3<a4) t = a4; a4 = a3; a3 = t; Console.WriteLine("0>1", a3, a4); else Console.WriteLine("0>1", a3, a4); public static void UseOut(out int a5,out int a6) int t; a5=98; a6 = 565; if(a5<a6) t = a5; a5 = a6; a6 = t; Console.WriteLin
21、e("0>1", a5, a6); else Console.WriteLine("0>1", a5, a6); public static void ShuZu(params int k) for (int i = 0; i < k.Length-1 ; i+) for (int j = i+1; j < k.Length ; j+) int t = 0; if (k i > kj) t = ki; ki = kj; kj = t; Console.WriteLine("最后數(shù)組的順序:"); for (int i = 0; i < k.Length; i+) Console.Write("0 ", ki); class Program static void
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 社會服務法的實施現(xiàn)狀與效果探討試題及答案
- 客戶體驗在戰(zhàn)略中的重要性試題及答案
- 題庫豐富的VB試題及答案過濾
- 信息化時代的網(wǎng)絡管理發(fā)展試題及答案
- 法律職業(yè)的社會責任感試題及答案
- 運營效率與風險控制試題及答案
- 戰(zhàn)略規(guī)劃與目標一致性試題及答案
- 交互設計2025年軟件設計師試題及答案
- 網(wǎng)絡管理員考試行業(yè)動態(tài)試題及答案
- 文化差異對開發(fā)的影響試題及答案
- 長款厚大衣項目質(zhì)量管理方案
- 模擬試卷(7)-【中職專用】2025年職教高考語文沖刺模擬卷(職教高考)解析版
- 【MOOC】創(chuàng)新與創(chuàng)業(yè)管理-南京師范大學 中國大學慕課MOOC答案
- 《裝配式建筑工程施工》課件-裝配式隔墻與墻面構造
- 少先隊活動課《民族團結一家親-同心共筑中國夢》課件
- 物流運輸環(huán)境保護制度
- 法律科技融合發(fā)展
- 《公路建設項目文件管理規(guī)程》
- 2024-2030年中國產(chǎn)權交易行業(yè)前景動態(tài)與未來發(fā)展形勢報告
- DB11∕T 854-2023 占道作業(yè)交通安全設施設置技術要求
- 國家職業(yè)技術技能標準 6-30-99-00 工業(yè)機器人系統(tǒng)操作員 人社廳發(fā)2020108號
評論
0/150
提交評論