C#-new 和 override 的区别

// override
public   class  A
{
    
public A()
    
{
        Console.WriteLine(
"A constructing");       
        
this.GetYear();
    }

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

}

public   class  B:A
{
    
public B():base()
    
{
        Console.WriteLine(
"B constructing");
        
this.GetYear();
    }

    
public override void GetYear()
    
{
        Console.WriteLine(
"B");
    }

}

public   class  C : B
{
    
public C()
    
{
        Console.WriteLine(
"C constructing");
        
this.GetYear();
    }

    
public override void GetYear()
    
{
        Console.WriteLine(
"C");
    }

}

public   class  MyClass
{
    
public static void Main()
    
{        
        C c 
= new C();
        Console.ReadLine();
    }
    
}

/***************************************************
output:
    A constructing
    C
    B constructing
    C
    C constructing
    C
**************************************************
*/


// new
public   class  A
{
    
public A()
    
{
        Console.WriteLine(
"A constructing");
        
this.GetYear();
    }

    
protected  void GetYear()
    
{
        Console.WriteLine(
"A");
    }

}


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

    
protected void GetYear() //new写不写一样,但不写编译时会给警告
    {
        Console.WriteLine(
"B");
    }

}

public   class  C : B
{
    
public C()
    
{
        Console.WriteLine(
"C constructing");
        
this.GetYear();
    }


    
protected new void  GetYear()
    
{
        Console.WriteLine(
"C");
    }

}

public   class  MyClass
{
    
public static void Main()
    
{        
        C c 
= new C();
        Console.ReadLine();
    }
    
}

/***************************************************
output:
    A constructing
    A
    B constructing
    B
    C constructing
    C
**************************************************
*/

2007-09-08添加
override相当于C++中的重载,需要与virtual关键字配合使用;
new,说实话,不写也无妨,只是编译器会报警告,而它通常出现在子类与父类同名的函数中.

2007-11-3添加
        在第一个例子中,我开始错误的认为,如果此方法被子类重载了,无论是类A的实例还是类B的实例,调用此方法时,一律显示"C",其实不是这样的.应该是哪个类的实例去调用,就显示那个类中该方法的内容.
        而为什么我会错误的分析第一例,还是"this.GetYear();"这句闹的.

public   class  MyClass
{
    
class Father
    
{
        
public virtual void ShowMe()
        
{
            Console.WriteLine(
"I am father.");
        }

        
public void Talk()
        
{
            Console.WriteLine(
"Father said...");
        }

    }

    
class Child : Father
    
{
        
public override void ShowMe()
        
{
            Console.WriteLine(
"I am child.");
        }

        
public new void Talk()
        
{
            Console.WriteLine(
"Child said...");
        }

    }

    
public static void Main()
    
{        
        Father fa 
= new Father();
        fa.ShowMe();
//output : "I am father"
        fa.Talk();//output : "Father said..."
        Child ch = new Child();
        ch.ShowMe();
//output : "I am child"
        ch.Talk();//output : "Child said..."
        Father fa1 = new Child();
        fa1.Talk();
//output : "Father said..."
        fa1.ShowMe();//output : "I am child"
        RL();
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值