会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132906个问题
Python 全系列/第八阶段:轻量级Web开发利器-Flask框架/Flask视图基础和URL 5567楼
JAVA 全系列/第三阶段:数据库编程/MySQL数据库 5569楼

class Person {
    private String name;
    private int age;
    public Person() {
 
    }
    public Person(String name, int age) {
        this.name = name;
        // this.age = age;//构造方法中不能直接赋值,应该调用setAge方法
        setAge(age);
    }
     
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setAge(int age) {
        //在赋值之前先判断年龄是否合法
        if (age > 130 || age < 0) {
            this.age = 18;//不合法赋默认值18
        else {
            this.age = age;//合法才能赋值给属性age
        }
    }
    public int getAge() {
        return age;
    }
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }
}
 
public class Test2 {
    public static void main(String[] args) {
        Person p1 = new Person();
        //p1.name = "小红"; //编译错误
        //p1.age = -45;  //编译错误
        p1.setName("小红");
        p1.setAge(-45);
        System.out.println(p1);
         
        Person p2 = new Person("小白"300);
        System.out.println(p2);
    }
}

老师,构造方法Person中,不使用this.age=age;是否因为需要用setAge(age);来限制输入的年龄

JAVA 全系列/第一阶段:JAVA 快速入门/面向对象详解和JVM底层内存分析 5571楼
Python 全系列/第一阶段:Python入门/控制语句 5573楼
Python 全系列/第二十三阶段:人工智能基础_机器学习理论和实战(旧)/代码实战梯度下降法与优化 5576楼
JAVA 全系列/(旧的隐藏)第二十一阶段:百战商城项目(Spring Cloud最新架构)/百战商城项目 5578楼

老师我再做这个记事本的时候想改下记事本的说,功能在保存的时候出了点问题

# 文本框
#coding=utf-8

from tkinter import *
from tkinter.filedialog import *


filename=""

class Application(Frame):
    def __init__(self,master=None):
        super().__init__()

        self.master=master
        self.pack()
        self.createWidget()

    def createWidget(self):
        #创建主菜单
        menubar = Menu(root)
        #创建下拉菜单
        menuFile = Menu(menubar)
        menuEdit = Menu(menubar)
        menuHelp = Menu(menubar)
        #添加菜单名字
        menubar.add_cascade(label="文件", menu=menuFile)
        menubar.add_cascade(label="编辑", menu=menuEdit)
        menubar.add_cascade(label="帮助", menu=menuHelp)
        #添加菜单选项
        menuFile.add_command(label="新建" ,accelerator="ctrl+n" ,comman=self.newfile)
        menuFile.add_command(label="打开" ,accelerator="ctrl+o" ,comman=self.opnefile)
        menuFile.add_command(label="保存", accelerator="ctrl+s", comman=self.savafile)
        menuFile.add_command(label="退出", accelerator="ctrl+q", comman=self.exitfile)
        root["menu"]=menubar

        self.textpad = Text(root,width=100,height=100)
        self.textpad.pack()
    def newfile(self):
        global filename
        root.title("未命名.txt")
        filename=None
        self.textpad.delete(1.0,END)
    def opnefile(self):
        global filename
        self.textpad.delete(1.0,END)
        with askopenfile(title="打开文本文件",defaultextension=".txt") as f:
            print(f.name)
            self.textpad.insert(INSERT,f.read())
    def savafile(self):
        global filename
        with open(filename,"w") as f:
            c= self.textpad.get(1.0,END)
            f.write(c)
            f.close()

    def exitfile(self):
        root.quit()
if __name__ == '__main__':

    root=Tk()
    root.geometry("300x300")
    root.title("记事本")
    app = Application(master=root)
    root.mainloop()

image.png

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

image.png

Python 全系列/第四阶段:函数式编程和核心特性/生成器、迭代器、动态性 5580楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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