版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实验二、面向对象程序设计 2.1面向对象程序设计一、实验目的1.理解C#语言是如何体现面向对象编程基本思想。2.掌握类对象的定义。3.了解类的封装方法,以及如何创建类和对象。4.了解成员变量和成员方法的特性。5.掌握静态成员的用法。二、实验要求1. 分析程序,上机验证结果。2. 写出程序,并调试程序,要给出测试数据和实验结果。3. 整理上机步骤,总结经验和体会。4. 完成实验日志和上交程序。三、实验内容题目一:程序分析(1)分析下面两个程序,确定那个程序好,说明理由。程序要求:定义一个圆类,计算圆的面积和周长。程序1:public class circlepublic static void
2、Main()double radium, delimeter, square;const double pai = 3.;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 circledouble delimeter, square;const double
3、 pai = 3.;public void calculate(double rad)delimeter = 2 * pai * rad;square = pai * pai * rad;Console.WriteLine(delimeter=0,square=1,delimeter,square);public static void Main()double radium;circle cir = new circle();radium = Convert.ToInt32(Console.ReadLine();cir.calculate(radium);Console.ReadLine()
4、;第二个程序号,因为将相关的操作进行了提取,采用函数的方式,将相关的事物包装成一个处理过程,方便代码的重用。(2)分析程序,写出程序的运行结果,并上机进行验证。Using System;public class studentsstring id,name;int age;public students(string id,string name,int age )this.id = id; = name;this.age = age;public void Display()Console.WriteLine(id=0,name=1,age=2,id,name,age);p
5、ublic static void Main()/string id, name;/int age;students stu = new students(0001,zhangsan,16);stu.Display();Console.ReadLine();(3)分析程序,写出程序的运行结果,并上机进行验证。public class Dateprivate int Year, Month, Day;public Date(int Year, int Month,int Day)this.Year=Year;this.Month=Month;this.Day=Day;public Date(Sy
6、stem.DateTime dt)Year = dt.Year;Month = dt.Month;Day = dt.Day;public void DisplayDate()Console.WriteLine(0年1月2日,Year,Month,Day);public class Testerpublic static void Main()System.DateTime currentTime=System.DateTime.Now;Date dt=new Date(2008,7,18);dt.DisplayDate();Date dt2 = new Date(currentTime);dt
7、2.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 Add(233, 467); Console.Wri
8、teLine(test.add(); Console.Read(); (2) 实现一个Person类,要求:属性包含姓名、年龄、身份证号、工作、工资等,并显示各属性的值。namespace ConsoleApplication2 class Person private string name; public string Name get return ; set = value; private int age; public int Age get return this.age; set this.age = value; private stri
9、ng pid; public string Pid get return 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()
10、 Console.WriteLine(name : + name + 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 = ; qihuan.Position = CEO; qihuan.Wage = ; qihuan.showInfo
11、rmation(); Console.Read(); 2.2 C#面向对象程序设计(二)一、实验目的1. 掌握构造函数和析构函数的含义与作用、定义方式和实现,能够根据要求正确定义和重载构造函数。能够根据给定的要求定义类并实现类的成员函数。2. 理解类的成员的访问控制的含义,公有、私有和保护成员的区别。3. 掌握参数传递的用法。4. 掌握属性的作用和使用。二、实验要求1. 分析程序,上机验证结果。2. 写出程序,并调试程序,要给出测试数据和实验结果。3. 整理上机步骤,总结经验和体会。4. 完成实验日志和上交程序。三、实验内容题目一:程序分析(1) 分析程序,写出程序的运行结果,并上机进行验证,
12、然后回答后面问题。public class BankAccountstatic int totalAccountNumber=0;string BankAccountId;double initialDepositAmount = 0.00;public BankAccount(string myId)this.BankAccountId = myId;this.initialDepositAmount = 0.00;totalAccountNumber+;public void displayid()Console.WriteLine(mbaid=0,initialDepositAmount
13、=1,this.BankAccountId,this.initialDepositAmount);public static void display()Console.WriteLine(totalAccountNumber=0, totalAccountNumber);public class Testerpublic static void Main()BankAccount mba = new BankAccount();BankAccount mba2 = new BankAccount();BankAccount mba3 = new BankAccount();BankAccou
14、nt mba4 = new BankAccount();/ Console.WriteLine(mba2ID=0, mba2.BankAccountId);mba2.displayid();BankAccount.display();Console.ReadLine();请回答问题:(1)按你自己的算法修改以上程序,比如可只输出生成的账户数。(2)把注释去掉后会怎样,为什么?输出ID(3)为什么display 用类名直接引用,可以用对象来引用么?尝试输出结果。不能输出(4)类的静态变量和非静态变量的引用区别。判断一下语句的正确性:静态方法只能使用静态变量,不能使用实例变量。因为对象实例化之前,
15、实例变量不可用。这个观点正确吗?()类的静态变量只有一个版本,所有实例对象引用的都是同一个版本。()对象实例化后,每个实例变量都被制作了一个副本,它们之间互不影响。()题目二:程序编写1. 编写一个传值调用的程序。程序功能要求:程序首先给整型变量x 和y 赋初值3,5,然后使用传值调用方式调用方法对x 和y 做乘方并及输出x 和y 的乘方值,最后输出x和y得值。再将此方法给为对象调用加ref修饰查看输出结果差异。using System;namespace Test class Power void DisplayPower1(double x, double y) x = Math.Pow(
16、x, 2); y = Math.Pow(y, 2); Console.WriteLine(x的值为:0,y的值为:1, x, y); void DisplayPower2(ref double x,ref double y) x = Math.Pow(x, 2); y = Math.Pow(y, 2); Console.WriteLine(x的值为:0,y的值为:1, x, y); static void Main(string args) double a = 3, b = 5; Power A = new Power(); A.DisplayPower1(a, b); Console.Wr
17、iteLine(传值调用后a的值为:0,b的值为:1, a, b); A.DisplayPower2(ref a,ref b); Console.WriteLine(引用调用后a的值为:0,b的值为:1, a, b); Console.ReadLine(); 【思考题】1 方法的参数传递有哪些方式?区别时什么?值传递,引用传递,前者是新生成一个局部变量进行数据的处理,并不会影响原始的传入参数,后者类似C中的指针,会影响原来的值。2. 什么是构造方法。构造方法是一个类实例化后第一个执行的方法。它主要用于成员变量的初始赋值。2.3 类的继承一、实验目的1.掌握继承的工作机制和意义。2.掌握派生类的
18、定义方法和实现。3.掌握base关键字的使用。4. 编写体现类的继承性(成员变量,成员方法,成员变量隐藏)的程序。二、实验要求1. 写出程序,并调试程序,要给出测试数据和实验结果。2. 整理上机步骤,总结经验和体会。3. 完成实验日志和上交程序。三、实验内容1.进一步理解继承的含义新类可从现有的类中产生,并保留现有类的成员变量和方法并可根据需要对它们加以修改。新类还可添加新的变量和方法。这种现象就称为类的继承。当建立一个新类时,不必写出全部成员变量和成员方法。只要简单地声明这个类是从一个已定义的类继承下来的,就可以引用被继承类的全部成员。被继承的类称为父类或超类(superclass),这个新
19、类称为子类。2. 进一步理解继承的意义C# 提供了一个庞大的类库让开发人员继承和使用。设计这些类是出于公用的目的,因此,很少有某个类恰恰满足你的需要。你必须设计自己的能处理实际问题的类,如果你设计的这个类仅仅实现了继承,则和父类毫无两样。所以,通常要对子类进行扩展,即添加新的属性和方法。这使得子类要比父类大,但更具特殊性,代表着一组更具体的对象。继承的意义就在于此。题目一:类的继承和基类构造方法的应用程序功能要求如下:编写一个学生和教师数据输入和显示程序,学生数据有编号、姓名、班级和成绩,教师数据有编号、姓名、职称和部门。要求将编号、姓名输入和显示设计成一个类person,并作为学生数据操作类
20、student和教师类数据操作类teacher的基类。using System;namespace Test public class Person string name, sex, address; int age; public Person() ; public Person(string name, string sex, int age, string address) = name; this.sex = sex; this.age = age; this.address = address; Console.Write(n0,1,2,3, name, sex
21、, age, address); public class Students : Person private string id; public Students(string id):base() ; public Students(string name, string sex, int age, string address,string id) : base(name, sex, age, address) this.id = id; Console.WriteLine(0, id); class Test static void Main(string args) Person a
22、 = new Person(user, , 12, ); Students b =new Students(user,10, ,); Console.ReadLine(); 题目二:类的继承和构造函数的灵活应用编写一个程序计算出球、圆柱和圆锥的表面积和体积。要求:定义一个基类圆,至少含有一个数据成员半径;定义基类的派生类球、圆柱、圆锥,都含有求体积函数,可以都在构造函数中实现,也可以将求体积和输出写在一个函数中,或者写在两个函数中,请比较使用。定义主函数,求球、圆柱、圆锥的和体积。using System;using System.Collections.Generic;using Syste
23、m.Text;namespace ConAp1 public class Circle/圆类 protected double radius; public Circle(double r) radius = r; public double GetArea() return Math.PI * radius * radius; public class Sphere : Circle/球体类 public Sphere(double r) : base(r) public new double GetArea() return (4 * base.GetArea(); public double GetVolumn() return (4*Math.PI*Math.Pow(radius,3)/3); public class Cylinder : Circle/圆柱类 private double height; public Cylinder(double r, double h) : base(r) height = h; public new double GetArea() return (2 * base.GetAre
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 酒店常用语重点讲义资料
- 2026下半年山东省事业单位退役军人服务中心笔试历年真题试卷附参考答案
- 高中英语教资面试听力专项真题演练试卷
- 家庭教育讲座观后感
- 电力风险专项试题及详细答案
- 产品开发方专业试题及答案解析
- 福建省泉州市永春县2027届八上数学期末质量跟踪监视试题含解析
- 淘宝品牌运营模拟试题及答案
- 写给家长的写作入门指南
- 今日申论题目及答案解析
- 《铁路机车运用管理规则》
- DB45T 1625-2024 地质灾害危险性评估规程
- 家装园林设计合同范例
- 企业碳信息披露与质量评价规范
- DBJ52T 088-2018 贵州省建筑桩基设计与施工技术规程
- 看图猜词游戏规则模板
- DL∕T 1671-2016 火力发电厂空冷岛钢结构安装及验收标准
- 2024年民用航空器维修人员执照英语备考试题库(核心600题)
- 重庆市铜梁职业教育中心辅导员招聘考试真题2023
- 统计与概率《义务教育数学课程标准》解读
- 智能制造的发展与时代背景
评论
0/150
提交评论