.NET实验二
实验名称:实验二 面向对象程序设计
一、 实验目的
1. 理解类的定义、继承等面向对象的的基本概念;
2. 掌握 C#语言定义类及其各种成员(字段,属性,方法)的方法;
3. 掌握方法覆盖的应用;
4. 掌握接口的定义和实现方法。
二、实验要求
根据要求,编写 C#程序,并将程序代码和运行结果写入实验报告。
二、 实验内容
- 设计编写一个控制台应用程序,输入某个成年人的性别、身高和体重,计算其BMI体重指数,并得出结论。
(1) 编写一个抽象类 Adult,具有Height、Weight属性,CalculateBMI方法及Conclusion 抽象方法。
(2) 编写两个类 Man 和 Woman,继承 Adult 类,并覆盖 Conclusion 方法。
(3) 编写控制流程,输入某个成年人的性别、身高和体重。
1)源程序
using System;
using System.Globalization;
namespace Experiment_Two
{
abstract class Adult
{
public float Height;
public float Weight;
public Adult()
{
Height = 1.7F;
Weight = 60.0F;
}
public float CalculateBMI()
{
return Weight / (Height * Height);
}
public abstract void Conclusion();
}
class Man : Adult
{
public override void Conclusion()
{
float BMI = CalculateBMI();
if (BMI < 20)
{
Console.WriteLine("过轻");
}
else if (BMI < 25 && BMI >= 20)
{
Console.WriteLine("适中");
}
else if (BMI < 30 && BMI >= 25)
{
Console.WriteLine("过重");
}
else if (BMI < 35 && BMI >= 30)
{
Console.WriteLine("肥胖");
}
else
{
Console.WriteLine("非常肥胖");
}
}
}
class Woman : Adult
{
public override void Conclusion()
{
float BMI = CalculateBMI();
if (BMI < 19)
{
Console.WriteLine("过轻");
}
else if (BMI < 24 && BMI >= 19)
{
Console.WriteLine("适中");
}
else if (BMI < 29 && BMI >= 24)
{
Console.WriteLine("过重");
}
else if (BMI < 34 && BMI >= 29)
{
Console.WriteLine("肥胖");
}
else
{
Console.WriteLine("非常肥胖");
}
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入某个成年人的性别(0代表男性):");
int a = int.Parse(Console.ReadLine());
Console.WriteLine("请输入身高(米为单位):");
string string_A = Console.ReadLine();
float value1 = float.Parse(string_A, CultureInfo.InvariantCulture.NumberFormat);
Console.WriteLine("请输入体重(千克为单位):");
string string_B = Console.ReadLine();
float value2 = float.Parse(string_B, CultureInfo.InvariantCulture.NumberFormat);
if (a == 0)
{
Man man = new Man();
man.Weight = value2;
man.Height = value1;
man.Conclusion();
}
else
{
Woman woman = new Woman();
woman.Weight = value2;
woman.Height = value1;
woman.Conclusion();
}
}
}
}
2) 运行结果
2.编写一个控制台应用程序,输入正方形边长或者半径,计算其周长和面积并输出。
(1) 编写两个接口,接口 IShape 包含三个方法:initialize, getPerimeter, getArea。分别进行初始化、获取边长和面积,其返回值均为 decimal。接口 IDisplayresult 显示计算结果。
(2) 编写两个类,Square(正方形)和 Circle(圆形),实现 IShape和IDisplayresult接口。
1)源程序
using System;
public interface IShape
{
public decimal initialize(decimal l);
public decimal getPerimeter();
public decimal getArea();
}
public interface IDisplayresult
{
void showResult();
}
public class Square : IShape, IDisplayresult
{
public decimal length;
public decimal area;
public decimal initialize(decimal l)
{
length = l;
area = l;
return length;
}
public decimal getPerimeter()
{
return length;
}
public decimal getArea()
{
area = length * length;
return area;
}
public void showResult()
{
Console.WriteLine("正方形的面积是:" + getArea());
}
}
public class Circle : IShape, IDisplayresult
{
public decimal radius;
public decimal area;
private decimal pi = 3.141592653M;
public decimal initialize(decimal r)
{
radius = r;
area = 1;
return radius;
}
public decimal getPerimeter()
{
return radius;
}
public decimal getArea()
{
area = pi * radius * radius;
return area;
}
public void showResult()
{
Console.WriteLine("圆形的面积是:" + getArea());
}
}
class InterfaceImplementer : Square
{
static void Main()
{
Square square = new Square();
Console.WriteLine("请输入正方形的边长");
decimal length = decimal.Parse(Console.ReadLine());
square.initialize(length);
square.showResult();
Circle circle = new Circle();
Console.WriteLine("请输入圆形的半径");
decimal radius = decimal.Parse(Console.ReadLine());
circle.radius = radius;
circle.getArea();
circle.showResult();
}
}
2)运行结果
最新文章
- DOM的基本属性
- 小议安卓定位伪造-实战足不出户畅玩Pokemon Go
- PHP大神的十大优良习惯
- [OpenXml] Generate excel in memory and dump to file
- iOS随机数-备
- matlab GUI之常用对话框(二)-- 进度条的使用方法
- J2EE开发框架搭建(2) - springmvc4 + spring4 + hibernate4 整合
- LeetCode 442. Find All Duplicates in an Array (在数组中找到所有的重复项)
- Python冒号的解释
- [HOJ2634] How to earn more 最大权闭合子图
- 直方图均衡化与Matlab代码实现
- Python_迭代器-生成器-复习-习题_41
- 磁盘修改AF
- 第三次作业 (一)----------------------Visual Studio 2015的安装及单元测试
- shell中expect介绍
- 开发中,android手机WIFI无法使用,无SIM卡故障解决
- CloneZilla 恢复系统报错Waiting for device dev/disk/by-id/.....
- 使用spin.js优化等待ajax返回时的页面效果
- js数组去重的方法(转)
- C#实例 Unity依赖注入使用
热门文章
- Github快速访问
- (linux笔记)开放防火墙端口
- CentOS 7.9 环境下部署 Docker 服务
- memoのPython环境配置
- 国内 IoT 物联网平台终局的思考:未来只会有 3家
- 如何用jupyter打开代码并租用服务器进行运行
- 深入理解snp-calling流程——转载
- Xcode常用&;开发常用
- 解决:pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it‘s not in your PATH. See README file for more information.
- DAST 代码分析