C# 基础: new 和 overrider 的区别

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            //contact ct = new contact(); //抽象类
            class1 ct1 = new class1(); 
            class2 ct2 = new class2();

            contact ct3 = new class1();
            contact ct4 = new class2();
            ct1.prinf();  
            ct2.prinf();
            ct3.prinf();
            ct4.prinf(); 
            

            //int i = System.Console.Read();
            //string str = System.Console.ReadLine();
            //System.Console.Read();
            
            System.Console.ReadLine();
        }

        abstract public class contact  
        {  
            public virtual void prinf()    //关键字virtual  
            {  
                Console.WriteLine("这是虚方法");  
            }  
        
        }  
        public class class1 : contact  
        {  
            public override void prinf()   //关键字override  
            {  
                Console.WriteLine("这是新的方法");  
            }  
        
        }  
        
        public class class2 : contact  
        {  
            public new void prinf()     //关键字new  
            {  
                Console.WriteLine("这是另一个新的方法");  
            }  
        
        }  

    }
}

运行结果如下:

posted @ 2014-05-28 14:49  慧由心生  阅读(331)  评论(0编辑  收藏  举报