标签: access-modifiers

MVVM:VM对象是应该直接暴露M对象,还是只通过getter委托给M的getter?

最好的解释方法是举例如下:

这是模型

public class Person 
{
    public int age;
    public string name;
}
Run Code Online (Sandbox Code Playgroud)

这是视图模型

public class PersonVM
{    
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:
vm应该将人员暴露给数据模板还是用他自己的属性封装模型属性?

c# getter design-patterns access-modifiers mvvm

32
推荐指数
4
解决办法
7137
查看次数

为什么内部保护不比内部更严格?

我想创建一个内部自动属性:

internal bool IP { get; protected internal set; }
Run Code Online (Sandbox Code Playgroud)

我认为可以制作setter protectedprotected internal- 但我总是得到错误的accessibility修饰符必须比属性更具限制性.不是这样的吗?Private这里没有帮助我.

编辑:
问题是:如何使用内部getter和受保护的setter实现自动属性?

c# access-modifiers

31
推荐指数
4
解决办法
6374
查看次数

当我的子类位于不同的包中时,为什么我的子类不能访问其超类的受保护变量?

我有一个抽象类,relation封装database.relation和它的一个子类,Join在包database.operations.relation有一个名为的受保护成员mStructure.

Join:

public Join(final Relation relLeft, final Relation relRight) {
        super();
        mRelLeft = relLeft;
        mRelRight = relRight;
        mStructure = new LinkedList<Header>();
        this.copyStructure(mRelLeft.mStructure);

        for (final Header header :mRelRight.mStructure) {
        if (!mStructure.contains(header)) {
            mStructure.add(header);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在线上

this.copyStructure(mRelLeft.mStructure);
Run Code Online (Sandbox Code Playgroud)

for (final Header header : mRelRight.mStructure) {
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

字段Relation.mStructure不可见

如果我把两个类都放在同一个包中,这样就可以了.有谁能解释这个问题?

java packages protected access-modifiers

31
推荐指数
2
解决办法
3万
查看次数

为什么我可以通过指向派生对象的基类指针访问派生的私有成员函数?

#include<iostream>

using namespace std;
class base
{
public:
    virtual void add() {
        cout << "hi";
    }
};

class derived : public base
{
private:
    void add() {
        cout << "bye";
    }
};

int main()
{
    base *ptr;
    ptr = new derived;
    ptr->add();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出是 bye

我对这是如何实现没有问题.我理解你使用vtable和派生的vtable包含新的add()函数的地址.但是当我尝试在类外部访问它时,add()是私有的,不应该编译器生成错误吗?不知怎的,这似乎不对.

c++ polymorphism inheritance access-modifiers

30
推荐指数
3
解决办法
2299
查看次数

C#中私有类的概念

除了Inner类之外,C#中是否可以存在私有类?

.net c# access-modifiers

29
推荐指数
3
解决办法
3万
查看次数

如何在C#中保护属性和内部?

这是我缩短的抽象类:

abstract class Report {

    protected internal abstract string[] Headers { get; protected set; }
}
Run Code Online (Sandbox Code Playgroud)

这是一个派生类:

class OnlineStatusReport : Report {

    static string[] headers = new string[] {
        "Time",
        "Message"
    }

    protected internal override string[] Headers {
        get { return headers; }
        protected set { headers = value; }
    }

    internal OnlineStatusReport() {
        Headers = headers;
    }
}
Run Code Online (Sandbox Code Playgroud)

我的想法是,我希望能够Report.Headers从程序集中的任何地方调用,但只允许它由派生类设置.我尝试Headers只做内部,但受保护不算比内部更严格.有没有办法使Headers内部及其set访问器受到保护和内部?

我觉得我非常滥用访问修饰符,所以任何设计帮助都会非常感激.

c# access-modifiers

28
推荐指数
5
解决办法
9409
查看次数

受保护的修饰符是什么意思?

我正在阅读"Java编程语言"第3版.

在第3.5章中,它protected使用以下单词说明了修饰符:

更确切地说,除了可以在类本身内访问以及在同一个包中编码之外,还可以通过对象引用从类访问受保护的成员,这些引用至少与类的类型相同,类的类型的引用或其中一个亚型.

两个方面让我感到困惑:

1.受保护的成员可以通过同一个包中的代码访问吗?之前我所知道的是受保护的成员只能由子类访问...

2.我不明白这是什么a protected member can also be accessed from ...意思,任何人都可以向我解释好吗?

java protected access-modifiers

27
推荐指数
3
解决办法
4万
查看次数

python中的受保护方法

可能重复:
在python子类中将方法设为
私有Python中的私有变量和方法

如何在受保护的python类中定义一个方法,只有子类可以看到它?

这是我的代码:

class BaseType(Model):
    def __init__(self):
        Model.__init__(self, self.__defaults())


    def __defaults(self):
        return {'name': {},
                'readonly': {},
                'constraints': {'value': UniqueMap()},
                'cType': {}
        }


    cType = property(lambda self: self.getAttribute("cType"), lambda self, data:              self.setAttribute('cType', data))
    name = property(lambda self: self.getAttribute("name"), lambda self, data: self.setAttribute('name', data))
    readonly = property(lambda self: self.getAttribute("readonly"),
                        lambda self, data: self.setAttribute('readonly', data))

    constraints = property(lambda self: self.getAttribute("constraints"))

    def getJsCode(self):
        pass

    def getCsCode(self):
        pass


    def generateCsCode(self, template=None, constraintMap=None, **kwargs):
        if not template:
            template = self.csTemplate

        if not constraintMap: constraintMap …
Run Code Online (Sandbox Code Playgroud)

python oop access-modifiers

27
推荐指数
2
解决办法
3万
查看次数

为什么我不能在包外使用受保护的构造函数?

为什么我不能在包外面使用受保护的构造函数来获取这段代码:

package code;
public class Example{
    protected Example(){}
    ...
}
Run Code Online (Sandbox Code Playgroud)

Check.java

package test;
public class Check extends Example {
  void m1() {
     Example ex=new Example(); //compilation error
  }
}
Run Code Online (Sandbox Code Playgroud)
  1. 为什么即使我扩展了课程,我也会得到错误?请解释

编辑:

编译错误:

构造函数Example()不可见

java protected access-modifiers

27
推荐指数
2
解决办法
9655
查看次数

在派生类中更改功能访问模式

请考虑以下代码段:

struct Base
{
  virtual ~Base() {}

  virtual void Foo() const = 0; // Public
};

class Child : public Base
{
  virtual void Foo() const {} // Private
};

int main()
{
  Child child;

  child.Foo(); // Won't work. Foo is private in this context.

  static_cast<Base&> (child).Foo(); // Okay. Foo is public in this context.
}
Run Code Online (Sandbox Code Playgroud)

这是合法的C++吗?"这"正在改变派生类中的虚函数访问模式.

c++ inheritance access-modifiers

26
推荐指数
3
解决办法
2万
查看次数