我现在来自Java和学习Python.我尝试理解Python中类成员的概念.
这是Java中的示例程序:
class Hello {
int x = 0;
void ex() {
x = 7;
}
public static void main(String args[]) {
Hello h = new Hello();
System.out.println(h.x);
h.ex();
System.out.println(h.x);
} }
Run Code Online (Sandbox Code Playgroud)
这是我在Python中所做的,遵循我发现的一些例子:
class Hello:
def __init__(self) :
self.x = 0
def ex(self):
self.x = 7
h = Hello()
print(h.x)
h.ex()
print(h.x)
Run Code Online (Sandbox Code Playgroud)
两个程序都返回:
0
7
Run Code Online (Sandbox Code Playgroud)
这是我的问题: