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

class ComputerFactory:
    _obj = None
    _init_flag = True

    def create_computer(self,brand):
        if brand == '联想':
           return Lenovo()
        elif brand == '华硕':
           return Asus()
        elif brand == '神舟':
            return Hasee()
        else:
            return '未知品牌,无法创建'

    def __new__(cls, *args, **kwargs):
        if cls._obj == None:
            cls._obj = object.__new__(cls)
        return cls._obj

    def __init__(self):
        if ComputerFactory._init_flag:
            print("init ComputerFactory....")
            ComputerFactory._init_flag = False


class Computer(ComputerFactory):
    def __init__(self,unit,amount):
        self.unit = unit
        self.amount = amount

    def calculate(self):
        print('总销售额是:{0}'.format(self.unit*self.amount))


class Lenovo(Computer):
    def __init__(self,unit,amount,year):
        self.year = year
        Computer.__init__(self,unit,amount)

    def calculate(self):
        print('第{0}年的总销售额是{1}'.format(self.year,self.unit*self.amount))

class Asus(Computer):
    def __init__(self, unit, amount, year):
        self.year = year
        Computer.__init__(self, unit, amount)

    def calculate(self):
        print('第{0}年的总销售额是{1}'.format(self.year, self.unit * self.amount))

class Hasee(Computer):
    def __init__(self, unit, amount, year):
        self.year = year
        Computer.__init__(self, unit, amount)

    def calculate(self):
        print('第{0}年的总销售额是{1}'.format(self.year, self.unit * self.amount))


factory = ComputerFactory()
c1 = factory.create_computer('联想')
c2 = factory.create_computer('华硕')
c3 = factory.create_computer('神舟')
print(c1)
print(c2)
print(c3)
l=Lenovo()
l.calculate('联想',3000,4789,2020)

老师好,最后想分别计算各个品牌的数据并打印输出,这里的代码不会写。

Python 全系列/第一阶段:Python入门/面向对象 3151楼
Python 全系列/第一阶段:Python入门/Python入门(动画版) 3154楼
Python 全系列/第一阶段:Python入门/序列 3156楼
Python 全系列/第一阶段:Python入门/函数和内存分析 3159楼

'''
定义一个 Employee 雇员类,要求如下:
(1) 属性有:id、name、salary
(2) 运算符重载+:实现两个对象相加时,默认返回他们的薪水和
(3) 构造方法要求:输入 name、salary,不输入 id。id 采用自增的方式,从 1000 开始自增,第一个新增对象是 1001,第二个新增对象是 1002
(4) 根据 salary 属性,使用@property 设置属性的 get 和 set 方法。set 方法要求输入:1000-50000 范围的数
'''
class Employee:
    id=1000
    def __init__(self,name,salary):
        self.__name=name
        self.__salary=salary
        Employee.id+=1
    def __add__(self, other):
        if isinstance(other,Employee):
            return self.__salary+other.__salary
    @property
    def salary(self):
        if 1000<self.__salary<50000:
            return self.__salary
        else:
            return '录入错误,薪水在1000到50000这个范围'
    @salary.setter
    def salary(self,salary):
        if 1000<salary<50000:
            self.__salary=salary
        else:
            return '薪水录入错误!只能在1000-50000之间'
emp1=Employee('高淇',30000)
print(emp1.id)
print(emp1.salary)
emp2=Employee('高希希',20000)
print(emp2.id)
print(emp2.salary)
emp2.salary=60000
print(emp1+emp2)

1.为什么emp2.salary=60000 并没有执行?

image.png

2.在运行emp2后再输出emp1.id,emp1.id就会等于emp2.id  请问老师这种情况正常吗?

image.png

image.png


Python 全系列/第一阶段:Python入门/面向对象 3161楼

111111111.PNG

222222222.PNG

老师我这结果和您视频里的有点出入,想问一下为什么

Python 全系列/第一阶段:Python入门/面向对象 3163楼
Python 全系列/第一阶段:Python入门/面向对象 3164楼
Python 全系列/第一阶段:Python入门/面向对象 3165楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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