列表需要一个类型参数
List requires a type argument
编译代码时出现三个错误。
1.Using 通用类型 List 需要 1 个参数。
2.Using 通用类型 List 需要 1 个参数。
3. foreach 语句不能对类型的变量进行操作,因为 List 不包含 'GetEnumerator'
的 public 定义
多态示例程序如下。
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> shapes = new List<Shape>();
您需要在列表声明中提供形状类型,以便它知道它的列表是什么
如果您查看 List<T>
class, you'll notice that List
is a generic 类型(因此 <T>
)的文档,并且泛型类型需要一个(或更多)参数来指定对象的类型它会 use/contain。您必须指定 some 类型,即使它只是 object
。
在您的例子中,您有一个 Shape
对象列表,因此可以修改您的初始化代码(并通过使用集合初始化语法进行简化)以指定该类型:
var shapes = new List<Shape>
{
new Rectangle(10, 10),
new Circle(10),
new Triangle(10, 10),
new Circle(20)
};
编译代码时出现三个错误。 1.Using 通用类型 List 需要 1 个参数。 2.Using 通用类型 List 需要 1 个参数。 3. foreach 语句不能对类型的变量进行操作,因为 List 不包含 'GetEnumerator'
的 public 定义多态示例程序如下。
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> shapes = new List<Shape>();
您需要在列表声明中提供形状类型,以便它知道它的列表是什么
如果您查看 List<T>
class, you'll notice that List
is a generic 类型(因此 <T>
)的文档,并且泛型类型需要一个(或更多)参数来指定对象的类型它会 use/contain。您必须指定 some 类型,即使它只是 object
。
在您的例子中,您有一个 Shape
对象列表,因此可以修改您的初始化代码(并通过使用集合初始化语法进行简化)以指定该类型:
var shapes = new List<Shape>
{
new Rectangle(10, 10),
new Circle(10),
new Triangle(10, 10),
new Circle(20)
};