正确的OOP行为或编译器故障? [英] Correct OOP Behavior or Compiler Glitch?

查看:61
本文介绍了正确的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 Baltrinic

This 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 a
Toolbox class deriving from System.Windows.Forms.Form. I want only one
instance of this class to ever exist and for it to be displayable by calling
a static method of the class called Show(). To my surprise, my static
method is masking the instance (non-static) Show() method which the class
inherits from System.Windows.Forms.Form and the compiler warns me to declare
it with the ''new'' modifier. Is this correct behavior? Can and should
static and non-static members mask each other? It would seem to me that
becuase reference to each are qualified differently (i.e. ClassName.Method()
vrs InstanceVar.Method()) the compiler can clearly distiguish which is being
sought and that there should never being any masking issues.

Below is an abreviated class definition for the Toolbox class that
exemplifies 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 instance
members sufficiently
//distinct to be able to avoid name conflicts?
public static new void Show()
{
//Compiler Glitch? Need to cast this to a form, otherwise the
compiler 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

推荐答案

Kenneth Baltrinic< ne * *********@baltrinic.com>写道:
Kenneth Baltrinic <ne**********@baltrinic.com> wrote:
从容易解决的角度来看这不是问题。
然而,我对这种行为的正确性非常苛刻。
一个名为Show()的类的静态方法来显示它。令我惊讶的是,我的静态
方法正在屏蔽实例(非静态)Show()方法,该类
从System.Windows.Forms.Form继承而且编译器警告我声明
它带有''new''修饰符。这是正确的行为吗?可以而且应该静态和非静态成员相互掩盖吗?在我看来,因为每个都有不同的限定(即ClassName.Method()
vrs InstanceVar.Method())编译器可以明确地解决哪些正在寻求和那个应该永远不会出现任何掩盖问题。
This 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 a
Toolbox class deriving from System.Windows.Forms.Form. I want only one
instance of this class to ever exist and for it to be displayable by calling
a static method of the class called Show(). To my surprise, my static
method is masking the instance (non-static) Show() method which the class
inherits from System.Windows.Forms.Form and the compiler warns me to declare
it with the ''new'' modifier. Is this correct behavior? Can and should
static and non-static members mask each other? It would seem to me that
becuase reference to each are qualified differently (i.e. ClassName.Method()
vrs InstanceVar.Method()) the compiler can clearly distiguish which is being
sought and that there should never being any masking issues.




C#编译器可以,但其他人可能无法做到。例如,在

Java中你可以这样做:


Thread.currentThread()。sleep(5000);


使得它看起来像睡眠是一种实例方法,实际上它是静态方法的
a静态方法。我想,J#的用户可能会被你的班级弄糊涂。


我不认为这是编译错误 - 来自ECMA规范的

的第10.7.1.2节:


< quote>

类或结构中引入的方法使用相同的名称隐藏所有非方法基础

类成员,并使用

相同的签名(方法名称和参数计数,修饰符和类型)隐藏所有基类方法。

< / quote>


没有提到静态方法只隐藏其他静态方法

或一个只隐藏其他实例方法的实例方法。


-

Jon Skeet - < sk *** @ pobox.com>
< a rel =nofollowhref =http://www.pobox.com/~skeettarget =_ blank> http://www.pobox.com/~skeet

如果回复小组,请不要给我发邮件



The C# compiler can, but others may not be able to. For instance, in
Java you can do:

Thread.currentThread().sleep(5000);

which makes it look like sleep is an instance method, when in fact it''s
a static method. I''d imagine that users of J# could get confused by
your class.

I don''t think it''s a compiler error though - from section 10.7.1.2 of
the ECMA spec:

<quote>
A method introduced in a class or struct hides all non-method base
class members with the same name, and all base class methods with the
same signature (method name and parameter count, modifiers, and types).
</quote>

There''s no mention of a static method only hiding other static methods
or an instance method only hiding other instance methods.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Kenneth,


从表面上看,我会想那它也是一个错误,或者更确切地说,它是b / b
不应该给你那个错误。然而,我内心深处有一种啃咬的感觉,告诉我编译器是正确的。


基于你在做什么,而不是暴露一个Show方法,

你真的应该使用单例模式来公开单个

实例。然后,将在该实例上调用Show。


希望这会有所帮助。


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Kenneth Baltrinic" <是ne ********** @ baltrinic.com>在消息中写道

新闻:Od ************** @ tk2msftngp13.phx.gbl ...
Kenneth,

On the surface, I would think that it is a bug as well, or rather, it
shouldn''t give you that error. However, there is a gnawing feeling deep
within me that is telling me that the compiler is correct.

Based on what you are doing though, instead of exposing a Show method,
you really should be using a singleton pattern to expose the single
instance. Then, Show would be called on that instance.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Kenneth Baltrinic" <ne**********@baltrinic.com> wrote in message
news:Od**************@tk2msftngp13.phx.gbl...
这不是从易于解决的角度来看问题。
然而,我对这种行为的正确性非常好。

我正在开发一个MDI应用程序。在应用程序中,我定义了一个派生自System.Windows.Forms.Form的工具箱类。我只希望这个类的一个实例存在,并且可以通过
来调用一个名为Show()的类的静态方法来显示它。令我惊讶的是,我的静态
方法正在屏蔽实例(非静态)Show()方法,该类
从System.Windows.Forms.Form继承,编译器警告我
用''new''修饰符声明它。这是正确的行为吗?可以而且应该静态和非静态成员相互掩盖吗?在我看来,因为每个都有不同的限定条件(即
ClassName.Method()vrs InstanceVar.Method())编译器可以清楚地区分哪个是
正在寻找和应该永远不会出现任何掩盖问题。

下面是Toolbox类的一个简化的类定义,它说明了这个问题。

公共类工具箱:System.Windows .Forms.Form
{private static Toolbox StaticInstance = new Toolbox();

私有工具箱()
//构建的初始化代码工具箱的内容
//来自配置文件。


//为什么我在这里需要''new''关键字。是不是静态和实例
成员足够
//能够避免名称冲突?
public static new void显示()
//编译器故障?需要将其转换为表单,否则
编译器会认为
//我们正在尝试引用Toolbox的静态Show方法,

//方法我们目前正在定义!
((Form)StaticInstance).Show();
}


- Ken Baltrinic
This 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 a
Toolbox class deriving from System.Windows.Forms.Form. I want only one
instance of this class to ever exist and for it to be displayable by calling a static method of the class called Show(). To my surprise, my static
method is masking the instance (non-static) Show() method which the class
inherits from System.Windows.Forms.Form and the compiler warns me to declare it with the ''new'' modifier. Is this correct behavior? Can and should
static and non-static members mask each other? It would seem to me that
becuase reference to each are qualified differently (i.e. ClassName.Method() vrs InstanceVar.Method()) the compiler can clearly distiguish which is being sought and that there should never being any masking issues.

Below is an abreviated class definition for the Toolbox class that
exemplifies 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 instance
members sufficiently
//distinct to be able to avoid name conflicts?
public static new void Show()
{
//Compiler Glitch? Need to cast this to a form, otherwise the
compiler 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



也许单身模式会是更好的选择?


class MyClass

{

//保存一个类的唯一实例

private static MyClass singleInstance = null;


//私有构造函数。你不能明确地创建一个这个

类的实例

private MyClass()

{

//构造函数逻辑

}


//静态公共methot创建一个且只有一个对象实例

public static MyClass GiveMyClass()

{

//如果没有创建,创建

if(singleInstance == null)

singleInstance = new MyClass ();

//返回唯一的实例

返回singleInstance;

}

}

-

" Zeglarstwo jest koniecznoscia

zycie nia nie jest"

www.saper.infra.pl/


Saper(ek)
U?ytkownik" Kenneth Baltrinic" <是ne ********** @ baltrinic.com> napisa3 w

wiadomo?ci news:Od ************** @ tk2msftngp13.phx.gbl ...
maybe a singleton pattern would be a better choice?

class MyClass
{
//holds the only instance of a class
private static MyClass singleInstance=null;

//private constructor. You cant explicitly create an instance of this
class
private MyClass()
{
//constructor logic
}

//static public methot creating one and only one instance of an object
public static MyClass GiveMyClass()
{
//if not created, create
if(singleInstance==null)
singleInstance=new MyClass();
//return the only instance
return singleInstance;
}
}
--
"Zeglarstwo jest koniecznoscia
zycie nia nie jest"

www.saper.infra.pl/

Saper(ek)
U?ytkownik "Kenneth Baltrinic" <ne**********@baltrinic.com> napisa3 w
wiadomo?ci news:Od**************@tk2msftngp13.phx.gbl...
这不是'从易于解决的角度来看问题。
但是,我对这种行为的正确性非常好。

我正在开发一个MDI应用程序。在应用程序中,我定义了一个派生自System.Windows.Forms.Form的工具箱类。我只希望这个类的一个实例存在,并且可以通过
来调用一个名为Show()的类的静态方法来显示它。令我惊讶的是,我的静态
方法正在屏蔽实例(非静态)Show()方法,该类
从System.Windows.Forms.Form继承,编译器警告我
用''new''修饰符声明它。这是正确的行为吗?可以而且应该静态和非静态成员相互掩盖吗?在我看来,因为每个都有不同的限定条件(即
ClassName.Method()vrs InstanceVar.Method())编译器可以清楚地区分哪个是
正在寻找和应该永远不会出现任何掩盖问题。

下面是Toolbox类的一个简化的类定义,它说明了这个问题。

公共类工具箱:System.Windows .Forms.Form
{private static Toolbox StaticInstance = new Toolbox();

私有工具箱()
//构建的初始化代码工具箱的内容
//来自配置文件。


//为什么我在这里需要''new''关键字。是不是静态和实例
成员足够
//能够避免名称冲突?
public static new void显示()
//编译器故障?需要将其转换为表单,否则
编译器会认为
//我们正在尝试引用Toolbox的静态Show方法,

//方法我们目前正在定义!
((Form)StaticInstance).Show();
}


- Ken Baltrinic
This 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 a
Toolbox class deriving from System.Windows.Forms.Form. I want only one
instance of this class to ever exist and for it to be displayable by calling a static method of the class called Show(). To my surprise, my static
method is masking the instance (non-static) Show() method which the class
inherits from System.Windows.Forms.Form and the compiler warns me to declare it with the ''new'' modifier. Is this correct behavior? Can and should
static and non-static members mask each other? It would seem to me that
becuase reference to each are qualified differently (i.e. ClassName.Method() vrs InstanceVar.Method()) the compiler can clearly distiguish which is being sought and that there should never being any masking issues.

Below is an abreviated class definition for the Toolbox class that
exemplifies 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 instance
members sufficiently
//distinct to be able to avoid name conflicts?
public static new void Show()
{
//Compiler Glitch? Need to cast this to a form, otherwise the
compiler 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



这篇关于正确的OOP行为或编译器故障?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆