虽然我找到了答案,但我遇到了一个我很难理解的问题.请看这个并给我一个答案的解释.
public class TestSeven extends Thread {
private static int x;
public synchronized void doThings() {
int current = x;
current++;
x = current;
}
public void run() {
doThings();
}
}
Run Code Online (Sandbox Code Playgroud)
问题和答案是......哪种说法是正确的?
A.编译失败.
B.在运行时抛出异常.
C.同步run()方法会使类成为线程安全的.
D.变量"x"中的数据受到保护,不会出现并发访问问题.
E. 将doThings()方法声明为static将使类成为线程安全的.
F.在同步(new Object()){}块中包装doThings()中的语句将使该类成为线程安全的.
大胆的一个作为答案.感谢您的回复!
在编写一些代码时,我注意到long(原始)数据类型不需要后缀l或L。我的代码可以编译并正常运行。谁能解释这个背后的逻辑?提前致谢..
long l=435; //Complies and Run fine
Run Code Online (Sandbox Code Playgroud) 我需要一个清除选中复选框的按钮.请有人指导我.提前致谢.
import Tkinter
from Tkinter import*
top = Tkinter.Tk()
CheckVar1=IntVar()
CheckVar2=IntVar()
C1=Checkbutton(top, text = "Music", variable = CheckVar1,
onvalue = 1, offvalue = 0, height=5,
width = 20,activebackground="red",bg="green")
C2=Checkbutton(top, text = "Video", variable = CheckVar2,
onvalue = 1, offvalue = 0, height=5,
width = 20)
C1.pack()
C2.pack()
B = Tkinter.Button(top, text ="Hello",activebackground="red",
,bd=3,bg="green",width=5) #Button
B.pack()
top.mainloop()
Run Code Online (Sandbox Code Playgroud) 我正在尝试学习python中的继承概念。我有一个雇员班级和派生班级主管。
class Employee:
'Class defined for employee'
def __init__(self, name, dept, salary):
self.name = name
self.dept = dept
self.salary = salary
Run Code Online (Sandbox Code Playgroud)
子类
class Executive(Employee):
def __init__(self, name, dept, salary, hascar):
Employee.__init__(name, dept, salary)
self.hascar = hascar
Run Code Online (Sandbox Code Playgroud)
具有car是传递给构造函数的布尔值,但是这会给我一个错误:
init Employee中的文件“ I:\ Python_practicals \ com \ python \ oop \ Executive.py”,第7行 。初始化(名称,部门,薪金)TypeError:初始化()缺少1个必需的位置参数:'salary'
当我尝试实例化Executive对象时。
emp4 = Executive("Nirmal", "Accounting", 150000, True)