本文介绍了正确的OOP行为或编译器故障?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 从容易解决的角度来看这不是问题。 但是,我对这种行为的正确性非常好。 我正在开发一个MDI应用程序。在应用程序中,我定义了一个从System.Windows.Forms.Form派生的 Toolbox类。我只希望这个类的一个 实例存在,并且可以通过调用 一个名为Show()的类的静态方法来显示它。令我惊讶的是,我的静态 方法正在屏蔽实例(非静态)Show()方法,该类 继承自System.Windows.Forms.Form和编译器警告我使用''new''修饰符声明 。这是正确的行为吗?可以而且应该 静态和非静态成员互相掩盖?在我看来, 因为每个参数都有不同的限定(即ClassName.Method() vrs InstanceVar.Method())编译器可以清楚地解除哪个是寻求寻求并且永远不会出现任何掩盖问题。 以下是Toolbox类的简化类定义 举例说明了这个问题。 公共类工具箱:System.Windows.Forms.Form { private static Toolbox StaticInstance =新工具箱(); 私有工具箱() { //构建工具箱内容的初始代码 //来自配置文件。 } //为什么我这里需要''new''关键字。不是静态的和实例 成员足够 //能够避免名称冲突吗? public static new void Show() { //编译器故障?需要将其转换为表单,否则 编译器认为 //我们正在尝试引用Toolbox的静态Show方法,即 我们目前定义的 //方法! ((表格)StaticInstance)。显示(); } } - Ken BaltrinicThis isn''t a problem from the standpoint that its easy to work around.However, I am very currious as to the correctness of this behavior.I am developing an MDI application. In the application I have defined aToolbox class deriving from System.Windows.Forms.Form. I want only oneinstance of this class to ever exist and for it to be displayable by callinga static method of the class called Show(). To my surprise, my staticmethod is masking the instance (non-static) Show() method which the classinherits from System.Windows.Forms.Form and the compiler warns me to declareit with the ''new'' modifier. Is this correct behavior? Can and shouldstatic and non-static members mask each other? It would seem to me thatbecuase reference to each are qualified differently (i.e. ClassName.Method()vrs InstanceVar.Method()) the compiler can clearly distiguish which is beingsought and that there should never being any masking issues.Below is an abreviated class definition for the Toolbox class thatexemplifies the issue.public class Toolbox : System.Windows.Forms.Form{private static Toolbox StaticInstance = new Toolbox();private Toolbox(){// Init code that builds the contents of the tool box// from a config file.}//Why do I need the ''new'' keyword here. Are not static and instancemembers sufficiently//distinct to be able to avoid name conflicts?public static new void Show(){//Compiler Glitch? Need to cast this to a form, otherwise thecompiler thinks//we are trying to reference a Toolbox''s static Show method, i.e.the//method we are currently defining!((Form)StaticInstance).Show();}}--Ken Baltrinic推荐答案 C#编译器可以,但其他人可能无法做到。例如,在 Java中你可以这样做: Thread.currentThread()。sleep(5000); 使得它看起来像睡眠是一种实例方法,实际上它是静态方法的a静态方法。我想,J#的用户可能会被你的班级弄糊涂。 我不认为这是编译错误 - 来自ECMA规范的 的第10.7.1.2节: < quote> 类或结构中引入的方法使用相同的名称隐藏所有非方法基础 类成员,并使用 相同的签名(方法名称和参数计数,修饰符和类型)隐藏所有基类方法。 < / quote> 没有提到静态方法只隐藏其他静态方法 或一个只隐藏其他实例方法的实例方法。 - Jon Skeet - < sk *** @ pobox.com> http://www.pobox.com/~skeet 如果回复小组,请不要给我发邮件The C# compiler can, but others may not be able to. For instance, inJava you can do:Thread.currentThread().sleep(5000);which makes it look like sleep is an instance method, when in fact it''sa static method. I''d imagine that users of J# could get confused byyour class.I don''t think it''s a compiler error though - from section 10.7.1.2 ofthe ECMA spec:<quote>A method introduced in a class or struct hides all non-method baseclass members with the same name, and all base class methods with thesame signature (method name and parameter count, modifiers, and types).</quote>There''s no mention of a static method only hiding other static methodsor an instance method only hiding other instance methods.--Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeetIf replying to the group, please do not mail me too 这篇关于正确的OOP行为或编译器故障?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的..
09-06 09:35