Java教程

面向对象的编程

本文主要是介绍面向对象的编程,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

初阶:

#面向对象的特征:封装,继承,多态

#初阶
class dog:
    def __init__(self,name):
        self.name = name
    def bulk(self):
        print("%s:wangwangwang!"%self.name)

d1 = dog("vergil")
d2 = dog("tanying")
d1.bulk()
d2.bulk()

#进阶
class role(object):
    def __init__(self,name,sex,age,job,money = 10000):
        self.name = name
        self.sex = sex
        self.age = age
        self.job = job
        self.money = money
    def buy(self,something):
        print("%s has bought a %s"%(self.name,something))
        print("%s has %s in his hand"%(self.name,self.money))
    def growth(self,newage):
        print("%s has growth from %s to %s"%(self.name,self.age,newage))
    def overview(self,high):
        print("%s is a %s,and he is %s high"%(self.name,self.sex,high))
    def drump(self,newjob):
        print("%s has changed job from %s to %s"%(self.name,self.job,newjob))

vergil = role("vergil","male",25,"IT")
vergil.buy("cake")
vergil.growth(26)
vergil.overview(175)
vergil.drump("Financial software test")

这篇关于面向对象的编程的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!