using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2017
{//形状类
public abstract class Shape
{//形状名称
public string ShapeName { get; private set;}
public Shape(string shapeName){
ShapeName=shapeName;}//输出周长
public virtual voidPrintPerimeter(double perimeter){
Console.WriteLine(ShapeName+" Perimeter: "+ perimeter);}//输出面积
public virtual voidPrintArea(double area){
Console.WriteLine(ShapeName+" Area: "+area);}//计算周长
public abstract doubleJsPerimeter();//计算面积
public abstract doubleJsArea();//}}
圆形、正方形类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2017
{//圆类
class Circle : Shape
{//圆的半径
public double R { get; set;}
public Circle():base("Circle"){
this.R =0;}//圆的周长
public override doubleJsPerimeter(){return2* Math.PI * R;}//圆的面积
public override doubleJsArea(){return Math.PI * R * R;}}//长方形类
class Rectangle : Shape
{//长方形的长和宽
public double Width { get; set;}
public double Height { get; set;}
public Rectangle():base("Rectangle"){
Width =0;
this.Height =0;}//长方形的周长
public override doubleJsPerimeter(){return(Width+Height)*2;}//长方形的面积
public override doubleJsArea(){return Width*Height;}}}
使用图形类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2017
{
class UseClass
{
public voidUseShape(){//实例化一个圆:
Circle circle = new Circle();//生成一个半径为0的圆
circle.R =20;//实例化一个长方形:
Rectangle rectangle = new Rectangle();//生成一个长宽为0的长方形
rectangle.Width =20;
rectangle.Height =30;//把子类赋给父类,能更好的体现多态性
IList<Shape> shapes = new List<Shape>();
shapes.Add(circle);
shapes.Add(rectangle);//遍历图形foreach(var shape in shapes){
shape.PrintPerimeter(shape.JsPerimeter());
shape.PrintArea(shape.JsArea());}}}}
main入口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2017
{
class Program
{staticvoidMain(string[] args){
UseClass useClass = new UseClass();
useClass.UseShape();
Console.ReadLine();}}}
图形类:基类using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApp2017{ //形状类 public abstract class Shape { //形状名称 public string ShapeName { get; pri