




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实验名称:类与对象 一 实验目的:(1)理解C#语言是如何体现面向对象编程基本思想;(2)掌握类对象的定义;(3)了解类的封装方法,以及如何创建类和对象;(4)了解成员变量和成员方法的特性;(5)掌握静态成员的用法;(6)掌握构造函数和析构函数的含义与作用、定义方式和实现,能够根据要求正确定义和重载构造函数。能够根据给定的要求定义类并实现类的成员函数;(7)掌握参数传递的用法;(8)掌握属性的作用和使用。二上机内容:1)创建MyDataTime类,熟悉构造函数、析构函数的定义方法、属性的定义方法以及一般方法的定义过程。(2)创建Fraction类,掌握运算符重载、静态方法的使用及其与实例方法的
2、区别。(3)创建Swap类,掌握C#方法中参数的传递。(4)整理上机步骤,总结经验和体会。(4)完成实验报告。四上机步骤:类的创建与应用:创建一个MyDataTime类,要求如下:(1)私有字段:year,month,day;(2)属性:Year,Month,Day。注意在定义Month和Day的settor时要检验设置值的有效性,其中,同时在对Day进行设置的时候要注意闰年和平年的2月的天数。(3)方法:构造函数:根据需求确定不同参数列表的构造方法。析构函数:提示析构对象。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(); 通过类程序说明静态变量/方法
8、与实例变量/方法的区别:创建一个分数类(Fraction),要求如下:私有字段:FenZi,FenMu构造函数:Fraction(int FenZi, int FenMu),注意要校验分母,不能为0;方法:重载运算符和-(负号),完成分数的加减乘除以及求相反数运算。注意四种运算均为静态方法。DaoShu:求分数的倒数。GongYueShu,GongBeiShu:分别用于求两个整数的公约数和公倍数,可以用于上述分数运算结果的化简;Display:用于在屏幕上输出分数,形式为:。ToDouble:用于将分数转换为一个小数。注意:运算符重载、公约数、公倍数、倒数为静态方法,其余为实例方法。在验证程序
9、中要用到两类方法,并体会两类方法的区别。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("得到分数: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("请输入第一个分数:"); 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的数:"); ba = int.Parse(Console.ReadLine(); Console.WriteLine("请输入第二个分数:"); Console
17、.WriteLine("请输入分子:"); int ab = int.Parse(Console.ReadLine(); Console.WriteLine("请输入分母:"); int bb = int.Parse(Console.ReadLine(); if (bb = 0) Console.WriteLine("分母不能为0!请重新输入一个不为0的数:"); 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(); 方法中参数的传递:创建一个Swap类,分别定一个采用值参数、引用型参数、输出型参数、数组型参数的方法,完成两个整型数据和整型数组的排序和输出。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("最后数组的顺序:"); 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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 区块链技术的创新发展及其商业价值
- 人工智能在医疗人才培养中的角色与价值
- 医务人员法律意识培养与职业道德教育
- 以人为本的科技应用探索区块链在办公场景中的优势
- 医疗人文关怀与患者安全文化的构建
- 保护患者隐私医疗行业的挑战与策略
- 会议服务制作合同范例
- 人员外派劳务合同范例
- 小升初地理介绍课件
- 两人股合同范例
- 红色经典影片与近现代中国发展学习通超星期末考试答案章节答案2024年
- 国家开放大学《Web开发基础》形考任务实验1-5参考答案
- 小学语文“跨学科学习任务群”内涵及解读
- 感觉统合教育指导师理论考试复习题库(含答案)
- 申请开票额度合同范本
- 2024年安全员C证考试题库附答案
- 2024年生态环境执法大练兵比武竞赛理论考试题库-下(多选、判断题)
- 医院创建服务基层行创建资料(3.5.2医院感染相关监测C)
- 2024年山东省东营市中考道德与法治试卷真题(含答案)
- SQL语句创建学生信息数据库表的示例学生信息数据库表
- 河南省安阳市林州市2023-2024学年八年级下学期6月期末历史试题(解析版)
评论
0/150
提交评论