2022年实验二面向对象程序设可用 .pdf
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《2022年实验二面向对象程序设可用 .pdf》由会员分享,可在线阅读,更多相关《2022年实验二面向对象程序设可用 .pdf(10页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、实验指导书- 1 - 实验二、面向对象程序设计2.1 面向对象程序设计一、 实验目的1.理解 C#语言是如何体现面向对象编程基本思想。2.掌握类对象的定义。3.了解类的封装方法,以及如何创建类和对象。4.了解成员变量和成员方法的特性。5.掌握静态成员的用法。二、实验要求1. 分析程序,上机验证结果。2. 写出程序,并调试程序,要给出测试数据和实验结果。3. 整理上机步骤,总结经验和体会。4. 完成实验日志和上交程序。三、实验内容题目一:程序分析(1)分析下面两个程序,确定那个程序好,说明理由。程序要求:定义一个圆类,计算圆的面积和周长。程序 1:public class circle publ
2、ic static void Main() double radium, delimeter, square; const double pai = 3.1415926; radium = Convert.ToInt32(Console.ReadLine(); delimeter = 2 * pai * radium; square = pai * pai * radium; Console.WriteLine(delimeter=0,square=1, delimeter, square); Console.ReadLine(); 程序 2:public class circle doubl
3、e delimeter, square; const double pai = 3.1415926; public void calculate(double rad) delimeter = 2 * pai * rad; square = pai * pai * rad; Console.WriteLine(delimeter=0,square=1,delimeter,square); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 10 页 - - - - - - -
4、 - - 实验指导书- 2 - public static void Main() double radium; circle cir = new circle(); radium = Convert.ToInt32(Console.ReadLine(); cir.calculate(radium); Console.ReadLine(); 第二个程序号,因为将相关的操作进行了提取,采用函数的方式,将相关的事物包装成一个处理过程,方便代码的重用。(2)分析程序,写出程序的运行结果,并上机进行验证。Using System; public class students string id,nam
5、e; int age; public students(string id,string name,int age ) this.id = id; this.name = name; this.age = age; public void Display() Console.WriteLine(id=0,name=1,age=2,id,name,age); public static void Main() /string id, name; /int age; students stu = new students(0001,zhangsan,16); stu.Display(); Cons
6、ole.ReadLine(); (3)分析程序,写出程序的运行结果,并上机进行验证。public class Date private int Year, Month, Day; public Date(int Year, int Month,int Day) this.Year=Year; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 10 页 - - - - - - - - - 实验指导书- 3 - this.Month=Month; this.Day=Day; p
7、ublic Date(System.DateTime dt) Year = dt.Year; Month = dt.Month; Day = dt.Day; public void DisplayDate() Console.WriteLine(0年1 月2 日,Year,Month,Day); public class Tester public static void Main() System.DateTime currentTime=System.DateTime.Now; Date dt=new Date(2008,7,18); dt.DisplayDate(); Date dt2
8、= new Date(currentTime);dt2.DisplayDate(); Console.ReadLine(); 题目二:程序编写(1)实现一个包含类属性方法的简单加法程序,并能显示结果。class Add int a; int b; int result; public Add(int a, int b) this.a = a; this.b = b; public int add() return this.result = this.a + this.b; public class Mains public static void Main() Add test = new
9、Add(233, 467); Console.WriteLine(test.add(); Console.Read(); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 10 页 - - - - - - - - - 实验指导书- 4 - (2)实现一个 Person类,要求:属性包含姓名、年龄、身份证号、工作、工资等,并显示各属性的值。名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - -
10、 - - - - 第 4 页,共 10 页 - - - - - - - - - 实验指导书- 5 - namespace ConsoleApplication2 class Person private string name; public string Name get return this.name; set this.name = value; private int age; public int Age get return this.age; set this.age = value; private string pid; public string Pid get retu
11、rn this.pid; set this.pid = value; private string position; public string Position get return this.position; set this.position = value; private string wage; public string Wage get return this.wage; set this.wage = value; public Person() public void showInformation() Console.WriteLine(name : + name +
12、 n + age : + age + n + pid : + pid + n + Position : + position + n + wage : $ + wage); public class Mains public static void Main() Person qihuan = new Person(); qihuan.Name = 齐欢; qihuan.Age = 22; qihuan.Pid = 0905010109; qihuan.Position = CEO; qihuan.Wage = 1000000; qihuan.showInformation(); Consol
13、e.Read(); 2.2 C#面向对象程序设计(二)一、实验目的1.掌握构造函数和析构函数的含义与作用、定义方式和实现,能够根据要求正确定义和重载构造函数。能够根据给定的要求定义类并实现类的成员函数。2.理解类的成员的访问控制的含义,公有、私有和保护成员的区别。3.掌握参数传递的用法。4.掌握属性的作用和使用。二、实验要求1. 分析程序,上机验证结果。2. 写出程序,并调试程序,要给出测试数据和实验结果。3. 整理上机步骤,总结经验和体会。4. 完成实验日志和上交程序。三、实验内容题目一:程序分析(1)分析程序,写出程序的运行结果,并上机进行验证,然后回答后面问题。名师资料总结 - - -精
14、品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 10 页 - - - - - - - - - 实验指导书- 6 - public class BankAccount static int totalAccountNumber=0; string BankAccountId; double initialDepositAmount = 0.00; public BankAccount(string myId) this.BankAccountId = myId; this.initialDeposi
15、tAmount = 0.00; totalAccountNumber+; public void displayid() Console.WriteLine(mbaid=0,initialDepositAmount=1,this.BankAccountId,this.initialDepositAmount); public static void display() Console.WriteLine(totalAccountNumber=0, totalAccountNumber); public class Tester public static void Main() BankAcc
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年实验二面向对象程序设可用 2022 实验 面向 对象 程序 可用
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内