栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python学习 DAY 10 第六章实操作业

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python学习 DAY 10 第六章实操作业

DAY10 第六章实操作业

1.设计一个名为 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 as t

class MyRectangle:
    def __init__(self,x=0,y=0,width=100,height=100):
        self.x = x
        self.y = y
        self.width = width
        self.height = height

    def getArea(self):
        print("面积为:",self.width*self.height)

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

    def draw(self):
        t.penup()
        t.goto(self.x,self.y)
        t.pendown()
        t.goto(self.x+self.width,self.y)
        t.goto(self.x+self.width,self.y-self.height)
        t.goto(self.x,self.y-self.height)
        t.goto(self.x,self.y)
        t.hideturtle()
		t.done()

m = MyRectangle(20,20,200)
m.getArea()
m.getPerimeter()
m.draw()

运行结果:

	面积为: 20000
	周长为: 600

  1. 定义发动机类 Motor、底盘类 Chassis、座椅类 Seat,车辆外壳类 Shell,并使用组合 关系定义汽车类。其他要求如下:

定义汽车的 run()方法,里面需要调用 Motor 类的 work()方法,也需要调用座椅 类 Seat 的 work()方法,也需要调用底盘类 Chassis 的 work()方法。

class Car:
    def __init__(self,motor,chassis,seat,shell):
        self.motor = motor
        self.chassis = chassis
        self.seat = seat
        self.shell = shell

    def run(self):
        print("汽车开始行驶")

class Motor:
    def work(self):
        print("发动机点火")

class Chassis:
    def work(self):
        print("调节底盘")

class Seat:
    def work(self):
        print("调节座椅")

class Shell:
    pass

c = Car(Motor(),Chassis(),Seat(),Shell())
c.motor.work()
c.chassis.work()
c.seat.work()
c.run()

结果:

发动机点火
调节底盘
调节座椅
汽车开始行驶
  1. 使用工厂模式、单例模式实现如下需求:

(1) 电脑工厂类 ComputerFactory 用于生产电脑 Computer。工厂类使用单例模式, 也就是说只能有一个工厂对象;

(2) 工厂类中可以生产各种品牌的电脑:联想、华硕、神舟;

(3) 各种品牌的电脑使用继承实现;

(4) 父类是Computer类,定义了calculate 方法;

(5) 各品牌电脑类需要重写父类的calculate 方法。

class ComputerFactory:
    __obj = None       #类属性
    __init_flag = True

    def creat_computer(self,brand):
        if brand == "联想":
            return Lenovo()
        if brand == "华硕":
            return ASUS()
        if 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:
    def calculate(self):
        print("计算")

class Lenovo(Computer):
    '''重写父类的方法'''
    def calculate(self):
        print("联想计算")

class ASUS(Computer):
    '''重写父类的方法'''
    def calculate(self):
        print("华硕计算")

class Hasee(Computer):
    '''重写父类的方法'''
    def calculate(self):
        print("神舟计算")

factory1 = ComputerFactory()
c1 = factory1.creat_computer("联想")
c2 = factory1.creat_computer("华硕")
c3 = factory1.creat_computer("神舟")
print("c1:",c1)
print("c2:",c2)
print("c3:",c3) #对比三个品牌存储位置不同

factory2 = ComputerFactory()
factory3 = ComputerFactory()
print("factory1:",factory1)
print("factory2:",factory2)
print("factory3:",factory3)  #单例模式,只有一个工厂对象

Lenovo().calculate()
ASUS().calculate()
Hasee().calculate() #重写父类的方法

运行结果:

init ComputerFactory...
c1: <__main__.Lenovo object at 0x000001B7E6486F70>
c2: <__main__.ASUS object at 0x000001B7E6486CA0>
c3: <__main__.Hasee object at 0x000001B7E6486BE0>
factory1: <__main__.ComputerFactory object at 0x000001B7E6486FA0>
factory2: <__main__.ComputerFactory object at 0x000001B7E6486FA0>
factory3: <__main__.ComputerFactory object at 0x000001B7E6486FA0>
联想计算
华硕计算
神舟计算
  1. 定义一个 Employee 雇员类,要求如下:

(1) 属性有:id、name、salary;

(2) 运算符重载+:实现两个对象相加时,默认返回他们的薪水和;

(3) 构造方法要求:输入name、salary,不输入 id。id 采用自增的方式,从 1000 开 始自增,第一个新增对象是 1001,第二个新增对象是 1002;

(4) 根据salary属性,使用@property设置属性的 get 和 set 方法。set 方法要求输入:1000-50000 范围的数字。

class Employee:
    __init_flag = True
    d = 0
    def __init__(self,ID,name,salary):

        self.ID = ID
        self.name = name
        if 1000 < salary < 50000:
            self.__salary = salary
        else:
            print("{0}录入错误,薪水应在1000~50000".format(self.name))

        if self.ID == ' ':
            if Employee.__init_flag: #第一次及给无ID员工赋值
                self.ID = 1000
                Employee.__init_flag = False
            else:         #第二次及以后给无ID员工赋值
                Employee.d += 1
                self.ID = 1000+Employee.d

    def __add__(self, other):
        '''运算符重载'''
        if isinstance(other,Employee):
            return self.__salary+other.__salary
        else:
            return "不是同类对象"


    @property
    def salary(self):
        return self.__salary

    @salary.setter
    def salary(self,salary):
        if 1000 

运行结果:

HeikoLee录入错误,薪水应在1000~50000
14000
8000
录入错误,薪水应在1000~50000
1000
1001
1002
1003
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/275086.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号