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

#coding=utf-8
'''定义一个 Employee 雇员类,要求如下:
(1) 属性有:id、name、salary
(2) 运算符重载+:实现两个对象相加时,默认返回他们的薪水和
(3) 构造方法要求:输入 name、salary,不输入 id。id 采用自增的方式,从 1000 开始自增,第一个新增对象是 1001,第二个新增对象是 1002
(4) 根据 salary 属性,使用@property 设置属性的 get 和 set 方法。set 方法要求输入:'''
class Employee:
    id = 1000
    def __init__(self,id,name,salary):
        self.name = name
        self.__salary = salary  # 私有变量
        Employee.id += 1
        self.id = Employee.id

    @property
    def salary(self):
        return "ID是:{0},员工名字:{1},薪水:{2}".format(self.id,self.name,self.__salary)

    @salary.setter
    def salary(self,salary):
        if 1000 < salary < 50000:
            self.__salary =salary
        else:
            print("输入有误,salary是1000-50000范围的数")

    def __add__(self, other):
        if isinstance(other, Employee):
            self.__salary += other.__salary
            return '薪水和为:{0}'.format(self.__salary )
        else:
            return '不是同类对象,无法相加!'

emp1 = Employee(1,"高f",100)
emp2 = Employee(2,"高q",1000)
emp3 = Employee(3,"高p",10)
print(emp1.salary)
print(emp2.salary)
print(emp3.salary)
emp1.salary = 100
print(emp1+emp2)

C:\Users\Administrator\PycharmProjects\mypro_exception\venv\Scripts\python.exe C:/Users/Administrator/PycharmProjects/mypro_exception/test03.py

薪水和为:1100

id:1001,薪水为:500

id:1002,薪水为:600

薪水只能在1000-50000之间


Process finished with exit code 0


老师,为甚么在第一次赋值的时候没有报错呢?第二次赋值100元的时候却报错了

Python 全系列/第一阶段:Python入门/面向对象 2506楼
Python 全系列/第一阶段:Python入门/编程基本概念 2509楼
Python 全系列/第一阶段:Python入门/编程基本概念 2510楼

# 设计一个名为 MyRectangle 的矩形类来表示矩形。
'''这个类包含
(1) 左上角顶点的坐标:x,y
(2) 宽度和高度:width、height
(3) 构造方法:传入 x,y,width,height。如果(x,y)不传则默认是 0,如果 width和 height 不传,则默认是 100.
(4) 定义一个 getArea() 计算面积的方法
(5) 定义一个 getPerimeter(),计算周长的方法
(6) 定义一个 draw()方法,使用海龟绘图绘制出这个'''
import turtle


class MyRectangle:

    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        if x == "" or y == "":
            x == 0 and y == 0
        elif width == "" or height == "":
            width == 100 and height == 100
        return "左上角顶点的坐标:{0},{1},矩形的宽:{2},矩形的高:{3}".format(self.x, self.y, self.width, self.height)

    def getArea(self):
        s = self.width * self.height
        print("面积是:", s)

    def getPerimeter(self):
        c = (self.width + self.height) * 2
        print("周长是:", c)

    def draw(self):
        turtle.penup()
        turtle.goto(self.x, self.y)  # 开始起点
        turtle.pendown()  # 下笔
        turtle.goto(self.x, self.y - self.height)
        turtle.goto(self.x + self.width, self.y - (self.height))
        turtle.goto(self.x + self.width, self.y)
        turtle.goto(self.x, self.y)
        turtle.done()  # 保持绘画窗口不消失

s = MyRectangle(x="", y="", width="", height="")
s.getArea()
s.getPerimeter()
s.draw()

老师:这断代码老是报错,找了半天也不知道错在哪里?

Traceback (most recent call last):

  File "C:/Users/Administrator/PycharmProjects/mypro_exception/test.py", line 43, in <module>

    s = MyRectangle(x="", y="", width="", height="")

TypeError: __init__() should return None, not 'str'


Process finished with exit code 1


Python 全系列/第一阶段:Python入门/面向对象 2514楼
Python 全系列/第一阶段:Python入门/Python入门(动画版) 2516楼
Python 全系列/第一阶段:Python入门/Python入门(动画版) 2517楼
Python 全系列/第一阶段:Python入门/编程基本概念 2518楼
Python 全系列/第一阶段:Python入门/Python入门(动画版) 2520楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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