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();
    }

你可能感兴趣的:(C++,c,Class,编译器,output)