C# 가상 메서드 재로드 new 및 virtual

3562 단어 virtual
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace OverrideTest

{

    class A

    {

        private String username;

        public void method1()

        {

            Console.WriteLine("A.method1");

        }

        //

        public virtual void method2()

        {

            Console.WriteLine("A.method2");

        }

    }

    class B : A

    {

        //          ,            ,                 。

        public new void method1()

        {

            Console.WriteLine("B.method1");

        }

        //          ,             ,                 。

        public override void method2()

        {

            Console.WriteLine("B.method2");

        }

        



    }



    class Test {

        public static void Main() {

            B b = new B();

            A a = b;

            a.method1();

            a.method2();

            b.method1();

            b.method2();

            Console.Read();

        }

    }

    

}

결과 출력:
A.method1
B.method2
B.method1
B.method2

좋은 웹페이지 즐겨찾기