我有一个抽象类,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不可见
如果我把两个类都放在同一个包中,这样就可以了.有谁能解释这个问题?
我有一个构造函数尝试初始化基类中的字段.编译器抱怨.该字段受到保护,因此派生类应具有访问权限.
//The base class:
class BaseClass
{
public:
BaseClass(std::string);
BaseClass(const BaseClass& orig);
virtual ~BaseClass();
const std::string GetData() const;
void SetData(const std::string& data);
protected:
BaseClass();
std::string m_data;
};
BaseClass::BaseClass(const std::string data) : m_data(data) { }
BaseClass::BaseClass() { }
BaseClass::BaseClass(const BaseClass& orig) { }
BaseClass::~BaseClass() { }
void BaseClass::SetData(const std::string& data)
{
m_data = data;
}
const std::string BaseClass::GetData() const
{
return m_data;
}
//The derived class:
class DerivedClass : public BaseClass
{
public:
DerivedClass(std::string data);
DerivedClass(const DerivedClass& orig);
virtual ~DerivedClass();
private: …Run Code Online (Sandbox Code Playgroud) 关于受保护构造函数的一个问题 我了解到受保护的构造函数可以在派生类中使用.但是,我发现下面的代码有错误.为什么会这样?
class A
{
protected:
A(){}
};
class B: public A {
public:
B() {
A* f=new A(); // Why it is not working here
}
};
Run Code Online (Sandbox Code Playgroud) 我正在阅读"Java编程语言"第3版.
在第3.5章中,它protected使用以下单词说明了修饰符:
更确切地说,除了可以在类本身内访问以及在同一个包中编码之外,还可以通过对象引用从类访问受保护的成员,这些引用至少与类的类型相同,类的类型的引用或其中一个亚型.
这两个方面让我感到困惑:
1.受保护的成员可以通过同一个包中的代码访问吗?之前我所知道的是受保护的成员只能由子类访问...
2.我不明白这是什么a protected member can also be accessed from ...意思,任何人都可以向我解释好吗?
为什么我不能在包外面使用受保护的构造函数来获取这段代码:
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)
编辑:
编译错误:
构造函数Example()不可见
我有一个Java类,我已经移植到Scala,但仍然有一些Java子类实现抽象功能.
最初的Java有
public abstract class Base {
...
protected abstract Foo getFoo();
}
Run Code Online (Sandbox Code Playgroud)
这被延长了
public class Fred extends Base {
...
protected Foo getFoo() {
return foo;
}
}
Run Code Online (Sandbox Code Playgroud)
现在我将Base移植到Scala
class Base {
...
protected def getFoo(): Foo
}
Run Code Online (Sandbox Code Playgroud)
编译器抱怨说
无法降低继承方法的可见性......
现在我明白了,因为它的规则略有不同,Scala倾向于在编译器中强制实现可见性而不是字节码,因此getFoo在javap类中被标记为public.但
protected[packagename] def getFoo()
Run Code Online (Sandbox Code Playgroud)
仍然没有生成pukka(Java)保护方法,虽然我认为可见性应该是相同的.
有没有办法说服Scala编译器发出一个Java会认为受保护的方法?
是否可以定义仅对它们所定义的类可用的属性,以及该类的子类?
换句话说,有没有办法定义受保护的属性?
这段代码适用于clang,但g ++说:
错误:'A :: A()'受到保护
class A
{
protected:
A() {}
};
class B : public A
{
static A f() { return A(); } // GCC claims this is an error
};
Run Code Online (Sandbox Code Playgroud)
哪个编译器是对的?
protected ×10
c++ ×4
java ×4
constructor ×3
class ×1
field ×1
inheritance ×1
objective-c ×1
oop ×1
packages ×1
private ×1
properties ×1
reflection ×1
scala ×1
static ×1
visibility ×1