Difference between revisions of "Documentation/DevGuide/ProUNO/CLI/The Override Problem"
From Apache OpenOffice Wiki
< Documentation | DevGuide
OOoWikiBot (Talk | contribs) m (Robot: Changing Category:Documentation/Developers Guide/Professional UNO) |
m |
||
Line 1: | Line 1: | ||
− | {{Documentation/DevGuide/ProUNOTOC | + | {{Documentation/DevGuide/ProUNOTOC/Zh |
|ProUNO2c=block | |ProUNO2c=block | ||
|CLIBinding=block | |CLIBinding=block | ||
|ClientProg=block | |ClientProg=block | ||
|ShowPrevNext=block | |ShowPrevNext=block | ||
− | |PrevPage=Documentation/DevGuide/ProUNO/CLI/Writing Client Programs | + | |PrevPage=Zh/Documentation/DevGuide/ProUNO/CLI/Writing Client Programs |
− | |NextPage=Documentation/DevGuide/ProUNO/CLI/Important Interfaces and Implementations | + | |NextPage=Zh/Documentation/DevGuide/ProUNO/CLI/Important Interfaces and Implementations |
}} | }} | ||
− | + | [[en:Documentation/DevGuide/ProUNO/CLI/The Override Problem]] | |
− | + | {{DISPLAYTITLE:重载问题}} | |
− | + | 术语 “override problem” 描述的是由于接口方法重载基类的实现,从而不能存取基对象的虚拟函数 | |
+ | 时将会发生的问题。例如,所有 CLI 对象都是由 <code>System.Object</code> 派生的。当接口中方法的签名与 <code>System.Object</code> 的其中一个方法相同时,如果接口方法是虚拟的,则不能存取相应的 <code>System.Object</code> 方法。 | ||
+ | |||
+ | |||
+ | 例如,请考虑接口 <code>XFoo</code> 的以下声明极其实现类: | ||
using namespace System; | using namespace System; | ||
Line 29: | Line 33: | ||
}; | }; | ||
− | + | ||
+ | 如果调用实例的方法 <code>ToString</code>,则将调用接口方法的实现。例如: | ||
int main(void) | int main(void) | ||
Line 41: | Line 46: | ||
} | } | ||
− | |||
− | + | 由于接口方法可能具有与 <code>System.Object</code> 的名称不同的语义,因此可能不是有意如此。 | |
− | + | 解决方案可防止接口方法重载 <code>System.Object</code> 的方法,而无需将接口方法设为非虚拟。CLI 通过 “newslot” 标志提供一种补救措施,可将该标志附加到 IL 代码的方法头中。在使用 “newslot” 表示方法时,CLI 语言可能具有不同的含义。 | |
+ | |||
+ | |||
+ | 以下示例说明用不同语言实现 <code>XFoo</code> 的方法,这样仍然可以调用 <code>Object.ToString</code>。 | ||
//C++ | //C++ | ||
Line 60: | Line 67: | ||
− | {{Documentation/Note| | + | {{Documentation/Note|尽管 XFoo::ToString 是虚拟的,但由于 CLI 方法头包含 final 属性,因此在继承类中无法将其重载。但是,在继承类中,可以再次由 XFoo 派生并提供实现。}} |
− | + | ||
+ | 在 C# 中,提供实现有不同的方法: | ||
// IL contains: newslot final virtual | // IL contains: newslot final virtual | ||
Line 69: | Line 77: | ||
} | } | ||
− | + | ||
+ | 关键字 new 将 newslot 属性插入到 CLI 方法头中:在继承类中,无法重载此实现。 | ||
//IL contains: newslot virtual | //IL contains: newslot virtual | ||
Line 76: | Line 85: | ||
} | } | ||
− | + | ||
+ | 在派生类中,可以重载此方法。 | ||
// Using a qualified method name for the implementation. The virtual | // Using a qualified method name for the implementation. The virtual | ||
Line 85: | Line 95: | ||
} | } | ||
− | + | ||
+ | 在派生类中,无法重载此实现。调用方法前,必须先将实现类的实例转换成 XFoo。 | ||
'VB .NET | 'VB .NET | ||
Line 92: | Line 103: | ||
End Function | End Function | ||
− | + | ||
+ | 在派生类中,无法重载此实现。 | ||
Public Overridable Shadows Function ToString() As String _ | Public Overridable Shadows Function ToString() As String _ | ||
Line 99: | Line 111: | ||
End Function | End Function | ||
− | + | 可以重载此方法。 | |
{{PDL1}} | {{PDL1}} | ||
− | [[Category: | + | [[Category:文档/开发者指南/专业 UNO]] |
Revision as of 05:55, 18 July 2008
术语 “override problem” 描述的是由于接口方法重载基类的实现,从而不能存取基对象的虚拟函数
时将会发生的问题。例如,所有 CLI 对象都是由 System.Object
派生的。当接口中方法的签名与 System.Object
的其中一个方法相同时,如果接口方法是虚拟的,则不能存取相应的 System.Object
方法。
例如,请考虑接口 XFoo
的以下声明极其实现类:
using namespace System; public __gc __interface XFoo { public: virtual String* ToString(); }; public __gc class Foo : public XFoo { public: virtual String * ToString() { return NULL; } };
如果调用实例的方法 ToString
,则将调用接口方法的实现。例如:
int main(void) { Foo * f = new Foo(); Object * o = f; f->ToString(); // calls Foo.ToString o->ToString(); // calls Foo.ToString return 0; }
由于接口方法可能具有与 System.Object
的名称不同的语义,因此可能不是有意如此。
解决方案可防止接口方法重载 System.Object
的方法,而无需将接口方法设为非虚拟。CLI 通过 “newslot” 标志提供一种补救措施,可将该标志附加到 IL 代码的方法头中。在使用 “newslot” 表示方法时,CLI 语言可能具有不同的含义。
以下示例说明用不同语言实现 XFoo
的方法,这样仍然可以调用 Object.ToString
。
//C++ //interface methods should be qualified with the interface they belong to public __gc class A: public XFoo { public: virtual String* XFoo::ToString() { Console::WriteLine("A::foo"); return NULL; } };
在 C# 中,提供实现有不同的方法:
// IL contains: newslot final virtual public new string ToString() { }
关键字 new 将 newslot 属性插入到 CLI 方法头中:在继承类中,无法重载此实现。
//IL contains: newslot virtual public new virtual string ToString() { }
在派生类中,可以重载此方法。
// Using a qualified method name for the implementation. The virtual //modifier is not allowed string XFoo.ToString() { return null; }
在派生类中,无法重载此实现。调用方法前,必须先将实现类的实例转换成 XFoo。
'VB .NET Public Shadows Function ToString() As String Implements XFoo.ToString Console.WriteLine("Foo.toString") End Function
在派生类中,无法重载此实现。
Public Overridable Shadows Function ToString() As String _ Implements XFoo.ToString Console.WriteLine("Foo.toString" End Function
可以重载此方法。
Content on this page is licensed under the Public Documentation License (PDL). |