实验二  面向对象程序设计

一、实验目的

1. 理解类的定义、继承等面向对象的的基本概念;

2. 掌握C#语言定义类及其各种成员(字段,属性,方法)的方法;

3. 掌握方法覆盖的应用;

4. 掌握接口的定义和实现方法。

二、实验要求

根据要求,编写 C#程序,并将程序代码和运行结果写入实验报告。

三、实验内容

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

接口。

Program.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace experiment2

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine("*******************   实验一开始   *******************");

Employer em;

Student stu;

//创建对象

em = new Employer("石家庄铁道大学", "张三", 35);

stu = new Student("石家庄铁道大学", "李四", 23);

//输出信息

em.Work();

stu.Work();

Console.WriteLine("*******************   实验一开始   *******************");

Console.WriteLine();

Console.WriteLine();

Console.WriteLine("*******************   实验二开始   *******************");

//创建对象

Square sq = new Square();

Circle ci = new Circle();

//初始化

sq.initialize(4);

ci.initialize(3);

//正方形计算数据

sq.getPerimeter();

sq.getArea();

//圆形计算数据

ci.getPerimeter();

ci.getArea();

//打印结果

sq.display();

ci.display();

Console.WriteLine("*******************   实验二结束   *******************");

Console.ReadKey();

}

}

}

IDisplayresult.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace experiment2

{

//接口 IDisplayresult 显示计算结果。

interface IDisplayresult

{

void display();

}

}

IShape.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace experiment2

{

//(1) 编写两个接口,接口 IShape 包含三个方法:initialize, getPerimeter, getArea。分

//别进行初始化、获取边长和面积,其返回值均为 decimal。

interface IShape

{

void initialize(decimal b);

decimal getPerimeter();

decimal getArea();

}

}

People.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace experiment2

{

//(1) 编写一个抽象类 People,具有”姓名”,”年龄”字段,”姓名”属性,Work 方法。

abstract class People

{

protected String name;

protected int age;

public abstract void Work();

//public People(String name, int age)

//{

//    this.name = name;

//    this.age = age;

//}

}

}

Student.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace experiment2

{

//(2) 由抽象类 People 派生出学生类 Student 和职工类 Employer,继承 People 类,并

//    覆盖Work 方法。

//(3) 派生类 Student 增加”学校”字段,派生类 Employer 增加”工作单位”字段。

class Student : People

{

String school;

public Student(String school, String name, int age)

{

this.school = school;

this.name = name;

this.age = age;

}

public override void Work()

{

Console.WriteLine("我的名字叫" + name + ",我" + age + "岁了,我是" + school + "的一名学生");

//throw new NotImplementedException();

}

}

}

Square.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace experiment2

{

//正方形

class Square : IShape, IDisplayresult

{

decimal b;

decimal perimeter;

decimal area;

public void display()

{

Console.WriteLine("这个边长为" + b + "正方形的周长为" + perimeter + ",面积为" + area);

//throw new NotImplementedException();

}

public decimal getArea()

{

this.area = perimeter * perimeter;

return area;

//throw new NotImplementedException();

}

public decimal getPerimeter()

{

this.perimeter = b * 4;

return this.perimeter;

//throw new NotImplementedException();

}

public void initialize(decimal b)

{

this.b = b;

this.perimeter = 0;

this.area = 0;

//throw new NotImplementedException();

}

}

}

Circle.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace experiment2

{

class Circle : IShape, IDisplayresult

{

decimal r;

decimal perimeter;

decimal area;

public void display()

{

Console.WriteLine("这个半径为" + r + "圆形的周长为" + perimeter + ",面积为" + area);

//throw new NotImplementedException();

}

public decimal getArea()

{

this.area = (decimal)3.14 * r * r;

return area;

//throw new NotImplementedException();

}

public decimal getPerimeter()

{

this.perimeter = 2 * r * (decimal)3.14;

return perimeter;

//throw new NotImplementedException();

}

public void initialize(decimal b)

{

this.r = b;

this.perimeter = 0;

this.area = 0;

//throw new NotImplementedException();

}

}

}

Employer.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace experiment2

{

//    (2) 由抽象类 People 派生出学生类 Student 和职工类 Employer,继承 People 类,并

//        覆盖Work 方法。

//    (3) 派生类 Student 增加”学校”字段,派生类 Employer 增加”工作单位”字段。

class Employer : People

{

public String work_place;

public Employer(String work_place, String name, int age)

{

this.work_place = work_place;

this.name = name;

this.age = age;

}

public override void Work()

{

Console.WriteLine("我的名字叫" + name + ",我" + age + "岁了,我是" + work_place + "的一名职工");

//throw new NotImplementedException();

}

}

}

四、实验总结

注:本部分写本次实验过程中出现的问题、如何解决、注意事项、以及自己的经 验体会。

 

这次实验比上次顺利很多,因为根据上次的经验对c#语法了解很多,主要就剩编程了。

 

.NET程序设计实验二的更多相关文章

  1. Java程序设计 实验二 Java面向对象程序设计

    北京电子科技学院(BESTI) 实     验    报     告 课程:Java程序设计 班级:1353  姓名:李海空  学号:20135329 成绩:             指导教师:娄嘉鹏 ...

  2. C++程序设计--实验二

    第二次实验主要内容是函数重载,快速排序及其模板实现,简单的user类实现. 实验结论: 一.函数重载编程练习 /*编写重载函数add(),实现对int型,double型和Complex型数据的加法.在 ...

  3. #20175201 实验二:Java面向对象程序设计

    20175201 实验二:Java面向对象程序设计 实验二 Java面向对象程序设计 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L ...

  4. 2017-2018-2 20165215 实验二 Java面向对象程序设计

    20165215 实验二 Java面向对象程序设计 一.实验报告封面 课程:Java程序设计 班级:1652班 姓名:张家佳 学号:20165215 指导教师:娄嘉鹏 实验日期:2018年4月16日 ...

  5. 20165308 实验二 Java面向对象程序设计

    20165308 实验二 Java面向对象程序设计 实验二 Java面向对象程序设计 一.实验报告封面 课程:Java程序设计 班级:1653班 姓名:张士洋 学号:20165308 指导教师:娄嘉鹏 ...

  6. 20165230 《Java程序设计》实验二(Java面向对象程序设计)实验报告

    20165230 <Java程序设计>实验二(Java面向对象程序设计)实验报告 一.实验报告封面 课程:Java程序设计 班级:1652班 姓名:田坤烨 学号:20165230 成绩: ...

  7. 2016-2017-2 20155227实验二《Java面向对象程序设计》实验报告

    2016-2017-2 20155227实验二<Java面向对象程序设计>实验报告 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉 ...

  8. 2017-2018-2 20165330实验二《Java面向对象程序设计》实验报告

    实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验步骤 (一)单元测试 三种代码 伪代码:从意图层面来解 ...

  9. 20145215实验二 Java面向对象程序设计

    一.实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 二.实验步骤 (一)单元测试 (1)三种代码 伪代码: ...

随机推荐

  1. 怎么在 liunx 上安装docker

    怎么在 liunx 上安装docker 作为一个非科班出身自学的小白,踩过很多的坑,特此留下记录 以下在虚拟机上示例 系统:linux(centos7) 操作方式:xshell连接终端操作 1.打开x ...

  2. js json.stringfy()和json.parse()的用法

    1.JSON.parse;作用:将JavaScript对象表示法的JSON字符串转换为对象(字符串转对象)语法:JSON.parse(text [, reviver])text 必选. 一个有效的 J ...

  3. count()用法

  4. LGP7840题解

    给出一种新的理解方式,本质上和正解是一致的. 首先我们现在已经有了一个森林,我们现在要给他加一条边,加哪一条边是最优的呢? 假设加的边是 \((u,v)\),那么 \(((d[u]+1)^2-d[u] ...

  5. Android系统编程入门系列之硬件交互——通信硬件电信SIM卡

    现在的SIM卡通常具备基站定位.语音通话.短信消息.网络流量这四大功能,而在移动端是无法对SIM卡使用基站定位功能的,所以这里只介绍移动端如何使用SIM卡实现语音通话.短信消息.数据流量三个功能. 语 ...

  6. 使用ABP SignalR重构消息服务(一)

    使用ABP SignalR重构消息服务 最近协助蟹老板升级新框架,维护基础设施服务,目前已经稳了. 早上蟹老板看到我进入公司,马上就叫停我,说我为什么左脚先进公司,你这样会让我很难做耶,这样把我给你一 ...

  7. 两个宝藏|关于我在github上冲浪时的一个小技巧。

    你好呀,我是歪歪. 前几天在 github 上冲浪的时候,发现了两个宝藏东西. 我也不藏着掖着了,拿出来给大家分享一下. 这两个宝藏是关于 arthas 和 SOFARegistry 的,这两个东西都 ...

  8. Linux企业常用命令详解

    cat :查看 cat [-AbeEnstTuv] [--help] [--version] fileName 常用参数: -n :由 1 开始对所有输出的行数编号 -b :和 -n 相似,对于空白行 ...

  9. 《手把手教你》系列基础篇(八十)-java+ selenium自动化测试-框架设计基础-TestNG依赖测试-番外篇(详解教程)

    1.简介 经过前边几篇知识点的介绍,今天宏哥就在实际测试中应用一下前边所学的依赖测试.这一篇主要介绍在TestNG中一个类中有多个测试方法的时候,多个测试方法的执行顺序或者依赖关系的问题.如果不用de ...

  10. K8S搭建自动化部署环境 Jenkins下载、安装和启动

    镜像下载.域名解析.时间同步请点击 阿里云开源镜像站 一.jenkins 下载 jenkins下载地址:https://jenkins.io/zh/download/ 如下图,左边是稳定版本,右边是每 ...