首页 新闻 会员 周边

用python定义青蛙

0
[已解决问题] 解决于 2020-06-11 19:20

在python中定义青蛙类,包括绿颜色和大嘴巴的属性,呱呱叫的方法和吃害虫的方法,并创建对象分别调用属性和方法。用def函数做,至今没有头绪

沐兔兔的主页 沐兔兔 | 菜鸟二级 | 园豆:202
提问于:2020-06-11 18:08
< >
分享
最佳答案
1

别忘了结贴,选我为最佳答案

class Frog(object):
    def __init__(self, color: str, mouth: str):
        self.__color = color
        self.__mouth = mouth
    
    @property
    def color(self) -> str:
        return self.__color

    @color.setter
    def color(self, color: str):
        self.__color = color

    @property
    def mouth(self) -> str:
        return self.__mouth

    @mouth.setter
    def mouth(self, mouth: str):
        self.__mouth = mouth

    def talk(self):
        print("I'm " + self.__color + " and have a " + self.__mouth + " mouth")
    
    def eat(self, bug: str):
        print(bug + " is so good")


if __name__ == "__main__":
    frog = Frog('green', 'big')

    print(frog.color)
    print(frog.mouth)

    frog.talk()
    frog.eat('moth')
奖励园豆:5
会长 | 专家六级 |园豆:12401 | 2020-06-11 18:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册