Java程序员的问题 [英] Question of a Java programmer

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

问题描述

你好,


我用C#编程相对较新。


我想设计一个只能用的课程在同一名称空间中使用。


所以我声明这个类是受保护的,但是编译器给了我

以下错误:

错误1命名空间元素不能显式声明为私有,

受保护,或受保护的内部[...]


我如何触及我的目标是什么?

你什么时候使用受保护的如果不是仅为

声明一个类在同一命名空间中使用?

问候,


Martin

Hello,

I?′m relatively new in programming with C#.

I want to design a class with can only be used in the same namespace.

So I declared this class as protected, but the compiler gives me the
following error:

Error 1 Namespace elements cannot be explicitly declared as private,
protected, or protected internal [...]

How can I reach my aim?
When do you use "protected" if not to declare a class only for
the use in the same namespace?
Regards,

Martin

推荐答案

Martin,


你不能在C#中严格执行此操作。但是,internal关键字标记为同一程序集中的其他小部件可见的
类。一个程序集

类似于一个包:它代表一个DLL或EXE。如果您使用的是

VS.NET,那么程序集就是一个项目;它可以包含多个

命名空间。


我知道这样做是为了划分可分发的

包的边缘,类似于它在Java中的样子。也就是说,您将一个

二进制程序集(DLL或EXE)分发给客户,它可能包含一个或多个
的命名空间。其中的内部元素仅对您可见,

并且您公开了您希望图书馆的消费者看到的内容。一个

考试是mscorlib.dll,主要的.NET框架库,其中
包含许多但不是所有的System。*命名空间。

Stephan


Martin P?pping写道:
Martin,

You can''t strictly do this in C#. However, the internal keyword marks a
class as visible to other widgets within the same assembly. An assembly
is similar to a package: it represents one DLL or EXE. If you''re using
VS.NET, an assembly is one project; it can contain more than one
namespace.

I intuit that this was done to delimit the edges of a distributable
package, similar to the way it looks in Java. That is, you distribute a
binary assembly (DLL or EXE) to a customer, and it may contain one or
more namespaces. Internal elements within it are visible only to you,
and you make public what you want consumers of your library to see. An
examble is mscorlib.dll, the main .NET framework library, which
contains many but not all of the System.* namespaces.
Stephan

Martin P?pping wrote:

你好,


我在使用C#进行编程方面相对较新。


我想设计一个只能在同一命名空间中使用的类。


所以我声明这个类是受保护的,但编译器给了我

以下错误:


错误1命名空间元素不能显式声明为私有,

受保护,或受保护内部[...]

我如何实现目标?

您何时使用受保护? ;如果不是仅为

声明一个类在同一命名空间中使用?

问候,


Martin
Hello,

I′m relatively new in programming with C#.

I want to design a class with can only be used in the same namespace.

So I declared this class as protected, but the compiler gives me the
following error:

Error 1 Namespace elements cannot be explicitly declared as private,
protected, or protected internal [...]

How can I reach my aim?
When do you use "protected" if not to declare a class only for
the use in the same namespace?
Regards,

Martin


Martin P ?? pping写道:
Martin P??pping wrote:

你好,


我用C#编程相对较新。


我想设计一个只能在同一命名空间中使用的类。


所以我声明这个类是受保护的,但编译器给了我

以下错误:


错误1命名空间元素不能显式声明为私有,

受保护,或受保护内部[...]

我如何实现目标?

你什么时候使用"保护"如果不是为了在同一名称空间中使用

来声明一个类?
Hello,

I?′m relatively new in programming with C#.

I want to design a class with can only be used in the same namespace.

So I declared this class as protected, but the compiler gives me the
following error:

Error 1 Namespace elements cannot be explicitly declared as private,
protected, or protected internal [...]

How can I reach my aim?
When do you use "protected" if not to declare a class only for
the use in the same namespace?



受保护意味着该项目仅适用于当前类和任何

派生类。没有声明将范围限制为当前的

命名空间。你可以使用内部将范围限制为当前程序集。


但如果它位于名称空间

级别,则不能直接应用于该类。您可以将构造函数设置为internal,这将限制创建类实例的能力仅限于

包含该类的程序集。例如:


命名空间MyNamespace

{

class MyClass

{

internal MyClass(){}

}

}


区别很重要,因为你可以拥有多个命名空间在一个

程序集中,相同的命名空间可以在多个程序集中。内部

限制了大会的范围。

-

汤姆波特菲尔德

Protected means the item is only available to the current class and any
derived classes. There is no declaration that limits scope to the current
namespace. You can use "internal" to limit scope to the current assembly.

But you don''t apply that directly to the class if it is at the namespace
level. You could make the constructor internal which would limit the
ability to create an instance of the class to only the assembly that
contains the class. Ex:

namespace MyNamespace
{
class MyClass
{
internal MyClass(){}
}
}

The distinction is important as you can have more than one namespace in an
assembly, and the same namespace can be in multiple assemblies. Internal
limits the scope to the assembly.
--
Tom Porterfield


ss*****@gmail.com schrieb:
ss*****@gmail.com schrieb:

你不能在C#中严格执行此操作。但是,internal关键字标记为同一程序集中的其他小部件可见的
类。一个程序集

类似于一个包:它代表一个DLL或EXE。如果您使用的是

VS.NET,那么程序集就是一个项目;它可以包含多个
名称空间。
You can''t strictly do this in C#. However, the internal keyword marks a
class as visible to other widgets within the same assembly. An assembly
is similar to a package: it represents one DLL or EXE. If you''re using
VS.NET, an assembly is one project; it can contain more than one
namespace.



感谢您的回答。现在听起来很清楚。


所以我不能让一个类只能以另一种方式在一个命名空间中访问?

问候,


Martin

Thanks for your answer. It sounds clear now.

So I cannot make a class only accessible in one namespace in another way?
Regards,

Martin


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

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