C#:abstarct 和 virtual关键字的区别分析,new 与 override关键字区别分析

1.首先


absratct 和 virtual 都是与继承概念有关系。

2.区别


① virtual 关键字用于在父类中修饰方法,该方法在父类中可提供实现,并且子类中使用此方法有两种方式。

方式1:

在父类中定义了virtual方法,在子类中没有用override修饰该方法(不写默认是new修饰或明显的用new修饰),那么在对(父类声明的子类实例的对象)的调用中,该方法使用的是父类定义的方法。通过强制类型转换,外面还可以调用父类的实现。

代码如下:

using System;
namespace StudyLanguage
{
    class Parent
    {
        public virtual void print()
        {
            Console.WriteLine("基类print()方法的打印");
        }


    }

    class Child : Parent
    {
        public new void print()
        {
            Console.WriteLine("子类print()方法的打印");
        }
    }

    class Test
    {
        static void Main(String[] args)
        {
            Console.WriteLine("子类声明,子类实例的对象:");
            Child child1 = new Child();
            child1.print();
            Console.WriteLine("强制转换后的打印如下:");
            ((Parent)child1).print();

            Console.WriteLine("\n父类声明,子类实例的对象:");
            Parent parent1 = new Child();
            parent1.print();


            Console.Write("输入任意按键结束退出...");
            Console.ReadKey();
        }
    }


}

打印如下:

方式2:

在父类中定义了virtual方法,在子类中使用override重写该方法。那么在对(父类声明的子类实例的对象)的调用中,该方法使用的是子类重写的方法。通过强制类型转换后,外面也不可以调用父类的实现。

代码如下:

using System;
namespace StudyLanguage
{
    class Parent
    {
        public virtual void print()
        {
            Console.WriteLine("基类print()方法的打印");
        }


    }

    class Child : Parent
    {
        public override void print()
        {
            Console.WriteLine("子类print()方法的打印");
        }
    }

    class Test
    {
        static void Main(String[] args)
        {
            Console.WriteLine("子类声明子类实例的对象:");
            Child child1 = new Child();
            child1.print();
            Console.WriteLine("强制转换后的打印如下:");
            ((Parent)child1).print();

            Console.WriteLine("\n父类声明子类实例的对象:");
            Parent parent1 = new Child();
            parent1.print();


            Console.Write("输入任意按键结束退出...");
            Console.ReadKey();
        }
    }


}

打印如下:

为什么 new 和 override 有如此区别?

override是重写,即将父类的方法在子类里直接抹去重新写,故而调用的方法就是子类方法。

new只是将父类的方法在子类里隐藏起来,故而调用的仍旧是父类方法。

② abstract 关键字只能用在抽象类中修饰方法,并且没有具体的实现,子类必须使用override重写方法。

代码如下:

using System;
namespace StudyLanguage
{
    abstract class Parent
    {
        public abstract void print();
    }

    class Child : Parent
    {
        public override void print()
        {
            Console.WriteLine("子类print()方法的打印");
        }
    }

    class Test
    {
        static void Main(String[] args)
        {

            Child child1 = new Child();
            child1.print();
            Console.WriteLine("强制转换后的打印如下:");
            ((Parent)child1).print();
            
            Parent parent1 = new Child();
            parent1.print();


            Console.Write("输入任意按键结束退出...");
            Console.ReadKey();
        }
    }


}

注意:没有 Parent p = new Parent(),因为Parent是抽象类。

3.接口 interface


1.接口只提供方法规约,不提供方法体;
2.接口中的方法不能用关键字(public也不行)修饰;     抽象类中可以有 public修饰方法
3.接口里不能有接口和变量;   抽象类中可以有接口和变量
4.接口里的方法在子类中必须全部实现;
5.接口可以实现多重继承; 类只能单继承,接口可以多继承

示例代码如下:

using System;
namespace StudyLanguage
{
    public interface MyInterFace
    {
        void getName();
    }


    abstract class Parent
    {
        public int id = 1;
        interface Mi
        {
            int testMi();
        }
        public abstract void print();
    }

    class Child : Parent,MyInterFace
    {
        public override void print()
        {
            Console.WriteLine("重写抽象类中的方法的打印");
        }

        public void getId()
        {
            //抽象类中的变量
            Console.WriteLine(id);
        }

        public void getName()
        {
            Console.WriteLine("重写接口中的方法的打印");
        }
        

    }

    class Test
    {
        static void Main(String[] args)
        {

            Child child1 = new Child();
            child1.print();
            child1.getId();
            child1.getName();

            Console.Write("输入任意按键结束退出...");
            Console.ReadKey();
        }
    }


}

打印如下:

猜你喜欢

转载自blog.csdn.net/qq_38261174/article/details/84197875