反对在接口上声明受保护访问成员的论点是什么?例如,这是无效的:
public interface IOrange
{
public OrangePeel Peel { get; }
protected OrangePips Seeds { get; }
}
Run Code Online (Sandbox Code Playgroud)
在此示例中,接口IOrange将保证实现者至少OrangePips向其继承者提供实例.如果实现者想要,他们可以扩大范围public:
public class NavelOrange : IOrange
{
public OrangePeel Peel { get { return new OrangePeel(); } }
protected OrangePips Seeds { get { return null; } }
}
public class ValenciaOrange : IOrange
{
public OrangePeel Peel { get { return new OrangePeel(); } }
public OrangePips Seeds { get { return new OrangePips(6); …Run Code Online (Sandbox Code Playgroud) 在VB6/VBA中,您可以在特定Sub或Function方法之外声明模块级变量.我用Private与Public前内部模块和理解他们,像这样:
Public - 模块内的所有代码和模块外的所有代码都可见,实际上使其成为全局代码.Private - 仅对模块内的代码可见.我注意到你可以使用Dim和Global作为模块变量的修饰符.是Dim与Global从不同Private,并Public分别作为模块化领域访问修饰符使用时?如果是这样,他们有什么不同?
我从swift开始并打开一个使用swift2从xcode 8 beta创建的项目,private修改器改为fileprivate.这个关键字是什么意思?和它有什么不同private?
我有一些协议
LoginStrategy
public protocol LoginStrategy {
func login(_ viewController: UIViewController)
func getUserInfo(withCompletionHandler completionHandler: @escaping (_ userInfo: [String: Any]?) -> ())
func createLoginButton(_ frame: CGRect, withCompletionHandler completionHandler: @escaping (_ loginButton: UIView) -> ())
func getUserId() -> String
}
Run Code Online (Sandbox Code Playgroud)
和两个类:
LoginProvider
public class LoginProvider {
public let strategy: LoginStrategy
public func login(_ viewController: UIViewController) {
return self.strategy.login(viewController)
}
public func getUserInfo(withCompletionHandler completionHandler: @escaping (_ userInfo: [String: Any]?) -> ()) {
return self.strategy.getUserInfo(withCompletionHandler: completionHandler)
}
public func createLoginButton(_ frame: CGRect, withCompletionHandler completionHandler: @escaping …Run Code Online (Sandbox Code Playgroud) C#7.2引入了private protected modifier.
我总是保护对具有属性的字段的访问,允许通过Get/Set方法进行访问,因为我通常不希望我的对象的内部状态被我自己的类以外的任何东西修改.
我试图理解为什么C#语言团队添加了这个功能.经过广泛搜索谷歌,阅读和观看"什么是新的"媒体(我看过Mads Torgerson的新闻稿,详细信息和视频),我仍然没有更聪明.
对我来说,这似乎允许开发人员打破Liskov替换原则,但这可能是因为我不明白为什么现在存在此功能.
我理解它是如何使用的,而不是为什么 - 请有人提供一个真实的使用示例而不是MSDN文档中的人为例子吗?
为什么Java指定覆盖方法的访问说明符可以允许比重写方法更多但不是更少的访问?例如,超类中的受保护实例方法可以在子类中公开,但不是私有的.
好的,所以这可能是一个有点愚蠢的问题,肯定有明显的答案,但我很好奇,如果我错过了任何细微之处.
public在internal类中internal声明的成员与在类中声明的成员之间的可见性/可用性方面是否有任何区别internal?
即之间
internal class Foo
{
public void Bar()
{
}
}
Run Code Online (Sandbox Code Playgroud)
和
internal class Foo
{
internal void Bar()
{
}
}
Run Code Online (Sandbox Code Playgroud)
如果您将方法声明为publicand virtual,然后在派生类中对其进行覆盖,则public使用此修饰符的原因很明显.然而,这是唯一的情况......我错过了别的什么吗?
哎呀,暂时不在那个套接字库上工作.我正在尝试用C++教育自己一点.
对于类,有没有办法让变量只读公开,但私有访问时读取+写入?例如:
class myClass {
private:
int x; // this could be any type, hypothetically
public:
void f() {
x = 10; // this is OK
}
}
int main() {
myClass temp;
// I want this, but with private: it's not allowed
cout << temp.x << endl;
// this is what I want:
// this to be allowed
temp.f(); // this sets x...
// this to be allowed
int myint = temp.x;
// this NOT to be allowed
temp.x …Run Code Online (Sandbox Code Playgroud) Scala中有两种类型的修饰符:final和sealed
它们之间有什么区别?什么时候应该使用另一个?