c#面向对象-关键字new

new关键字:

(1)创建对象;

(2)隐藏从父类那里继承过来的同名成员(就是子类调用不了父类成员);

下边在子类和父类中写了同样的方法,调用子类实例时,只能调用到子类中的方法,而父类中的方法调用不到,而且此时在编辑器中子类方法报了一个警告:

意思是:如果是故意这样做的,那么需要在子类方法前加上new 关键字。

namespace ConsoleApp6
{
    class Program
    {
        static void Main(string[] args)
        {
            Student stu = new Student();
            stu.test();
        }
    }
    public class Person
    {
        public void test()
        {
        }
    }
    public class Student: Person
    {
        public new void test()
        {

        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_42778001/article/details/108966790