Py学习Day5:小练习

练习一:如下代码,使用图文分析整个内存过程:

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('gao',80) #s1 是实例对象,自动调用__init__()方法
s1.say_score()
print('一共创建{0}个 Student 对象'.format(Student.count))

 练习二:

设计一个名为 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):
        print("是否输入四边形左上角的X,Y坐标值,Y/N?如N默认原点")
        i=input()
        if i=="Y":
            self.x,self.y=map(int,input("输入X,Y值,用空格隔开:").split())
        else:
            self.x,self.y=0,0
        print("是否输入四边形宽度值,Y/N?如N默认10")
        i=input()
        if i=="Y":
            self.width=int(input("输入宽度值:"))
        else:
            self.width=10
        print("是否输入四边形高度值,Y/N?如N默认10")
        i=input()
        if i=="Y":
            self.height=int(input("输入宽度值:"))
        else:
            self.height=10


    def GetArea(self):
        print("这个四边形的高是:{0},宽是:{1},面积是:{2}".format(self.height,self.width,self.width*self.height))
    def GetPerimeter(self):
        print("这个四边形的高是:{0},宽是:{1},周长是:{2}".format(self.height,self.width,(self.width+self.height)*2))
    def draw(self):
        turtle.penup()
        turtle.goto(self.x,self.y)
        turtle.pendown()
        turtle.goto((self.x+self.width),(self.y))
        turtle.goto((self.x + self.width), (self.y+self.height))
        turtle.goto((self.x), (self.y+self.height))
        turtle.goto(self.x, self.y)

cube=MyRectangle()
cube.GetArea()
cube.GetPerimeter()
cube.draw()
turtle.done()

结果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值