C#中new, override, virtual的具体用法

作者:袖梨 2022-06-25

c#中new, override, virtual的具体用法

  

class program
    {
        static void main(string[] args)
        {
            testshape();
            console.writeline("testshape   end =============" + environment.newline);
            testderive();
            console.writeline("testderive  end =============" + environment.newline);
            testderive2();
            console.writeline("testderive2 end =============" + environment.newline);
            console.readkey();
        }

        private static void testshape()
        {
            system.collections.generic.list shapes = new system.collections.generic.list();
            shapes.add(new circle());
            shapes.add(new rectangle());
            shapes.add(new triangle());
            shapes.add(new diamond());
            foreach (shape s in shapes)
            {
                s.methodvirtual();
                s.method();
                console.writeline();
            }

        }

        private static void testderive()
        {
            circle circle = new circle();
            rectangle rectangle = new rectangle();
            triangle triangel = new triangle();
            diamond diamond = new diamond();
            circle.methodvirtual();
            circle.method();
            console.writeline();
            rectangle.methodvirtual();
            rectangle.method();
            console.writeline();
            triangel.methodvirtual();
            triangel.method();
            console.writeline();
            diamond.methodvirtual();
            diamond.method();
            console.writeline();
        }

        private static void testderive2()
        {
            circle circle = new circle();
            printshape(circle);
            rectangle rectangle = new rectangle();
            printshape(rectangle);
            triangle triangel = new triangle();
            printshape(triangel);
            diamond diamond = new diamond();
            printshape(diamond);
            ///out put:
            //circle override methodvirtual
            //base method call

            //base methodvirtual call
            //base method call

            //base methodvirtual call
            //base method call

            //base methodvirtual call
            //base method call
        }

        static void printshape(shape sharpe)
        {
            sharpe.methodvirtual();
            sharpe.method();
            console.writeline();
        }
    }

    public class shape
    {
        public virtual void methodvirtual()
        {
            console.writeline("base methodvirtual call");
        }

        public void method()
        {
            console.writeline("base method call");
        }
    }

    ///类描述:override了基类的virtual方法
    ///
    ///第一种使用方法:转型为父类
    ///sharp s = new circle()
    ///s.methodvirtual();
    ///s.method();
    ///因为子类已经override了父类的methodvirtual,所以即使子类转型为了sharp,调用的还是子类的方法
    ///out put:
    ///circle override methodvirtual
    ///base method call
    ///
    ///第二类使用方法:使用子类本身
    ///这很好理解,全部输出的是子类的方法
    ///circle circle = new circle();
    ///circle.methodvirtual();
    ///circle.method();
    ///out put:
    ///circle override methodvirtual
    ///base method call
    class circle : shape
    {
        public override void methodvirtual()
        {
            console.writeline("circle override methodvirtual");
        }
    }

    ///类描述:未做任何处理
    ///
    ///第一种使用方法
    ///sharp s = new rectangle()
    ///s.methodvirtual();
    ///s.method();
    ///out put:
    ///base methodvirtual call
    ///base method call
    ///
    ///第二类使用方法:使用子类本身
    ///这很好理解,全部输出的是子类的方法
    ///rectangle rectangle = new rectangle();
    ///rectangle.methodvirtual();
    ///rectangle.method();
    ///out put:
    ///base methodvirtual call
    ///base method call
    class rectangle : shape
    {

    }

    ///类描述:new了基类的虚方法即非虚方法
    ///
    ///第一种使用方法
    ///sharp s = new triangle()
    ///s.methodvirtual();
    ///s.method();
    ///因为子类已经new了父类的方法,所以s输出的是父类的方法
    ///out put:
    ///base methodvirtual call
    ///base method call
    ///
    ///第二类使用方法:使用子类本身
    ///这很好理解,全部输出的是子类的方法
    ///triangle triangel = new triangle();
    ///triangel.methodvirtual();
    ///triangel.method();
    ///out put:
    ///triangle new methodvirtual
    ///triangle new method
    class triangle : shape
    {
        public new void methodvirtual()
        {
            console.writeline("triangle new methodvirtual");
        }

        public new void method()
        {
            console.writeline("triangle new method");
        }
    }

    ///类描述:创建了基类方法相同的方法,未new及override
    ///编译器会做提示“隐藏继承”,并有如存在 new 关键字一样执行操作
    ///
    ///第一种使用方法
    ///sharp s = new diamond()
    ///s.methodvirtual();
    ///s.method();
    ///因为默认new的效果,所以输出和显式new修饰的一样
    ///out put:
    ///base methodvirtual call
    ///base method call
    ///
    ///第二类使用方法:使用子类本身
    ///这很好理解,全部输出的是子类的方法
    ///diamond diamond = new diamond();
    ///diamond.methodvirtual();
    ///diamond.method();
    ///out put:
    ///diamond default methodvirtual
    ///diamond default method
    class diamond : shape
    {
        public void methodvirtual()
        {
            console.writeline("diamond default methodvirtual");
        }

        public void method()
        {
            console.writeline("diamond default method");
        }
    }

上面代码的结果,如果基类使用接口代替,也是一样的效果。

主要注意看override或new了基类的方法后,调用方将子类对象转型为父类后的输出会有

相关文章

精彩推荐