公共与共享 [英] public vs. shared

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

问题描述

我仍在努力理清公共和

之间的差异,在提到声明属性或变量时共享。这是我的理解:


共享 - 类似于应用程序的''全局''变量。应用程序中的任何其他代码

都可以访问它。

public - 如果实例化,可以在整个应用程序中共享。


听起来不对劲?看来这些方法对于方法而言比对变量更有用。大多数时候,我想我会使用共享,如果我想要设置一个其他控件可以看到的变量。


另一个问题,'私人共享'是什么意思?这只是在特定的班级中共享但是

吗?


-Darrel

I''m still trying to sort out in my head the differences between public and
shared when referring to declaring properties or variables. This is my
understanding:

shared - akin to a ''global'' variable for the application. Any other code
within the application can access it.
public - can be shared across the application if instatiated.

Does that sound about right? It seems these are more useful for methods
rather than variables. Most of the time, I imagine that I''d use shared if I
want to set a variable that other controls can see.

The other question, is what does ''private shared'' mean? Is that shared but
only with in the particular class?

-Darrel

推荐答案

公共和共享是不可比的,如受保护,私人和公共

和朋友。这些是访问修饰符 - 他们修改谁拥有正确的

来访问它们。公共只是意味着任何代码都可以访问成员

(函数/属性/字段)。


共享更像是一个标志(某些东西是共享的)或者不是,

表示成员(函数/属性/字段)不表现/属于

a类的特定实例。所以你自己的定义相当准确,我只想说清楚公众和共享是不是可比的。有些东西可以是公共共享,私人共享,朋友共享,

受保护共享或只是公共,私人,朋友或受保护(然后

有受保护的朋友,但我们我现在会忽略它。


私有共享意味着只有类本身可以访问该字段,

经常使用私有共享字段用于单身人士:


公共课MyClass

私人共享MyClass实例=无

公共共享功能GetInstance()为MyClass

如果实例什么都没有那么

instance = new MyClass()

结束如果

返回实例()

结束函数


private sub new()

end sub


...

结束类

从上面的代码可以看出MyClass永远不能直接创建

因为构造函数是私人(外部代码不能称之为)。外部代码

也无法访问实例字段,因为它也是私有的。外部

代码可以访问GetInstance,因为它是公开的。 GetInstance检查

以查看私有字段instance是否为什么都不是(它可以访问私人

字段因为它是同一个类),如果是,它会创建实例

(同样,它可以访问私有构造函数)并返回实例

(可能存在竞争条件,但除了这一点之外)。


什么是关于上面的例子很简单,GetInstance被标记为共享。

如果它不是,没有人能够调用它,因为构造函数

是私有的因此无法创建实例。如果没有实例,就无法访​​问

非共享成员。

你可能会发现Paul Vick的伟大VB.Net书很有用:
http://print.google.com/print?id=ejz...6Q910vyvVa9Gwg


Karl

-

我的ASP.Net教程
http://www.openmymind.net/

darrel <无***** @ hotmail.com>在留言中写道

新闻:OA ************** @ TK2MSFTNGP11.phx.gbl ...
Public and Shared aren''t comparable like protected and private and public
and friend are. Those are access modifiers - they modify who has the right
to access them. Public simply means that any code can access the member
(function/property/field).

Shared which is more like a flag (something either is shared or isn''t),
indicates that the member (function/property/field) doesn''t behave/belong to
a specific instance of the class. So your own definitions are pretty
accurate, I simply want to make it clear that public and shared aren''t
comparable. Something can be public shared, private shared, friend shared,
protected shared or simply public, private, friend or protected (then
there''s protected friend, but we''ll ignore that for now).

private shared means that only the class itself can access the field, a
frequent use of a private shared field is for use with singletons:

public class MyClass
private shared MyClass instance = nothing

public shared function GetInstance() as MyClass
if instance is nothing then
instance = new MyClass()
end if
return instance()
end function

private sub new()
end sub

...
end class
from the above code you can see that MyClass can never be created directly
since the constructor is private (outside code can''t call it). Outside code
also can''t access the instance field because it too is private. Outside
code can however access GetInstance because it''s public. GetInstance checks
to see if the private field "instance" is nothing (it can access a private
field because it''s all the same class), if it is, it creates the instance
(again, it can access the private constructor) and returns the instance
(there is a possible race condition, but that''s besides the point).

What''s neat about the above example is that GetInstance is marked shared.
IF it wasn''t, no one would ever be able to call it because the constructor
is private and thus an instance can''t be created. Without an instance, a
non-shared member can''t be accessed.
You might find Paul Vick''s great VB.Net book useful:
http://print.google.com/print?id=ejz...6Q910vyvVa9Gwg

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"darrel" <no*****@hotmail.com> wrote in message
news:OA**************@TK2MSFTNGP11.phx.gbl...
我还在在提到声明属性或变量时,试图理清公众和
之间的差异。这是我的理解:

共享 - 类似于应用程序的全局变量。应用程序中的任何其他代码都可以访问它。
public - 如果实例化,可以在应用程序中共享。

这听起来是对的吗?看来这些对于方法而不是变量更有用。大多数时候,我想我会使用共享if
我想设置一个其他控件可以看到的变量。

另一个问题是'私有共享'是什么'' 意思?这是共享的,但仅限于特定班级吗?

-Darrel
I''m still trying to sort out in my head the differences between public and
shared when referring to declaring properties or variables. This is my
understanding:

shared - akin to a ''global'' variable for the application. Any other code
within the application can access it.
public - can be shared across the application if instatiated.

Does that sound about right? It seems these are more useful for methods
rather than variables. Most of the time, I imagine that I''d use shared if I want to set a variable that other controls can see.

The other question, is what does ''private shared'' mean? Is that shared but
only with in the particular class?

-Darrel



>公共和共享是不可比的,如受保护,私人和公共
> Public and Shared aren''t comparable like protected and private and public
和朋友。这些是访问修饰符 - 他们修改谁拥有
权限来访问它们。公共只是意味着任何代码都可以访问成员
(函数/属性/字段)。


啊。这就是我的困惑所在。那么,''公共和共享''对于方法和类只是



私有共享意味着只有类本身可以访问该字段,经常使用私有共享字段用于单身人士:

公共类MyClass
私有共享MyClass实例=无

公共共享函数GetInstance()as MyClass
如果实例什么都没有那么
instance = new MyClass()
结束如果
返回实例()
结束函数

私有子新的()
结束子

...结束课
and friend are. Those are access modifiers - they modify who has the right to access them. Public simply means that any code can access the member
(function/property/field).
Ah. This is where my confusion lies then. So, ''public and shared'' really are
only for methods and classes?
private shared means that only the class itself can access the field, a
frequent use of a private shared field is for use with singletons:

public class MyClass
private shared MyClass instance = nothing

public shared function GetInstance() as MyClass
if instance is nothing then
instance = new MyClass()
end if
return instance()
end function

private sub new()
end sub

...
end class




啊。现在,我现在这样做,但我只是调暗变量:


class

dim变量为字符串


sub

variable = monkey

end sub

end class


当我这样做时,默认情况下是'变量',公共共享?


概念的高级概述。我已经提到了这些书,并且很多教程

在线深入描述它们在我们头上的详细程度。

这一点。 />

我要查看你建议的那本书。谢谢!


-Darrel



Ah. Now, I do this now, but I only dim the variable:

class
dim variable as string

sub
variable = monkey
end sub
end class

When I do that, is ''variable'' by default, public shared?

Obiously, what I''m struggling with is a high-level overview of these
concepts. The books I have mention these in passing, and a lot of tutorials
online dive into describing them in a level of detail way above my head at
this point.

I''m going to check out that book you suggested. Thanks!

-Darrel


不,公共和共享用于功能,属性和字段(不仅仅是

方法和类......不确定你是怎么从我说的那样得到的。


当你在类范围内声明dim变量为字符串时,它使用

默认访问修饰符(私有)。并且你没有宣布它是共享的b $ b,所以它不是。换句话说:


class

dim变量为字符串

sub

variable = monkey < br $>
end sub

结束班




类相同

私有变量为字符串

sub

变量= monkey

end sub

end class

如果你想分享一些东西,你总是要暗示这么说。如果你想要私人的东西,这是默认的,所以你不必这么说。如果

你想要一些公开的东西,因为私有是默认的,你必须

特别说出来。


Karl


-

我的ASP.Net教程
http://www.openmymind.net/

" darrel" <无***** @ hotmail.com>在消息中写道

新闻:Oy ************** @ TK2MSFTNGP10.phx.gbl ...
no, public and shared are for functions, properties and fields (not just
methods and classes...not sure how you got that from what i said).

when you declare dim variable as string in the class scope, it uses the
default access modifier (which is private). and you didn''t declare it
shared, so it isn''t. in other words:

class
dim variable as string
sub
variable = monkey
end sub
end class

is the same as
class
private variable as string
sub
variable = monkey
end sub
end class
if you want something shared, you always have to implicitly say so. if you
want something private, that''s the default so you don''t have to say so. If
you want something public, since private is the default, you have to
specifically say so.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"darrel" <no*****@hotmail.com> wrote in message
news:Oy**************@TK2MSFTNGP10.phx.gbl...
公共和共享是不可比的,如受保护和私人,公共和朋友是
。这些是访问修饰符 - 他们修改谁拥有正确的
Public and Shared aren''t comparable like protected and private and public and friend are. Those are access modifiers - they modify who has the right
来访问它们。公共只是意味着任何代码都可以访问成员
(函数/属性/字段)。
to access them. Public simply means that any code can access the member
(function/property/field).



啊。这就是我的困惑所在。那么,''公共和共享''真的



Ah. This is where my confusion lies then. So, ''public and shared'' really



仅用于方法和类?


are only for methods and classes?

私有共享意味着只有类本身可以访问该领域,经常使用私人共享字段用于单身人士:

公共类MyClass
私人共享MyClass实例=没有
公共共享函数GetInstance()作为MyClass
如果实例什么都没有那么
instance = new MyClass()
结束如果
返回实例()
结束函数结束子

终结类
啊。现在,我现在这样做,但我只是变暗了:


dim变量为字符串

sub
变量= monkey
结束子
结束课

当我这样做时,默认情况下是'变量',公共分享?

很明显,我正在努力with是对这些概念的高级概述。我已经提到了这些书籍,并且很多
private shared means that only the class itself can access the field, a
frequent use of a private shared field is for use with singletons:

public class MyClass
private shared MyClass instance = nothing

public shared function GetInstance() as MyClass
if instance is nothing then
instance = new MyClass()
end if
return instance()
end function

private sub new()
end sub

...
end class
Ah. Now, I do this now, but I only dim the variable:

class
dim variable as string

sub
variable = monkey
end sub
end class

When I do that, is ''variable'' by default, public shared?

Obiously, what I''m struggling with is a high-level overview of these
concepts. The books I have mention these in passing, and a lot of



教程在网上深入探讨了我们在这一点上的详细程度。
/>
我要查看你建议的那本书。谢谢!

-Darrel


tutorials online dive into describing them in a level of detail way above my head at
this point.

I''m going to check out that book you suggested. Thanks!

-Darrel



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

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