小编Iod*_*zer的帖子

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 …
Run Code Online (Sandbox Code Playgroud)

c# polymorphism

-4
推荐指数
1
解决办法
55
查看次数

标签 统计

c# ×1

polymorphism ×1