首页 > 代码库 > C# : new 和 overrider 的区别

C# : new 和 overrider 的区别

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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("这是另一个新的方法"); 
            
         
        
 
    }
}<br><br>

运行结果如下: