-4
我想在C#中使用Java風格的多態。可能嗎?C#中的Java風格多態?
這裏是不編譯
using System;
namespace HelloWorld
{
public class Program
{
public static void Main (string[] args)
{
Triangle triangle = new Triangle(2);
Square square = new Square(3);
printID(square);
}
public void printID(Shape s){
Console.WriteLine ("id is " + s.id);
}
}
public class Shape{
public int id;
}
public class Triangle: Shape{
float b;
float height;
float area(){
return b*height/2;
}
public Triangle(int k){
id=k;
}
}
public class Square: Shape{
float side;
float area(){
return side*side;
}
public Square(int k){
id=k;
}
}
}
該消息是
MyClass.cs一個例子(11,4):錯誤CS0120:一個對象引用需要訪問非靜態成員`HelloWorld.Program.printID(HelloWorld.Shape)'
謝謝!
哦,我的,這是尷尬的,謝謝謝爾蓋。現在工作正常 – Iodizer