在python中定义青蛙类,包括绿颜色和大嘴巴的属性,呱呱叫的方法和吃害虫的方法,并创建对象分别调用属性和方法。用def函数做,至今没有头绪
别忘了结贴,选我为最佳答案
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')