The given code has a syntax error as there are missing semicolons at the end of each line. The corrected code should be:

using System;

public abstract class A
{
    public A()
    {
        Console.WriteLine("A");
    }

    public virtual void Fun()
    {
        Console.WriteLine("A.Fun()");
    }
}

public class B : A
{
    public B()
    {
        Console.WriteLine("B");
    }

    public new void Fun()
    {
        Console.WriteLine("B.Fun()");
    }
}

public class Program
{
    public static void Main()
    {
        A a = new B();
        a.Fun();
    }
}

The above code defines two classes A and B. A is an abstract class with a constructor that prints "A" and a virtual method Fun() that prints "A.Fun()". Class B inherits from A and has its own constructor that prints "B", and a new method Fun() that overrides the Fun() method in A and prints "B.Fun()". In the Main() method, an object of B is created and assigned to a variable of type A, and then the Fun() method is called on that object, which will invoke the Fun() method of class B (since it overrides the method in class A)

public abstract class Apublic AConsoleWriteLineA;public virtual void FunConsoleWriteLineAFunpublic class BApublic BConsoleWriteLineB;public new void FunConsoleWriteLineBFun;public static void MainA a

原文地址: http://www.cveoy.top/t/topic/iXGr 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录