实验四面向对象程序设计.doc
文本预览下载声明
实验四 面向对象程序设计
实验类型: 验证性 实验课时: 8 指导教师: 陈志勇
时 间: 2015年 10月 21日 课 次: 第 3、4节 教学周次: 第 7周
实验分室: 5-4 实验台号: 29 实 验 员: 马征
一、实验目的
1.了解并掌握面向对象程序设计的基本思想和方法。
2.掌握。
3.掌握。
4.理解。rectangle类:
属性:长和宽两个属性;
方法:
无参数构造函数:长和宽的值为0
两个参数构造函数:长和宽的值为对应参数值
计算周长方法:返回长方形的周长
计算面积方法:返回长方形的面积
定义test类中,在Main方法实例化两个对象,并输出其周长和面积
程序:
namespace shiyan4._1
{
class rectangle
{
public int c, k, s, z;
public rectangle()
{
c = 0;
k = 0;
}
public rectangle(int c, int k)
{
this.c = c;
this.k = k;
}
public int zhouchang(int c, int k)
{
z = (c + k) * 2;
return (z);
}
public int mianji(int c, int k)
{
s = c * k;
return (s);
}
}
class test
{
static void Main(string[] args)
{
Console.WriteLine(请输入长方形的长:);
int x = Int32.Parse(Console.ReadLine());
Console.WriteLine(请输入长方形的宽:);
int y= Int32.Parse(Console.ReadLine());
rectangle a = new rectangle(x, y);
Console.WriteLine(长方形的周长为:{0}, a.zhouchang(x, y));
Console.WriteLine(长方形的面积为:{0}, a.mianji(x, y));
Console.Read();
}
}
}
运行结果:
2.
(1)设计一个Person1类,包含下列数据:
字段:姓名(name)、血型(blood)、体重(weight)、身高(height)。
属性:Name和Blood访问姓名(name)、血型(blood)字段
方法:显示姓名PrintName()、显示血型PrintBlood()、显示重量PrintWeight()、显示身高PrintHeight()、增加身高AddHeight()、增加体重AddWeight()、减少体重SubWeight()、显示对象本身Tostring()。
构造函数:Person1()
Person1(string name,string blood,int weight,int height)
对于姓名的设置要进行验证,字符数不能大于4 ,不能小于2。
(2)设计一个Person2类,除了Person1类字段及方法外,增加字段电话telephone,增加方法PrintTelephone(),两个构造函数person2()和Person2(string name,string blood,int weight,int height,int telephone)
(3)在主函数中创建Person1类和Person2类的对象,进行测试。
程序:
namespace shiyan4._2
{
class person1
{
public string name, blood;
pu
显示全部