using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Together
{
abstract class Shape<T>
{
protected T Length;
protected T Width;
public Shape(T length, T width)
{
Length = length;
Width = width;
}
public double Area()
{
double temp;
temp = Length * Width;
return temp;
}
public double Perimeter()
{
return 2*(Length + Width);
} }
class Rectangle : Shape<double>
{
public Rectangle(double length, double width) : base(length, width) { }
}}
提示错误:Error 1 Operator '*' cannot be applied to operands of type 'T' and 'T' C:\Documents and Settings\cie\桌面\Together\Together\Shape.cs 20 21 Together
Error 2 Operator '+' cannot be applied to operands of type 'T' and 'T' C:\Documents and Settings\cie\桌面\Together\Together\Shape.cs 25 24 Together
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Together
{
abstract class Shape<T>
{
protected T Length;
protected T Width;
public Shape(T length, T width)
{
Length = length;
Width = width;
}
public double Area()
{
double temp;
temp = Length * Width;
return temp;
}
public double Perimeter()
{
return 2*(Length + Width);
} }
class Rectangle : Shape<double>
{
public Rectangle(double length, double width) : base(length, width) { }
}}
提示错误:Error 1 Operator '*' cannot be applied to operands of type 'T' and 'T' C:\Documents and Settings\cie\桌面\Together\Together\Shape.cs 20 21 Together
Error 2 Operator '+' cannot be applied to operands of type 'T' and 'T' C:\Documents and Settings\cie\桌面\Together\Together\Shape.cs 25 24 Together
public double Area()
{
T temp;
temp = Length * Width;
return temp;
}
public double Perimeter()
{
return 2 * (Length + Width);
}
真是想不明白 模版为什么不能这样定义?