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

python第六章实操作业

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

python第六章实操作业

python第六章实操作业

1. 如下代码,使用图文分析整个内存过程2.设计一个名为MyRectangle的矩形类来表示矩形

1. 如下代码,使用图文分析整个内存过程
class Student:
    company="尚学堂"#类属性
    count=0#类属性
    def __init__(self,name,score):
        self.name=name#实例属性
        self.score=score
        Student.count=Student.count+1
    def say_score(self):#实例方法
        print("我的公司是:",Student.company)
        print(self.name,'的分数是:',self.score)
s1=Student('高淇',80)#s1是实例对象,自动调用__init__()方法
s1.say_score()
print('一共创建{0}个Student对象'.format(Student.count))

2.设计一个名为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=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+self.height))

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

r1=MyRectangle()
r1.getArea()
r1.getPerimeter()
r1.draw()

结果如下:

面积为 10000
周长为 400

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/743946.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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