反对在接口上声明受保护访问成员的论点是什么?例如,这是无效的:
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) 我有这个测试用例:
struct A{ protected: A(){} };
struct B: A{};
struct C: A{ C(){} };
struct D: A{ D() = default; };
int main(){
(void)B{};
(void)C{};
(void)D{};
}
Run Code Online (Sandbox Code Playgroud)
gcc和clang都用C++ 11和C++ 14模式编译它.两者都在C++ 17模式下失败:
$ clang++ -std=c++17 main.cpp
main.cpp:7:10: error: base class 'A' has protected default constructor
(void)B{};
^
main.cpp:1:22: note: declared protected here
struct A{ protected: A(){} };
^
main.cpp:9:10: error: base class 'A' has protected default constructor
(void)D{};
^
main.cpp:1:22: note: declared protected here
struct A{ protected: A(){} };
^
2 …Run Code Online (Sandbox Code Playgroud) 在C++中,我想不出一个我希望从基类继承private/protected的情况:
class Base;
class Derived1 : private Base;
class Derived2 : protected Base;
Run Code Online (Sandbox Code Playgroud)
它真的有用吗?
我来自Java EE世界,但现在我正在开发一个.Net项目.在Java中,当我想测试受保护的方法时,它非常简单,只需让具有相同包名的测试类就足够了.C#有什么类似的东西吗?单元测试受保护的方法有什么好的做法吗?我只发现框架和人们说我应该只测试公共方法.应该可以在没有任何框架的情况下做到这一点......
非常感谢.
我知道,Python中没有'真正的'私有/受保护方法.这种方法并不意味着隐藏任何东西; 我只是想了解Python的作用.
class Parent(object):
def _protected(self):
pass
def __private(self):
pass
class Child(Parent):
def foo(self):
self._protected() # This works
def bar(self):
self.__private() # This doesn't work, I get a AttributeError:
# 'Child' object has no attribute '_Child__private'
Run Code Online (Sandbox Code Playgroud)
那么,这种行为是否意味着,"受保护"方法将被继承,但"私有"根本不会被继承?
或者我错过了什么?
我有一个对象有一些我想要获取和设置的受保护属性.对象看起来像
Fields_Form_Element_Location Object
(
[helper] => formText
[_allowEmpty:protected] => 1
[_autoInsertNotEmptyValidator:protected] => 1
[_belongsTo:protected] =>
[_description:protected] =>
[_disableLoadDefaultDecorators:protected] =>
[_errorMessages:protected] => Array
(
)
[_errors:protected] => Array
(
)
[_isErrorForced:protected] =>
[_label:protected] => Current City
[_value:protected] => 93399
[class] => field_container field_19 option_1 parent_1
)
Run Code Online (Sandbox Code Playgroud)
我想获得value该对象的属性.当我尝试$obj->_value或$obj->value它产生错误.我搜索并找到了使用的解决方案PHP Reflection Class.它在我的本地工作但在服务器PHP版本是5.2.17因此我不能在那里使用此功能.那么任何解决方案如何获得这样的属性?
我们都知道protected从基类指定的成员只能从派生类自己的实例访问.这是标准的一个特性,这已在Stack Overflow上多次讨论:
但似乎有可能用成员指针来解决这个限制,因为用户chtz 向我展示:
struct Base { protected: int value; };
struct Derived : Base
{
void f(Base const& other)
{
//int n = other.value; // error: 'int Base::value' is protected within this context
int n = other.*(&Derived::value); // ok??? why?
(void) n;
}
};
Run Code Online (Sandbox Code Playgroud)
为什么这可能,它是一个想要的功能或实施中的某个地方或标准的措辞?
从评论中出现了另一个问题:如果Derived::f用实际调用Base,是不确定的行为?
c++ protected access-specifier member-pointers language-lawyer
我应该为私有或受保护的方法编写JavaDoc 吗?私有变量怎么样?
我在Java书上看到了类示例,私有变量是JavaDoc.所以我无法理解JavaDoc 私有(或受保护)方法是否是一个好习惯.
关于继承的访问修饰符的含义我很困惑.是什么涉及继承之间的区别private,protected以及public关键字?
在Java中,具有"受保护"修饰符的成员不仅可以由同一个类和子类访问,还可以由同一个包中的每个人访问?
我想知道语言设计的原因,而不是实际的应用程序(例如,测试)