我在python3类中有2个列表:
self.keys = ["a","b","c","d"]
self.values = [1,2,3,4]
Run Code Online (Sandbox Code Playgroud)
len(self.keys)== len(self.values),总是.
我想创建一个模仿字典打印输出的字符串: {a:1, b:2, c:3, d:4}
我在一个方法中执行它包含:
sr = ""
for i,k in enumerate(self.keys):
sr += "{}:{},".format(k,self.values[i])
return "{%s}" % sr[:len(sr)-1]
Run Code Online (Sandbox Code Playgroud)
有可能在一条线上吗?如果没有,有没有更好的方法来做到这一点?
我是Java的新手,我想知道为什么可以这样设置私有属性的值,而不使用setter?:
class Example {
private int[] thing;
public void initialize() {
thing = new int[10];
}
public int[] getThing() {
return thing;
}
}
class myclass {
public static void main(String args[]) {
Example e = new Example();
e.initialize();
e.getThing()[0] = 1;
System.out.println(e.getThing()[0]) // value is still 1...
}
Run Code Online (Sandbox Code Playgroud)
我真的不明白为什么这是合法的......
编辑:我希望e.getThing()返回thing的值而不是对thing的引用,即使是这种情况,因为"thing"是私有的,我以为我不能直接修改它.