24小时热门版块排行榜    

查看: 2376  |  回复: 15

yjcmwgk

禁虫 (文坛精英)

密度泛函·小卒

优秀版主

[交流] 祝贺自己 终于自己成功编写了一个class 从此跨入面向对象的程序设计的第一道门槛 已有14人参与

看了整整一天class,终于编写了第一个能成功运行的class,一只小企鹅(penguin)。
祝贺自己从此跨入面向对象的程序设计的第一道门槛
我和小企鹅的互动,好萌的
先定义了鸟类(bird),包括吃饭(eat),喝水(drink),起飞(takeoff),落地(landing),唱歌(sing),消化(digest)
然后用小企鹅(penguin)继承了鸟类(bird),同时在penguin类中重新定义了起飞(takeoff被改写,只剩下“企鹅不会飞”的一句话了),重新定义了落地(landing被改写,改成从海里到陆地上,而不是从空中到陆地上),增加了游泳(swim)。
然后我就开始跟我的企鹅互动了。感觉很有成就感呢
CODE:
>>> p=penguin()
>>> p.digest()
Our penguin is still waiting for your care.
>>> p.takeoff()
I am a penguin. I cannot fly.
>>> p.swim()
I am too hungry to swim. Please run bird.eat()
>>> p.eat()
I am eating! Aaaaaaaaaaah~
>>> p.eat()
I am full! I don not want to eat anything!
>>> p.swim()
I am too thirsty to swim. Please run bird.drink()
>>> p.drink()
I am drinking water. Gu lu, Gu lu, Gu lu~
>>> p.drink()
Too much water! Now I do not need water.
>>> p.swim()
I am ready to swim! Jump to sea now! Gu Gu Gu~
>>> p.digest()
After two hours swim, our lovely penguin gets hungry and thirsty.
Oh! I am too hungry to swim. I am going back to the land.
>>> p.eat()
I am eating! Aaaaaaaaaaah~
>>> p.drink()
I am drinking water. Gu lu, Gu lu, Gu lu~
>>> p.digest()
After four hours waiting, our lovely penguin gets hungry and thirsty.
>>> p.sing()
Squawk! Squawk! Squawk!
>>>

类的定义
CODE:
>>> class bird:
         def __init__(self):
                 self.hungry = True
                 self.thirsty = True
                 self.insky = False
         def eat(self):
                 if (self.hungry == True):
                         print "I am eating! Aaaaaaaaaaah~"
                         self.hungry = False
                 else:
                         print "I am full! I don not want to eat anything!"
         def drink(self):
                 if (self.thirsty == True):
                         print "I am drinking water. Gu lu, Gu lu, Gu lu~"
                         self.thirsty = False
                 else:
                         print "Too much water! Now I do not need water."
         def takeoff(self):
                 if (self.hungry == True):
                         print "I am too hungry to fly. Please run bird.eat()"
                 elif (self.thirsty == True):
                         print "I am too thirsty to fly. Please run bird.drink()"
                 else:
                         if(self.insky == False):
                                 print "I am ready to fly! Takeoff now! Pu pu pu pu ~"
                                 self.insky = True
                         else:
                                 print "I am already in the sky! Wow!"
         def landing(self):
                 if (self.insky == True):
                         print "I am tired. Now I am going to land."
                         self.insky = False
                 else:
                         print "I have already on the land."
         def sing(self):
                 print "Squawk! Squawk! Squawk!"
         def digest(self):
                 if ((self.hungry == False) and (self.insky==True)) or ((self.thirsty == False) and (self.insky==True)):
                         self.hungry = True
                         self.thirsty = True
                         self.insky = False
                         print "After two hours flight, our lovely bird gets hungry and thirsty."
                         print "Oh! I am too hungry to fly. I am going back to the land."
                 elif ((self.hungry == False) and (self.insky==False)) or ((self.thirsty == False) and (self.insky==False)):
                         self.hungry = True
                         self.thirsty = True
                         print "After four hours waiting, our lovely bird gets hungry and thirsty."
                 elif ((self.hungry == True)  or (self.thirsty == True)):
                         print "Our bird is still waiting for your care."

                        
>>> class penguin(bird):
        def __init__(self):
                bird.__init__(self)
                self.insea = False
        def takeoff(self):
                print "I am a penguin. I cannot fly."
        def swim(self):
                 if (self.hungry == True):
                         print "I am too hungry to swim. Please run bird.eat()"
                 elif (self.thirsty == True):
                         print "I am too thirsty to swim. Please run bird.drink()"
                 else:
                         if(self.insea == False):
                                 print "I am ready to swim! Jump to sea now! Gu Gu Gu~"
                                 self.insea = True
                         else:
                                 print "I am already in the sea! Wow!"
        def landing(self):
                 if (self.insea == True):
                         print "I am tired. Now I am going to land."
                         self.insea = False
                 else:
                         print "I have already on the land."
        def digest(self):
                 if ((self.hungry == False) and (self.insea==True)) or ((self.thirsty == False) and (self.insea==True)):
                         self.hungry = True
                         self.thirsty = True
                         self.insea = False
                         print "After two hours swim, our lovely penguin gets hungry and thirsty."
                         print "Oh! I am too hungry to swim. I am going back to the land."
                 elif ((self.hungry == False) and (self.insea==False)) or ((self.thirsty == False) and (self.insea==False)):
                         self.hungry = True
                         self.thirsty = True
                         print "After four hours waiting, our lovely bird gets hungry and thirsty."
                 elif ((self.hungry == True)  or (self.thirsty == True)):
                         print "Our penguin is still waiting for your care."

                        



[ Last edited by yjcmwgk on 2012-6-22 at 07:55 ]
回复此楼
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

ktkh

铁杆木虫 (知名作家)

青椒一百五十斤


dubo: 金币+1, 欢迎讨论 2012-06-22 21:55:28
很强悍。我不懂。呵呵
海阔天空
2楼2012-06-21 21:44:36
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

dubo: 金币+1, 欢迎讨论 2012-06-22 21:55:31
3楼2012-06-21 21:59:38
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

dubo: 金币+1, 欢迎讨论 2012-06-22 21:55:34
4楼2012-06-21 23:08:16
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

libralibra

至尊木虫 (著名写手)

骠骑将军


dubo: 金币+1, 欢迎讨论 2012-06-22 21:55:39
哈哈,很好玩,顶了
matlab/VB/python/c++/Java写程序请发QQ邮件:790404545@qq.com
5楼2012-06-21 23:11:28
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

longliuly

至尊木虫 (正式写手)


dubo: 金币+1, 欢迎讨论 2012-06-22 21:55:44
祝贺 努力中
喜欢的话,去做就行!
6楼2012-06-22 00:00:24
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

typho0n

金虫 (正式写手)


dubo: 金币+1, 欢迎讨论 2012-06-22 21:55:48
楼主好萌,

[ 发自手机版 http://muchong.com/3g ]
7楼2012-06-22 00:46:54
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

zhaozhh02

铜虫 (小有名气)


dubo: 金币+1, 欢迎讨论 2012-06-22 21:55:52
很有意思哈哈
8楼2012-06-22 06:38:27
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

yjcmwgk

禁虫 (文坛精英)

密度泛函·小卒

优秀版主

引用回帖:
8楼: Originally posted by zhaozhh02 at 2012-06-22 06:38:27
很有意思哈哈

谢谢
9楼2012-06-22 11:10:43
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

steincat

荣誉版主 (知名作家)

天啊,小木虫


dubo: 金币+1, 欢迎讨论 2012-06-22 21:55:57
祝贺
Lifeisdifficult.
10楼2012-06-22 16:14:28
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 yjcmwgk 的主题更新
普通表情 高级回复 (可上传附件)
信息提示
请填处理意见