会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132847个问题

屏幕截图 2023-07-20 112114.png

老师就是为什么a=T()这个语句明明调用了W()这个类中的init方法,但是他没有打印结果呢

Python 全系列/第一阶段:Python入门/面向对象 6271楼
Python 全系列/第五阶段:数据库编程/mysql的使用 6272楼

class LipStick {
    private String LName;

    public LipStick(String LName) {
        this.LName = LName;
    }
}
class Mirror{
    private String MName;
    public Mirror(String MName){
        this.MName = MName;
    }
}
class Makeup extends Thread{
    private Mirror mirror = new Mirror("m1");
    private LipStick lipStick = new LipStick("l1");
    private String girlName;
    private int flag;//0代表口红,1代表镜子

    public Makeup(int flag,String girlName){
        this.flag = flag;
        this.girlName = girlName;
    }

    @Override
    public void run() {
        this.doMakeup();
    }
    public void doMakeup(){
        if(flag==0){
            synchronized (lipStick){
                System.out.println(this.girlName+" 拿着口红");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (mirror){
                    System.out.println(this.girlName+" 拿着镜子");
                }
            }
        }else{
            synchronized (mirror){
                System.out.println(this.girlName+" 拿着镜子");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (lipStick){
                    System.out.println(this.girlName+" 拿着口红");
                }
            }
        }
    }
}



public class TestDeadThread {
    public static void main(String[] args) {
        Thread T1 = new Thread(new Makeup(0,"1"));
        Thread T2 = new Thread(new Makeup(1,"2"));
        T1.start();
        T2.start();

    }
}

老师想问问为什么没有死锁呢image.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术 6273楼
Python 全系列/第一阶段:Python入门/编程基本概念 6275楼
JAVA 全系列/第八阶段:SpringBoot与MybatisPlus/Spring Boot旧 6276楼


pymysql
con=pymysql.connect()
cursor=con.cursor()
:
    spl=cursor.execute(sal)
    con.commit()
:
    ()
    con.rollback()
:
    con.close()

老师看看我这什么问题,我一执行就出现下面报错

Traceback (most recent call last):

  File "C:\Users\acer\AppData\Roaming\Python\Python37\site-packages\pymysql\connections.py", line 583, in connect

    **kwargs)

  File "C:\Program Files (x86)\Python37-32\lib\socket.py", line 707, in create_connection

    for res in getaddrinfo(host, port, 0, SOCK_STREAM):

  File "C:\Program Files (x86)\Python37-32\lib\socket.py", line 748, in getaddrinfo

    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):

socket.gaierror: [Errno 11001] getaddrinfo failed


During handling of the above exception, another exception occurred:


Traceback (most recent call last):

  File "D:/python/pythondemo/com/插入数据.py", line 2, in <module>

    con=pymysql.connect('localhosrt','root','sen6263234','pythondemo',3306)

  File "C:\Users\acer\AppData\Roaming\Python\Python37\site-packages\pymysql\__init__.py", line 94, in Connect

    return Connection(*args, **kwargs)

  File "C:\Users\acer\AppData\Roaming\Python\Python37\site-packages\pymysql\connections.py", line 325, in __init__

    self.connect()

  File "C:\Users\acer\AppData\Roaming\Python\Python37\site-packages\pymysql\connections.py", line 630, in connect

    raise exc

pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhosrt' ([Errno 11001] getaddrinfo failed)")


Python 全系列/第五阶段:数据库编程/mysql的使用 6277楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 6278楼

"""
开发一个简单的记事本。
包含: 新建,保存,修改文本文件,退出
包含; 各种快捷键的处理
"""
from tkinter.filedialog import *
from tkinter.colorchooser import *


class Application(Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.textpad = None
        self.filename = None
        self.contextMenu = None
        self.pack()
        self.createWidget()

    def createWidget(self):
        menubar = Menu(root)
        root["menu"] = menubar

        menuFile = Menu(menubar)
        menuEdit = Menu(menubar)
        menuHelp = Menu(menubar)

        menubar.add_cascade(label="文件(F)", menu=menuFile)
        menubar.add_cascade(label="编辑(E)", menu=menuEdit)
        menubar.add_cascade(label="帮助(H)", menu=menuHelp)

        menuFile.add_command(label="新建", accelerator="ctrl+n", command=self.newfile)
        menuFile.add_command(label="打开", accelerator="ctrl+o", command=self.openfile)
        menuFile.add_command(label="保存", accelerator="ctrl+s", command=self.savefile)
        menuFile.add_command(label="退出", accelerator="ctrl+q", command=self.exit)

        root.bind("<Control-n>", lambda event: self.newfile())
        root.bind("<Control-0>", lambda event: self.openfile())
        root.bind("<Control-s>", lambda event: self.savefile())
        root.bind("<Control-h>", lambda event: self.exit())

        self.textpad = Text(root, width=50, height=30)
        self.textpad.pack()

        self.contextMenu = Menu(root)
        self.contextMenu.add_command(label="背景颜色", command=self.openAskColor)

        root.bind("<Button-3>", self.createContextMenu)

    def newfile(self):
        self.textpad.delete('1.0', "end")
        self.filename = asksaveasfilename(title='另存为',
                                          initialfile='未命名.txt', filetypes=[("文本文件", "*.txt")], defaultextension=".txt")
        print(self.filename)
        self.savefile()

    def openfile(self):
        with askopenfile(title="打开文件") as f:
            self.textpad.insert(INSERT, f.read())
            self.filename = f.name
            print(f.name)

    def savefile(self):
        with open(self.filename, "w") as f:
            c = self.textpad.get(1.0, END)
            f.write(c)

    def exit(self):
        root.quit()

    def openAskColor(self):
        s1 = askcolor(color="red", title="选择背景色")
        self.textpad.config(bg=s1[1])

    def createContextMenu(self, event):
        self.contextMenu.post(event.x_root, event.y_root)


if __name__ == "__main__":
    root = Tk()
    root.geometry("400x100+200+100")
    root.title("记事本")
    app = Application(master=root)
    root.mainloop()

Exception in Tkinter callback

Traceback (most recent call last):

  File "C:\Users\Inspiron\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 1699, in __call__

    return self.func(*args)

  File "D:/WorkSpace/py_case/pythonProject/FirstGUI/twenty-two.py", line 63, in savefile

    with open(self.filename, "w") as f:

TypeError: expected str, bytes or os.PathLike object, not NoneType

调用Savefile方法总是提示这个错误,newfile方法里调用也失败,老师求解答!

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 6279楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 6280楼


屏幕截图 2023-10-27 173317.png

Python 全系列/第十四阶段:Python 爬虫开发/爬虫基础 6284楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637