你不该知道的.NET 第零回: 不继承Object的后果

本文探讨了.NET框架中接口的概念,解释了接口与Object类的关系,并通过示例代码展示了接口如何实现及其实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

作者:不及格的程序员-八神

 

事起有音:

相关链接

http://www.cnblogs.com/allenlooplee/archive/2007/01/22/627386.html System.Object 是 .NET 中所有类型的根吗

http://www.cnblogs.com/anytao/archive/2009/03/05/must_net_27.html interface到底继承于object吗

看看吧,后果很严重,知道又能怎么样呢, 谁知道你的类父类是什么,就连CLR都不知道,你还指望谁?

接口根继承Object有直接关系吗? 没有,接口:契约而,那它方法从哪来-----因为引用类型的方法表中有定义(CLR自动为你生成).

 

源码:

 

 1  
 3  using  System;
 4 
 5  namespace  Test
 6   {
 7     public   interface  ITest
 8    {
 9      void  No();
10    }
11 
12    public   class  Obj1
13    {
14    }
15 
16    public   class  Obj2 : ITest
17    {
18      public   void  No(){}
19    }
20 
21  }

 

 

中间码:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
 1 
 2 
 3 
 4  //  Microsoft (R) .NET Framework IL Disassembler.  Version 3.5.30729.1
 5 
 6  
 7 
 8 
 9  // Metadata version: v2.0.50727
10  .assembly extern mscorlib
11  {
12    .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
13    .ver 2:0:0:0
14  }
15  .assembly '1'
16  {
17    .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 ) 
18    .custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78   // .T..WrapNonEx
19                                                                                                               63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 )       // ceptionThrows.
20    .hash algorithm 0x00008004
21    .ver 0:0:0:0
22  }
23  .module '1.dll'
24  // MVID: {176435F4-DB87-437C-A621-B1370EF3AB88}
25  .imagebase 0x00400000
26  .file alignment 0x00000200
27  .stackreserve 0x00100000
28  .subsystem 0x0003       // WINDOWS_CUI
29  .corflags 0x00000001    //  ILONLY
30  // Image base: 0x03360000
31 
32 
33  // =============== CLASS MEMBERS DECLARATION ===================
34 
35 .class interface public abstract auto ansi Test.ITest
36  {
37    .method public hidebysig newslot abstract virtual 
38            instance void  No() cil managed
39    {
40    } // end of method ITest::No
41 
42 // end of class Test.ITest
43 
44 .class public auto ansi beforefieldinit Test.Obj1
45         
46  {
47    .method public hidebysig specialname rtspecialname 
48            instance void  .ctor() cil managed
49    {
50      // 代码大小       7 (0x7)
51      .maxstack  8
52      IL_0000:  nop
53      IL_0001:  ret
54    } // end of method Obj1::.ctor
55 
56 // end of class Test.Obj1
57 
58 .class public auto ansi beforefieldinit Test.Obj2
59         
60         implements Test.ITest
61  {
62    .method public hidebysig newslot virtual final 
63            instance void  No() cil managed
64    {
65      // 代码大小       2 (0x2)
66      .maxstack  8
67      IL_0000:  nop
68      IL_0001:  ret
69    } // end of method Obj2::No
70 
71   .method public hidebysig specialname rtspecialname 
72            instance void  .ctor() cil managed
73    {
74      // 代码大小       7 (0x7)
75      .maxstack  8
76      IL_0000:  nop
77      IL_0001:  ret
78    } // end of method Obj2::.ctor
79 
80 // end of class Test.Obj2
81 
82 
83  // =============================================================
84 
85 // *********** 反汇编完成 ***********************
86  //

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值