问题标签 [class-visibility]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
c# - 具有更多库项目的 .NET 类可见性
我MyClass
在库项目 A 中有一个公共类,该项目是使用该类的项目 B 的引用。
项目 B 也是一个库,假设它被项目 C 引用。
是否可以MyClass
仅通过引用项目 B 在项目 C 中使用,以及如何使用?
.net - How do I avoid opening up my classes too much when using external frameworks?
We have a bunch of MVC and WebAPI projects that use dependency injection to access service classes and other dependencies like that from the controllers in both frameworks. The problem I'm having is that I wanted to make all the services specific to each project internal
, but I can't because the controller classes on both frameworks need to be public, which causes compilation errors if I try to receive instances of parameters that are internal types using constructor injection.
This has bitten me more than once. It happens with almost all frameworks that call your code, be it web frameworks like WebAPI, MVC or even WebForms, and with other kinds of projects like tests, where the test classes need to be public also.
My understanding of object orientation in general is that you want to have as closed a scope as possible, so that things only have access to a minimal set of features that they need to work with. I find this a very good approach in general, because it minimizes the influence of other stuff on each class, and leads to clearer and easier to discover code.
My service classes are just that. They were created to be used just by the controllers in the same assembly, and by nobody else. By having to mark them public
, I'm exposing them to external callers for no reason, and I lose a lot of important compile time information by doing so, like for instance FxCop will warn me if there is no one calling a certain method from an internal class, but it can't infer that if the class is marked public, since someone else outside of the project (or even the solution) could be using that type and calling that method.
This extends to interfaces as well obviously. As I said, we are using dependency injection (with Unity) and usually there is a interface for each service class in that scenario. Even if I can mark implementations themselves as internal, needing the interface to be public is still not ideal.
Is there some pattern that would allow me to cleanly define internal classes and interfaces but still work with external frameworks that need my outside facing classes to be public so they can call me? How should I proceed in general to make sure my code is as tightly scoped as possible?
knockout.js - 如何仅在可见时激活组合模型?
我有一个包含组合模型的视图。当我上传页面时,组合组件虽然不可见,但正在被激活。我想要的是单击“Answer”按钮,然后只有这样组合组件才会被激活(意味着启动“composedView”的激活生命周期
这是我的看法:
谢谢!
php - PHP 属性可见性:允许某些文件访问
我知道这有点奇怪,但我正在玩弄一个概念,我不确定执行它的最佳方法。
在 PHP 中,您可以将方法和属性设为公开或私有。在我们的项目中,我想强制我们的 php 控制器在访问方面表现得与前端应用程序相同,这意味着没有业务逻辑和有限的访问。当然,我可以手动执行此操作,但我喜欢能够使用对象隐私来执行它的想法。当然,我可以将方法设置为私有或受保护(可能是),但没有其他类可以与这些方法/属性交互。
我几乎想要的是另一种类型的隐私,其中只有指定的文件/文件夹等。可以与方法/属性交互
在概念上,我基本上希望看到的是:
- 私人的
- 受保护
- 有限- 只有某些文件夹/文件可以与之交互
- 上市
(实际上,在我的确切情况下,我使用的是 Laravel,所以我会在模型中使用它,它基本上是允许访问的命令和其他模型)
我知道这很棘手,而且我以前从未见过它使用过,但我认为可能有一些创造性的解决方案可以让它发挥作用。我知道我实际上无法定义新的访问类型,但也许通过一些技巧我可以让我的模型具有类似的东西?
有没有人听说过类似的事情?其他任何编程语言都有这样的东西吗?
java - 在java中设计包层次结构,可见性约束
我有一个包ts
,包含票务系统的接口。我也有hts
包,其中包含ts
. 我有TicketSystem.class
in ts
,并HTicketSystem.class
从hts
它扩展了第一个。HTicketSystem.class
是hts
包中唯一的公共类。中的所有其他类hts
都具有默认可见性。hts
包有很多文件,其中包括 java bean。我想将这些 bean 提取到 sub-package hts.beans
。显而易见的问题是可见性,因为根据JLS
:
7.1 包成员
...
包的分层命名结构旨在方便以常规方式组织相关包,但除了禁止包具有与顶级类型相同的简单名称的子包(第 7.6 节)外,其本身没有任何意义在该包中声明。一个名为oliver 的包与另一个名为oliver.twist 的包之间,或者名为evelyn.wood 和evelyn.waugh 的包之间没有特殊的访问关系。
1)如果我beanA.class
以默认可见性离开,hts
班级将看不到它。
2) 如果我更改beanA.class
为公共可见性,它将从外部可见。
非常感谢任何建议/最佳实践。
java - 修饰符的非法组合:public 和 private
我对以下代码有疑问...
还有更多,但我的错误出现在我的主要构造函数中。它给了我在标题中输入的错误。如您所见,我的类和构造函数都是公共的。这不应该引起任何问题,但似乎正在这样做。
php - 访问另一个类的私有变量的最有效方法?
有很多方法可以让你访问另一个类的私有变量,但是最有效的方法是什么?
例如:
我有这门课,其中有一些细节:
在另一个类中,我需要访问它们,例如(尽管这显然不起作用,因为变量不在此类的范围内):
这样的函数在访问类使用的“某物”类中是否足够好?:
php - PHP:类变量可见性
我只是一个偶尔的 PHP 开发人员。现在,在处理遗留应用程序时,我遇到了以下问题,这似乎很愚蠢。但我无法让 $someString 类变量保持正确的值:
据我所见, doSomething() 是在类上下文中调用的,正如我所展示的那样。
我究竟做错了什么?
编辑: 我查看了代码,我想我找到了导致这种情况的原因。html 输出中嵌入了一个 iframe,它是在一部分生成并稍后调用的。所以 setString() 方法实际上并没有立即调用,我首先想到的是在调用 iframe 代码时。所以这就是为什么它在我需要字符串输出的地方不可用。
我想就像现在的代码一样,除了 getIframe() 方法之外,没有办法获得 $someString 输出。
css - 哪个是视觉隐藏事物的更好方法,clip: rect(0, 0, 0, 0) 或 visibility: hidden?
最近,我看到了一些隐藏东西的技巧。我很好奇 和 之间有什么区别clip: rect(0, 0, 0, 0);
,visibility: hidden;
而两者都可以隐藏东西并同时保持空间。
clip: rect(0, 0, 0, 0);
另外,使用和之间有什么区别clip: rect(1px, 1px, 1px, 1px);
吗?
php - PHP 中的可见性?继承时`$this`的上下文?
我是 php 新手,我正在阅读文档以了解可见性。我对文档中的这个例子有点困惑。当调用 to 时$myFoo->test()
,不应该调用Foo
s $this->testPrivate();
。我的意思是不$this
应该Foo
是对象而不是Bar
对象?. 据我所知(我在这里可能错了)Foo
将有一种自己的test()
方法,该方法继承自Bar
并且调用$myFoo->test()
将调用 '$this->testPrivate' ,其中$this
应该是Foo
s object myFoo
。那么它是如何调用Bar
的testPrivate
方法呢?