博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用多态求矩形的面积和周长以及圆形的面积和周长
阅读量:7124 次
发布时间:2019-06-28

本文共 1124 字,大约阅读时间需要 3 分钟。

//使用多态求矩形的面积和周长以及圆形的面积我周长

Shape shape = new Circle(5); //new Square(5,6);

double area = shape.GetArea();
double perimeter = shape.GetPerimeter();
Console.WriteLine(" 这个形状的面积是{0},周长是{1}",area,perimeter);
Console.ReadKey();

}

public abstract class Shape

{

 

public abstract double GetArea();
public abstract double GetPerimeter();

}

public class Circle : Shape
{
private double _r;
public double R
{
get { return _r; }
set { _r = value; }
}

public Circle(double r)

{

this.R = r;

}

public override double GetArea()
{
return Math.PI * this.R * this.R;
}
public override double GetPerimeter()
{
return 2*Math.PI*this.R;
}
}

public class Square : Shape

{
private double _height;
public double Height
{
get { return _height; }
set { _height = value; }

}

private double _width;
public double Width
{
get { return _width; }
set { _width = value; }
}

public Square(double height, double width)

{
this.Height = height;
this.Width = width;
}
public override double GetArea()
{
return this.Height * this.Width;
}
public override double GetPerimeter()
{
return (this.Height+this.Width)*2;
}
}

转载于:https://www.cnblogs.com/swlq/p/5397436.html

你可能感兴趣的文章
jQuery可放大预览的图片滑块
查看>>
SpringBoot连接Redis哨兵模式
查看>>
【WPF】ComboBoxItem的禁用
查看>>
HTML5_CSS3仿Google Play垂直菜单
查看>>
达观杯文本智能处理挑战赛 练手代码实现
查看>>
Tornado 简单入门教程(一)——Demo1
查看>>
Pgpool-II 最新小版本更新发布,PgSQL 负载均衡中间件
查看>>
数据传输加密方式总结
查看>>
U-Boot启动过程完全分析
查看>>
深入理解Java中的底层阻塞原理及实现
查看>>
shell编程之转义和引用
查看>>
云盾.态势感知情报生态合作发布
查看>>
可视化生信分析利器-Galaxy(第一讲)
查看>>
Java多线程1:进程和线程的区别
查看>>
BugkuCTF 域名解析
查看>>
JVM(二)—GC垃圾回收
查看>>
合并多个Excel文件
查看>>
2018年高教社杯全国大学生数学建模竞赛D题解题思路
查看>>
PHP排序函数
查看>>
如何更专业的 重装系统?
查看>>