【发布时间】:2019-05-19 05:10:24
【问题描述】:
编译代码时出现三个错误。 1.使用泛型 List 需要 1 个参数。 2.使用泛型 List 需要 1 个参数。 3. foreach 语句不能对类型变量进行操作,因为 List 不包含 'GetEnumerator' 的公共定义
多态示例的程序如下。
namespace PolymorExample
{
abstract class Shape
{
public abstract void area();
}
class Rectangle : Shape
{
private double length;
private double width;
public Rectangle(double length, double width)
{
this.length = length;
this.width = width;
}
public override void area()
{
Console.WriteLine("Rectangel Area: {0}", length * width);
}
}
class Triangle : Shape
{
private double baseline;
private double height;
public Triangle(double baseline, double height)
{
this.baseline = baseline;
this.height = height;
}
public override void area()
{
Console.WriteLine("Triangel Area: {0}", baseline * height / 2.0);
}
}
class Circle : Shape
{
const double PI = 3.14;
private double radius;
public Circle(double radius)
{
this.radius = radius;
}
public override void area()
{
Console.WriteLine("Circle Area: {0}", radius * radius * PI);
}
}
public class TestShape
{
static void Main()
{
List shapes = new List();
Shape shape1 = new Rectangle(10, 10);
shapes.Add(shape1);
shapes.Add(new Circle(10));
shapes.Add(new Triangle(10, 10));
shapes.Add(new Circle(20));
foreach (Shape s in shapes)
{
s.area();
}
Console.Read();
}
}
}
【问题讨论】:
-
试试
List<Shape>。 -
据我所知,.NET Framework 中没有 non-generic
List类型。您必须指定类型参数:List<Shape>
标签: c# list oop object-oriented-analysis