C#程序设计之抽象类多态和接口
Posted shangzh!
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#程序设计之抽象类多态和接口相关的知识,希望对你有一定的参考价值。
C#程序设计之抽象类、多态和接口
题1
题目描述
定义一个抽象类CShape,包括抽象方法Area()(用来计算面积)和SetData()(用来实现输入现状大小)然后派生出三角形类CTriangle、矩形类CRect、圆形类CCircle,分别实现抽象方法Area()和SetData()。最后定义一个CArea类,计算这几个形状的面积之和,各形状的数据通过CArea类构造函数或成员函数来设置。编写一个完整的程序。
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Example_AbstrcatClass
public abstract class Shape
public abstract double Area();
public abstract void SetData();
public class Trianle : Shape
double dblEdge1, dblEdge2, dblEdge3;
public Trianle(double e1, double e2, double e3)
dblEdge1 = e1;
dblEdge2 = e2;
dblEdge3 = e3;
public override double Area()
double p = (this.dblEdge1 + this.dblEdge2 + this.dblEdge3) / 2.0;
double area = System.Math.Sqrt(p * (p - this.dblEdge1) * (p - this.dblEdge2) * (p - this.dblEdge3));
return area;
public override void SetData()
Console.WriteLine("The shape is a Triangle");
public class Rect : Shape
double dblWidth, dblHeight;
public Rect(double wid, double hei)
dblWidth = wid;
dblHeight = hei;
public override double Area()
double area = dblWidth * dblHeight;
return area;
public override void SetData()
Console.WriteLine("The shape is a Rect");
public class Circle : Shape
double dblRadius;
public Circle(double r)
dblRadius = r;
public override double Area()
double area = Math.PI * dblRadius * dblRadius;
return area;
public override void SetData()
Console.WriteLine("The shape is a Circle");
class Area
static void Main(string[] args)
Trianle tri = new Trianle(3, 4, 5);
tri.SetData();
Console.WriteLine("三角形的面积为:0", tri.Area());
Rect rect = new Rect(3, 5);
rect.SetData();
Console.WriteLine("矩形的面积为:0", rect.Area());
Circle cir = new Circle(10);
cir.SetData();
Console.WriteLine("圆形的面积为:0", cir.Area());
Console.Write("\\n");
double dblArea = 0.0;
dblArea = tri.Area() + rect.Area() + cir.Area();
Console.WriteLine("总的面积为:0", dblArea);
Console.Write("\\n");
//抽象类的多态
double dblArea1 = 0.0;
Shape shape;
shape = new Trianle(6, 8, 10);
shape.SetData();
Console.WriteLine("三角形的面积为:0", shape.Area());
dblArea1 += shape.Area();
shape = new Rect(4, 5);
shape.SetData();
Console.WriteLine("矩形的面积为:0", shape.Area());
dblArea1 += shape.Area();
shape = new Circle(12);
shape.SetData();
Console.WriteLine("圆形的面积为:0", shape.Area());
dblArea1 += shape.Area();
Console.Write("\\n");
Console.WriteLine("总的面积为:0", dblArea1);
Console.Read();
运行结果
题2
题目描述
定义一个接口Ishape,其中包含抽象方法Area()(用来计算面积)和SetData()(用来实现输出形状大小)。然后派生出三角形类CTriangle、矩形类CRect、圆形类CCircle,分别实现抽象方法Area()和SetData()。最后定义一个CArea类,计算这几个形状的面积之和,各形状的数据通过CArea类构造函数或成员函数来设置。编写一个完整的程序。
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IShape
public interface IShape
double Area();
void SetData();
public class Trianle : IShape
double dblEdge1, dblEdge2, dblEdge3;
public Trianle(double e1, double e2, double e3)
dblEdge1 = e1;
dblEdge2 = e2;
dblEdge3 = e3;
public double Area()
double p = (this.dblEdge1 + this.dblEdge2 + this.dblEdge3) / 2.0;
double area = System.Math.Sqrt(p * (p - this.dblEdge1) * (p - this.dblEdge2) * (p - this.dblEdge3));
return area;
public void SetData()
Console.WriteLine("The shape is a Triangle");
public class Rect : IShape
double dblWidth, dblHeight;
public Rect(double wid, double hei)
dblWidth = wid;
dblHeight = hei;
public double Area()
double area = dblWidth * dblHeight;
return area;
public void SetData()
Console.WriteLine("The shape is a Rect");
public class Circle : IShape
double dblRadius;
public Circle(double r)
dblRadius = r;
public double Area()
double area = Math.PI * dblRadius * dblRadius;
return area;
public void SetData()
Console.WriteLine("The shape is a Circle");
class Area
static void Main(string[] args)
Trianle tri = new Trianle(3, 4, 5);
tri.SetData();
Console.WriteLine("三角形的面积为:0", tri.Area());
Rect rect = new Rect(3, 5);
rect.SetData();
Console.WriteLine("矩形的面积为:0", rect.Area());
Circle cir = new Circle(10);
cir.SetData();
Console.WriteLine("圆形的面积为:0", cir.Area());
Console.Write("\\n");
double dblArea = 0.0;
dblArea = tri.Area() + rect.Area() + cir.Area();
Console.WriteLine("总的面积为:0", dblArea);
Console.Write("\\n");
//接口多态
double dblArea1 = 0.0;
IShape shape;
shape = new Trianle(6, 8, 10);
shape.SetData();
Console.WriteLine("三角形的面积为:0", shape.Area());
dblArea1 += shape.Area();
shape = new Rect(6, 10);
shape.SetData();
Console.WriteLine("矩形的面积为:0", shape.Area());
dblArea1 += shape.Area();
shape = new Circle(5);
shape.SetData();
Console.WriteLine("圆形的面积为:0", shape.Area());
dblArea1 += shape.Area();
Console.Write("\\n");
Console.WriteLine("总的面积为:0", dblArea1);
Console.Read();
运行结果
以上是关于C#程序设计之抽象类多态和接口的主要内容,如果未能解决你的问题,请参考以下文章