实验三 面向对象编程基础(一)
- 格式:doc
- 大小:123.00 KB
- 文档页数:7
课程实验报告
课程名称:C#程序设计
实验项目名称:实验三面向对象编程基础专业班级:B11522
姓名:张旭刚
学号:20114052233
指导教师:钱文光
完成时间:2013 年9 月15 日
计算机科学与工程系
实验三面向对象编程基础(一)
一、实验目的
1、理解类与对象的基本概念;
2、掌握声明类的方法以及类内部字段与方法的声明;
3、掌握构造函数的定义方法。
二、实验内容
1.定义一个名为Cuboid的长方体类,类体中包含长(length)、宽(width)、高(high)字段,以及求体积方法Cubage()。在主程序中声明Cuboid对象,通过控制台界面接收输入的长、宽、高赋值给对象的length、width和high字段,并通过Cubage()方法求该长方体对象体积并输出。
2. 修改上题中的Cuboid类,在类中增加Cuboid的构造函数,函数声明中包含长、宽、高参数,函数体中通过参数为长、宽、高字段赋值。通过控制台界面接收输入的长、宽、高作为参数传递给Cuboid 对象,通过Cubage()方法求该长方体对象体积并输出。
3. 在Cuboid类中增加静态字段cuboidNumber,用于统计长方体对象个数。增加静态方法GetCuboidNumber(),返回长方体的数量。修改上题中的构造函数,在函数体中增加cuboidNumber 变量加1。增加输出长方体的数量。
4. 定义Cuboid的派生类正方体类Cube,增加静态字段cubeNumber,用于统计正方体对象个数。增加静态方法GetCubeNumber(),返回正方体的数量。增加求正方体体积的方法CubeCubage(){return length*length*length},定义Cube的构造函数Cube(double len),函数体中为cubeNumber自动加1,该构造函数自动调用基类的构造函数为length赋值。输出正方体体积以及正方体个数。
三、实验过程
ing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class Cuboid
{
public double length, width, high;
public void volum()
{
Console.WriteLine("体积为:" + length * width * high);
}
}
class Program
{
static void Main(string[] args)
{
Console.Write("请输入长方体的长:");
double length = double.Parse(Console.ReadLine());
Console.Write("请输入长方体的宽:");
double width = double.Parse(Console.ReadLine());
Console.Write("请输入长方体的高:");
double high = double.Parse(Console.ReadLine());
Cuboid c = new Cuboid();
c.length = length;
c.width = width;
c.high = high;
c.volum();
Console.Read();
}
}
}
2public class Cuboid
{
public double length, width, high;
static int cuboidnumber=0;
public void volum()
{
Console.WriteLine("体积为:" + length * width * high); }
public Cuboid(double l, double w, double h)
{
length = l;
width = w;
high = h;
}
}
class Program
{
static void Main(string[] args)
{
Console.Write("请输入长方体的长:");
double length = double.Parse(Console.ReadLine());
Console.Write("请输入长方体的宽:");
double width = double.Parse(Console.ReadLine());
Console.Write("请输入长方体的高:");
double high = double.Parse(Console.ReadLine());
Cuboid c = new Cuboid(length, width, high);
c.length = length;
c.width = width;
c.high = high;
c.volum();
Console.Read();
}