会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132469个问题
Python 全系列/第十五阶段:Python 爬虫开发/爬虫基础 30766楼
Python 全系列/第二阶段:Python 深入与提高/坦克大战 30767楼
JAVA 全系列/(旧的隐藏)第七阶段:JAVA 高级技术/Linux 30768楼
Python 全系列/第十三阶段:高并发性能怪兽-Tornado 框架/Tornado项目 30769楼
Python 全系列/第十六阶段:数据结构与算法/数据结构与算法 30772楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 30775楼

from tkinter import *
from tkinter import messagebox
import random

win_width=450
win_height=300
class Application(Frame):
    def __init__(self,master):
        super().__init__(master)
        self.master = master
        self.pack()
        # label组建:小姐姐真漂亮,我喜欢你,图片
        self.label01 = Label(root, text="小姐姐真漂亮,我喜欢你\n做我女朋友好吗?"
                             , font=("KaiTi", 17), width=27, height=2, fg="red")
        self.label01.place(x=0, y=0)
        global photo
        photo = PhotoImage(file="e:/python图片/AKAKA.gif")
        self.label02 = Label(root, image=photo)
        self.label02.place(x=200, y=60)

        # 设置按钮好哒、不愿意
        self.but01 = Button(root, text="好哒", font=("KaiTi", 28), command=self.haoda, activebackground="blue")
        self.but01.place(x=60,y=70)
        self.but02 = Button(root,text="不愿意", font=("KaiTi", 15))
        self.but02.place(x=60,y=170)

        self.but02.bind("<Enter>",self.buyuanyi)

    def buyuanyi(self, event):
        x1 = random.randrange(int(win_width)-50)
        y1 = random.randrange(int(win_height)-50)
        print(x1, y1)
        self.but02.place(x=x1, y=y1)

    def haoda(self):
        messagebox.showinfo("宝贝", "属于我们的甜甜的恋爱开始啦")
        root.destroy()


if __name__ == '__main__':
    root = Tk()
    root.title("I LOVE U")
    root.geometry(str(win_width)+"x"+str(win_height)+"+700+400")
    app = Application(root)
    root.mainloop()

老师,为啥这些地方是root,不能是self,我用了self,就显示不出来了?417236ae20f6b9145b984759fc5ab06.png

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 30776楼

//老师,如果在Account类中重写equals方法,使得当accountNo相同的情况下判定为同一对象
那为什么线程同步没有进行互斥呢

import java.util.Objects;

class Account {
    private String accountNo;
    private double balance;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Account account = (Account) o;
        return Double.compare(account.balance, balance) == 0 && Objects.equals(accountNo, account.accountNo);
    }

    @Override
    public int hashCode() {
        return Objects.hash(accountNo, balance);
    }

    public Account(String accountNo, double balance) {
        this.accountNo = accountNo;
        this.balance = balance;
    }

    public Account() {
    }

    public String getAccountNo() {
        return accountNo;
    }

    public void setAccountNo(String accountNo) {
        this.accountNo = accountNo;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }
}

class DrawThread extends Thread {
    //账户对象
    private Account account;
    private double drawMoney;

    public DrawThread(String Name, Account account, double drawMoney) {
        super(Name);
        this.account = account;
        this.drawMoney = drawMoney;
    }

    @Override
    public void run() {
        synchronized (this.account){
            //判断当前账户余额是否大于或等于取款金额
            if (this.account.getBalance() >= this.drawMoney) {
                System.out.println(this.getName() + "取款成功!" + this.drawMoney);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //更新账户余额
                this.account.setBalance(this.account.getBalance() - this.drawMoney);
                System.out.println("当前余额为:" + this.account.getBalance());
            } else {
                System.out.println(this.getName() + "取款失败,余额不足");
            }
        }
    }
}

public class DrawMoneyDemo {
    public static void main(String[] args) {
        Account account = new Account("1001", 1000);
        Account account1 = new Account("1001", 1000);
        System.out.println(account1.equals(account));
        new DrawThread("老公", account, 800).start();
        new DrawThread("老妞", account1, 800).start();
    }
}


JAVA 全系列/第二阶段:JAVA 基础深化和提高/多线程技术(旧) 30778楼
JAVA 全系列/第四阶段:网页编程和设计/Javascript 语言(旧) 30780楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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