静态成员 [英] Static Members

查看:51
本文介绍了静态成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友

请检查下面的代码。

一个用C#和另一个用VB .Net

在C#我不能通过

实例变量访问静态属性,但在VB中我可以轻松完成。


错误是


无法访问静态成员''ClassInCS.ABC.MyInt''

带有实例引用;使用类型名称来限定它

而不是


有人可以解释为什么

与语言之间存在这样的差异吗? />
VB的优势是什么?

或者它不适合OOP规则?

问候

Mark。


C#Code


class MyClass {

[STAThread]

static void Main(string [] args){

Console.WriteLine(ABC.MyInt);

ABC a;

Console.WriteLine( a.MyInt); //错误

a =新ABC();

Console.WriteLine(a.MyInt); //错误

}

}

类ABC {

private static int m_MyInt = 1;

public static int MyInt {

get {

return m_MyInt;

}

set {

m_MyInt = value;

}

}

}


VB代码


Sub Main()

Console.WriteLine(XYZ.MyInt)

Dim x As XYZ

Console.WriteLine(x.MyInt)

x =新XYZ()

Console.WriteLine(x.MyInt)

结束子

类XYZ

Public Const MyPConst As Integer = 2

私有共享m_MyInt为Int32 = 1

公共共享属性MyInt()为Int32

获取

返回m_MyInt

结束获取

Set(ByVal Value As Int32)

m_MyInt =价值

结束套件

结束物业

结束班

解决方案

可能会减少混乱。一个静态变量不属于

任何实例,而是属于类。

我认为VB.Net做了一些不同的事情,因为它需要像VB一样

而C#是为.Net Framework设计的?

-

使用M2,Opera的革命性电子邮件客户端: http://www.opera.com/m2/

Mark< ma ** @ discussion.microsoft.com>写道:

请检查下面的代码。
一个在C#和VB中的其他.Net
在C#中,我无法通过
访问静态属性实例变量,但在VB中我可以轻松完成。




为什么你认为*应该*能够通过
$ b访问静态属性$ b一个实例变量?


它使得它看起来像属性是一个实例属性,所以

可能会让读者感到困惑。这是Java,IMO的一个问题 - 并且

它导致很多人编写如下的愚蠢代码:


Thread.currentThread() .sleep(5000);


当他们真正意味着


Thread.sleep(5000);


- 但是第一个版本看起来你可以在

特定线程上调用sleep()来将其发送到睡眠状态。


基本上我将其视为VB.NET中的一个缺陷,而不是C#中的缺陷。


-

Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet

如果回复该团体,请不要给我发邮件


* Morten Wennevik< Mo ********** **@hotmail.com> scripsit:

可能会让它不那么混乱。静态变量不属于任何实例,而是属于类。




在VB.NET中,静态变量属于/ all / instances(''Shared'')。

这在语义上是一个区别。


-

Herfried K 。瓦格纳[MVP]

< http://www.mvps.org/dotnet>


Hello Friends
Please check the code below.
One in C# and other in VB .Net
In C# I am not able to access a static property by an
instance variable, but in VB I can do it easily.

The Error is

Static member ''ClassInCS.ABC.MyInt'' cannot be accessed
with an instance reference; qualify it with a type name
instead

Can anybody explain why there is such a difference between
to language?
Is this advantage to VB??
Or it doesn''t fit in rule of OOP?
Regards
Mark.

C# Code

class MyClass{
[STAThread]
static void Main(string[] args){
Console.WriteLine(ABC.MyInt);
ABC a;
Console.WriteLine(a.MyInt); //ERROR
a = new ABC();
Console.WriteLine(a.MyInt); //ERROR
}
}
class ABC{
private static int m_MyInt = 1;
public static int MyInt{
get{
return m_MyInt;
}
set{
m_MyInt = value;
}
}
}

VB Code

Sub Main()
Console.WriteLine(XYZ.MyInt)
Dim x As XYZ
Console.WriteLine(x.MyInt)
x = New XYZ()
Console.WriteLine(x.MyInt)
End Sub
Class XYZ
Public Const MyPConst As Integer = 2
Private Shared m_MyInt As Int32 = 1
Public Shared Property MyInt() As Int32
Get
Return m_MyInt
End Get
Set(ByVal Value As Int32)
m_MyInt = Value
End Set
End Property
End Class

解决方案

Probably to make it less confusing. A static variable doesn''t belong to
any instance, but rather to the class.
I think VB.Net does some things differently because it needs to be like VB
whereas C# was designed for .Net Framework?
--
Using M2, Opera''s revolutionary e-mail client: http://www.opera.com/m2/


Mark <ma**@discussions.microsoft.com> wrote:

Please check the code below.
One in C# and other in VB .Net
In C# I am not able to access a static property by an
instance variable, but in VB I can do it easily.



Why do you think you *should* be able to access a static property via
an instance variable?

It makes it look like the property is an instance property, so would
possibly confuse the reader. This is one problem with Java, IMO - and
it''s led a lot of people to write silly code like:

Thread.currentThread().sleep(5000);

when they really mean

Thread.sleep(5000);

- but the first version makes it look like you could call sleep() on a
particular thread to send it to sleep.

Basically I view this as a flaw in VB.NET, not in C#.

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


* Morten Wennevik <Mo************@hotmail.com> scripsit:

Probably to make it less confusing. A static variable doesn''t belong
to any instance, but rather to the class.



In VB.NET, a static variable belongs to /all/ instances (''Shared'').
That''s semantically a difference.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>


这篇关于静态成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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