我想将非继承方法(特定于某些类的方法)仅在调用Main方法时放在抽象类的类型下。可能吗?还是有什么方法可以实现?
abstract class Shape
{
public int side;
public virtual int Getperimeter()
{
return side;
}
}
class Square : Shape
{
public Square()
{
}
public int NumberofSides() => side =4;
public override int GetPerimeter() => side*4;
}
class Pentagon : Shape
{
public Pentagon()
{
}
public int NumberofEqualSides() => side =5;
public override int GetPerimeter() => side*5;
}
public class main
{
public static void Main()
{
shape x,y;
x=new Square();
x.NumberofSides //Error
y=new Square();
y.NumberofEqualSides //Error
}
}
}
'Shape'不包含'NumberofSides'的定义,找不到可访问的扩展方法'NumberofSides'接受类型为'Shape'的第一个参数(缺少目录)。