《python语言程序设计》2018版第8章19题几何Rectangle2D类(上)--原来我可以直接调用

在这里插入图片描述
2024.9.29
玩了好几天游戏。
感觉有点灵感了。还想继续玩游戏。
2024.10.4
今天练习阿斯汤加练完从早上10点睡到下午2点.跑到单位玩游戏玩到晚上10点多.
现在回家突然有了灵感
顺便说一句,因为后弯不好,明天加练一次.
然后去丈母娘家.
加油吧

我在self的基础上如何让它的变量能够被外部调用.

 def draw_rec2(self, other_rec):
        x = other_rec.out_x
        y = other_rec.out_y
        width_r = other_rec.out_width
        height_r = other_rec.out_height
        turtle.penup()
        turtle.goto(x, y)
        turtle.dot(6, "yellow")
        turtle.goto(width_r/ 2 + x, height_r/2 + y)
        turtle.pendown()
        turtle.forward(width_r)
        turtle.right(90)
        turtle.forward(height_r)
        turtle.right(90)
        turtle.forward(width_r)
        turtle.right(90)
        turtle.forward(height_r)
        turtle.hideturtle()
        turtle.done()

在这个代码前self 后面加了一个other_rec利用他来作为输入的入口.也是为了扣题上所说的.

但实际的问题却很头疼.

1 statement expected,found py:dedemt

请添加图片描述
请添加图片描述

2 Method ‘draw_rec2’ may be ‘static’

请添加图片描述

不管了实际试一下看看如何


class Rectangle2D:
    def __init__(self, x, y, width, height):
        self.__x = x
        self.__y = y
        self.__width = width
        self.__height = height

    def get_area(self):
        return self.__width * self.__height

    def get_perimeter(self):
        return (self.__width + self.__height) * 2

    def out_x(self):
        return self.__x

    def out_y(self):
        return self.__y

    def out_width(self):
        return self.__width

    def out_height(self):
        return self.__height
        
    # 他内带绘画矩形,直接调用的self
    def draw_rec1(self):
        turtle.penup()
        turtle.goto(self.__x, self.__y)
        turtle.dot(6, "blue")
        turtle.goto(self.__x-self.__width / 2 , self.__height / 2 + self.__y)
        turtle.pendown()
        turtle.forward(self.__width)
        turtle.right(90)
        turtle.forward(self.__height)
        turtle.right(90)
        turtle.forward(self.__width)
        turtle.right(90)
        turtle.forward(self.__height)
        turtle.hideturtle()
        turtle.done()
    #此处是调用外部的矩形并且绘画
    def draw_rec2(self, other_rec):
        x = other_rec.out_x
        y = other_rec.out_y
        width_r = other_rec.out_width
        height_r = other_rec.out_height
        turtle.penup()
        turtle.goto(x, y)
        turtle.dot(6, "yellow")
        turtle.goto(x - width_r / 2, height_r / 2 + y)
        turtle.pendown()
        turtle.forward(width_r)
        turtle.right(90)
        turtle.forward(height_r)
        turtle.right(90)
        turtle.forward(width_r)
        turtle.right(90)
        turtle.forward(height_r)
        turtle.hideturtle()
        turtle.done()


a = Rectangle2D(100, 0, 50, 60)


def main():
    #调用它成功
    a.draw_rec1()
   


main()

只绘制rec1成功

请添加图片描述

奇怪的计算

篇幅关系仅仅展示增加修改的地方.其他和上面的代码一样.


a = Rectangle2D(100, 0, 50, 60)
b = Rectangle2D(60, 0, 80, 30)


def main():
    a.draw_rec1()
    a.draw_rec2(b)



main()

出错的提示请添加图片描述

请添加图片描述

似乎发现点什么??

我把draw_rec1的属于self的代码加入到了draw_rec2里刚才所谓的static提示消失了.静态变动态了!!

TypeError: unsupported operand type(s) for -: ‘method’ and ‘float’

成功了原来直接调用可以不用out


    def draw_rec2(self, other_rec):
        x_1 = other_rec.__x
        y_1 = other_rec.__y
        width_r = other_rec.__width
        height_r = other_rec.__height
        turtle.penup()
        turtle.goto(self.__x, self.__y)
        turtle.dot(6, "blue")
        turtle.goto(self.__x - self.__width / 2, self.__height / 2 + self.__y)
        turtle.pendown()
        turtle.forward(self.__width)
        turtle.right(90)
        turtle.forward(self.__height)
        turtle.right(90)
        turtle.forward(self.__width)
        turtle.right(90)
        turtle.forward(self.__height)

原来直接调用就可以不用额外做一个,来玩改一下之前Rectangle2D的类


class Rectangle2D:
    def __init__(self, x, y, width, height):
        self.__x = x
        self.__y = y
        self.__width = width
        self.__height = height

    def get_area(self):
        return self.__width * self.__height

    def get_perimeter(self):
        return (self.__width + self.__height) * 2


    def draw_rec1(self):
        turtle.penup()
        turtle.goto(self.__x, self.__y)
        turtle.dot(6, "blue")
        turtle.goto(self.__x-self.__width / 2 , self.__height / 2 + self.__y)
        turtle.pendown()
        turtle.forward(self.__width)
        turtle.right(90)
        turtle.forward(self.__height)
        turtle.right(90)
        turtle.forward(self.__width)
        turtle.right(90)
        turtle.forward(self.__height)
        turtle.hideturtle()
        turtle.done()

    def draw_rec2(self, other_rec):
        x_1 = other_rec.__x
        y_1 = other_rec.__y
        width_r = other_rec.__width
        height_r = other_rec.__height
        turtle.penup()
        turtle.goto(self.__x, self.__y)
        turtle.dot(6, "blue")
        turtle.goto(self.__x - self.__width / 2, self.__height / 2 + self.__y)
        turtle.pendown()
        turtle.forward(self.__width)
        turtle.right(90)
        turtle.forward(self.__height)
        turtle.right(90)
        turtle.forward(self.__width)
        turtle.right(90)
        turtle.forward(self.__height)


        turtle.penup()
        turtle.goto(x_1, y_1)
        turtle.dot(6, "yellow")
        turtle.goto(x_1 - width_r / 2, height_r / 2 + y_1)
        turtle.pendown()
        turtle.forward(width_r)
        turtle.right(90)
        turtle.forward(height_r)
        turtle.right(90)
        turtle.forward(width_r)
        turtle.right(90)
        turtle.forward(height_r)
        turtle.hideturtle()
        turtle.done()
可能有些啰嗦,但是大家看到了.对比第一章我是不是去掉了out_x之类的.原来我是可以直接通过私有函数将x,y,widht,height.调用的呀.

结果

请添加图片描述

水平问题rec2的位置没有设计好.下集再弄,明早上7点30瑜伽后弯,天呐.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

电饭叔

谢谢各位兄弟们的关注,谢谢破费

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值