如果在包含类之外仍然无法访问Java中私有内部类公共成员的原因是什么?或者可以吗?
public class DataStructure {
// ...
private class InnerEvenIterator {
// ...
public boolean hasNext() { // Why public?
// ...
}
}
}
Run Code Online (Sandbox Code Playgroud) class MyClass
{
public static final int num=90;
}
Run Code Online (Sandbox Code Playgroud)
在以下代码中:
class Outer {
private:
void f_private(Outer::Inner in); // Wrong
public:
class Inner {};
void f_public(Outer::Inner in); // OK
};
Run Code Online (Sandbox Code Playgroud)
f_private()
不能使用嵌套类Outer::Inner
作为参数类型。但是在f_public()
.
有人可以向我解释这是基于什么规则,它有什么好处?
我有这个代码(JSFiddle)
var OBJ = function(){
var privateVar = 23;
var self = this;
return {
thePrivateVar : function() {
return privateVar;
},
thePrivateVarTimeout : function() {
setTimeout(function() { alert(self.thePrivateVar()); } , 10);
}
}
}();
alert(OBJ.thePrivateVar());
OBJ.thePrivateVarTimeout();
Run Code Online (Sandbox Code Playgroud)
这是我正在遇到的一个真正问题的抽象.
所以 - 我希望调用OBJ.thePrivateVarTimeout()
等待10
然后alert
使用23(我希望它通过其他公开的方法访问).
但self
似乎没有正确设置.当我设置self = this
它时,它似乎this
不是对函数的引用,而是对全局对象的引用.为什么是这样?
如何让public方法thePrivateVarTimeout
调用其他公共方法thePrivateVar
?
TypeScript 构造函数中的公共成员在类中是公共的,而私有成员是私有的,我对吗?
如果是这样,公共成员和属性之间的有效区别是什么?
假设不同之处在于属性可以更像 c# 属性(即可以具有与其访问相关联的代码),为什么要使字段公开,而没有使其成为属性所固有的保护?
我正在 Dropwizard 应用程序中设置视图,但遇到了一个关于 Freemarker 的奇怪问题。
public class ExampleFreemarkerView extends View {
private Foo foo;
public ContractHtmlView(Foo Foo) {
super("FooView.ftl");
this.foo = foo;
}
public Contract getFoo() { return foo };
}
public class Foo {
public String bar = "Hello World";
}
Run Code Online (Sandbox Code Playgroud)
和 FooView.ftl
<html>
<body>
<h1>${foo.bar}</h1>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
呈现时的预期输出ExampleFreemarkerView
是显示 的 HTML 文档Hello World
。
实际发生的是 Freemarker 抛出异常,抱怨${foo.bar}
- 特别bar
是 - 未定义。
这似乎是因为bar
是一个公共字段,没有吸气剂。当我向 中添加public String getBar() …
java freemarker public-members dropwizard dropwizard-templates
我知道这可能听起来很愚蠢,但我真的很想知道:)我正在学习c#当前,
如你所知,你需要设置"对象"(按钮,标签,文本,变量等)公开或任何你喜欢的.
但是,您仍然需要编写如下代码:
// my point is you cant just type label1.text you need to type class.label1.text
// so there is no chance of getting bugged
//because there is label1 in each of forms/classes
class Classlol = new class();
classlol.label1.blabla
Run Code Online (Sandbox Code Playgroud)
那么以其他形式使其无法达到的重点是什么?为什么每件事都不公开或默认不公开?
谢谢.
我在写一个银行应用程序Python
,并从这里读了一些源代码的银行应用程序.该balance
课程定义如下:
class Balance(object):
""" the balance class includes the balance operations """
def __init__(self):
""" instantiate the class """
self.total = 0
def add(self, value):
""" add value to the total
Args:
value (int): numeric value
"""
value = int(value)
self.total += value
def subtract(self, value):
""" subtract value from the total
Args:
value (int): numeric value
"""
value = int(value)
self.total -= value
Run Code Online (Sandbox Code Playgroud)
我的问题
由于不应该在类之外访问余额细节,我们应该定义属性self.total
,self.__total
因为我们应该使它变得private
相当public
可变?我的思路是否正确?
如果在类中声明公共变量,则可以从也是该类成员的任何函数中修改该变量.
如果在函数内声明变量,则其范围不会超出函数的范围.
那么公共类变量本质上是一个全局变量,可以被类的任何成员访问和更改吗?
如果是这样的话,全局变量和公共变量之间有什么区别?
因此,如果我有以下 Super 类:
class Super {
public:
string member = "bla bla";
void doSth() { cout << member; }
};
Run Code Online (Sandbox Code Playgroud)
还有一个继承 Super 的 Sub 类:
class Sub : Super {
public:
string member2 = "bla bla 2";
};
Run Code Online (Sandbox Code Playgroud)
然后,当我有一个 Sub 对象时,我无法访问 Super 的成员,即使它们是公共的。
using namespace std;
int main(){
Sub sub;
cout << sub.member2 << endl;
cout << sub.member << endl; // error: public Super::member is inaccessible
sub.doSth(); // error: public Super::doSth() is inaccessible
}
Run Code Online (Sandbox Code Playgroud)
但为什么如果它们是公开的呢?或者我做错了什么?
public-members ×10
c++ ×3
java ×3
oop ×2
scope ×2
c# ×1
class ×1
constructor ×1
difference ×1
dropwizard ×1
freemarker ×1
javascript ×1
python ×1
python-2.7 ×1
subclass ×1
superclass ×1
typescript ×1