实验二 面向对象程序设计
一、实验目的
1. 理解类的定义、继承等面向对象的的基本概念;
2. 掌握C#语言定义类及其各种成员(字段,属性,方法)的方法;
3. 掌握方法覆盖的应用;
4. 掌握接口的定义和实现方法。
二、实验要求
根据要求,编写 C#程序,并将程序代码和运行结果写入实验报告。
三、实验内容
这次的实验同上次,我将两个实验写到了一个控制台程序中,这里会附上整体的代码,后面会单独截图
using System;
// 抽象类 People
abstract class People
{
// 字段
protected string name;
protected int age;
// 属性
public string Name
{
get { return name; }
set { name = value; }
}
// 构造函数
public People(string name, int age)
{
this.name = name;
this.age = age;
}
// 抽象方法
public abstract void Work();
}
// 学生类 Student
class Student : People
{
private string school;
// 构造函数
public Student(string name, int age, string school) : base(name, age)
{
this.school = school;
}
// 重写 Work 方法
public override void Work()
{
Console.WriteLine($"我是一名学生,名字是{name},今年{age}岁,学校是{school}.");
}
}
// 职工类 Employer
class Employer : People
{
private string workPlace;
// 构造函数
public Employer(string name, int age, string workPlace) : base(name, age)
{
this.workPlace = workPlace;
}
// 重写 Work 方法
public override void Work()
{
Console.WriteLine($"我是一名老师,名字是{name},年龄是{age},在{workPlace}工作.");
}
}
public interface IShape
{
void Initialize(decimal sideLengthOrRadius);
decimal GetPerimeter();
decimal GetArea();
}
// 定义IDisplayResult接口
public interface IDisplayResult
{
void DisplayResult();
}
// 实现IShape和IDisplayResult接口的Square类
public class Square : IShape, IDisplayResult
{
private decimal sideLength;
private decimal perimeter;
private decimal area;
// 实现IShape接口的方法
public void Initialize(decimal sideLength)
{
this.sideLength = sideLength;
}
public decimal GetPerimeter()
{
perimeter = 4 * sideLength;
return perimeter;
}
public decimal GetArea()
{
area = sideLength * sideLength;
return area;
}
// 实现IDisplayResult接口的方法
public void DisplayResult()
{
Console.WriteLine($"正方形的边长: {sideLength}, 周长: {perimeter}, 面积: {area}");
}
}
// 实现IShape和IDisplayResult接口的Circle类
public class Circle : IShape, IDisplayResult
{
private decimal radius;
private decimal perimeter;
private decimal area;
// 实现IShape接口的方法
public void Initialize(decimal radius)
{
this.radius = radius;
}
public decimal GetPerimeter()
{
perimeter = 2 * (decimal)Math.PI * radius;
return perimeter;
}
public decimal GetArea()
{
area = (decimal)Math.PI * radius * radius;
return area;
}
// 实现IDisplayResult接口的方法
public void DisplayResult()
{
Console.WriteLine($"圆的半径: {radius}, 周长: {perimeter}, 面积: {area}");
}
}
class Program
{
static void Main(string[] args)
{
// 创建学生实例并输出信息
Student student = new Student("段耀旭", 20, "石家庄铁道大学");
student.Work();
// 创建职工实例并输出信息
Employer employer = new Employer("洛熙蓝", 30, "陆灵思公司");
employer.Work();
Console.WriteLine("接下来开始进行实验2,计算图形周长和面积");
Console.WriteLine("_______________________________________");
while (true)
{
Console.WriteLine("******请选择图形形状******");
Console.WriteLine(" 1.正方形");
Console.WriteLine(" 2.圆形");
Console.WriteLine("*****输入0以退出程序*****");
int choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 0:
return;
case 1:
square();
break;
case 2:
circular();
break;
default:
Console.WriteLine("无效的选择");
break;
}
}
}
static void square()
{
Console.Write("请输入正方形的边长: ");
decimal squareSideLength = Convert.ToDecimal(Console.ReadLine());
// 创建Square对象
Square square = new Square();
square.Initialize(squareSideLength);
// 计算并显示正方形周长和面积
square.GetPerimeter();
square.GetArea();
square.DisplayResult();
Console.WriteLine(); // 空行
}
static void circular()
{
Console.Write("请输入圆的半径: ");
decimal circleRadius = Convert.ToDecimal(Console.ReadLine());
// 创建Circle对象
Circle circle = new Circle();
circle.Initialize(circleRadius);
// 计算并显示圆形周长和面积
circle.GetPerimeter();
circle.GetArea();
circle.DisplayResult();
Console.ReadKey(); // 防止控制台窗口关闭
}
}
1. 设计编写一个控制台应用程序,练习类的继承。
(1) 编写一个抽象类 People,具有”姓名”,”年龄”字段,”姓名”属性,Work 方法。
(2) 由抽象类 People 派生出学生类 Student 和职工类 Employer,继承 People 类,并
覆盖Work 方法。
(3) 派生类 Student 增加”学校”字段,派生类 Employer 增加”工作单位”字段。
(4) 在 Student 和 Employer 实例中输出各自不同的信息。
2. 编写一个控制台应用程序,输入正方形边长或者半径,计算其周长和面积并输出。
(1) 编写两个接口,接口 IShape 包含三个方法:initialize, getPerimeter, getArea。分
别进行初始化、获取边长和面积,其返回值均为 decimal。接口 IDisplayresult 显示计算结果。
(2) 编写两个类,Square(正方形)和 Circle(圆形),实现 IShape 和 IDisplayresult
接口。
四、实验总结
本次实验是有关继承方面的实验,主要需要注意的一个是字段和属性的区别,另一个是函数的多次重构,比如实验一的people就要求有name字段和Name属性,同时它的work函数也被student和teacher所重构使用,这是比较需要注意的
另外就是这次又把程序写到了一起,我觉得这样会方便老师的查询和自己debug,至于调试过程中的问题,如果确定不用某段代码时可以先将代码折叠起来,等到需要时再放开,会对观感有比较大的提升
注:本部分写本次实验过程中出现的问题、如何解决、注意事项、以及自己的经 验体会。