python-对象、私有属性_方法、继承(多重继承)、重写

来源:互联网 发布:电脑看片有什么软件 编辑:程序博客网 时间:2024/05/22 20:01

对象

#!/usr/bin/python#coding:utf-8class Dog:    weight = 5    age = 1.5    def __init__(self):        self.weight = 1        self.name = "gou"    def run(self):        self.weight = 4#构造对象xiaoGou = Dog()#调用属性print xiaoGou.weightprint xiaoGou.ageprint xiaoGou.name#添加属性xiaoGou.color = "red"print xiaoGou.colorxiaoGou.color = "green"print xiaoGou.colorxiaoGou.run()print xiaoGou.weight################print "------------"class Person:    # name = "xx"    def __init__(self,pName):        self.name = pName    def __str__(self):        return "哈哈。。。"person = Person("person")print person.nameprint person

输出如下:

E:\python\python_jdk\python.exe E:/python/py_pro/safly/Python_Demo.py11.5gouredgreen4------------person哈哈。。。Process finished with exit code 0

私有属性

#!/usr/bin/python#coding:utf-8class Person(object):    def __init__(self,name,age):        self.name = name        self.__age = age    def setNewAge(self,newAge):        self.__age = newAge    def getNewAge(self):        return  self.__age    def __str__(self):        return "年龄:"+str(self.__age)person = Person("person",11)print  person.nameperson.setNewAge(19)print person.getNewAge()personTemp = personprint id(person) == id(personTemp)print "--NameError: name 'person' is not defined-----"del person# print  person

输出如下:

E:\python\python_jdk\python.exe E:/python/py_pro/safly/SaflyDemo.pyperson19True--NameError: name 'person' is not defined-----Process finished with exit code 0

继承、私有方法

#!/usr/bin/python#coding:utf-8class Person(object):    def __init__(self,name,age):        self.name = name        self.__age = age    ####私有属性####    def setNewAge(self,newAge):        self.__age = newAge    ####私有方法####    def __getNewAge(self):        return  self.__age########调用私有方法#######    def getNewAge(self):        return self.__getNewAge()class XiaoMing(Person):    ########覆盖父类构造方法#######    def __init__(self):        self.sex = 1person = Person("person",111)#####调用非私有方法#######print person.getNewAge()xiaoming = XiaoMing()#####调用父类方法###xiaoming.setNewAge(11)print xiaoming.getNewAge()print "-----XiaoMing' object has no attribute '__age'-----"# print xiaoming.__age

输出如下:

E:\python\python_jdk\python.exe E:/python/py_pro/safly/SaflyDemo.py11111-----XiaoMing' object has no attribute '__age'-----Process finished with exit code 0

重写方法

#!/usr/bin/python#coding:utf-8class Animal(object):    def sing(self):        print "啊啊啊啊"    def run(self):        print "animal run"class Dog(Animal):    def sing(self):        print "汪汪汪"    def run(self):        Animal.run(self)        super(Dog, self).run()        # super().run()  3.xx的特性        print "dog run"dog = Dog()dog.sing()dog.run()

输出如下:

E:\python\python_jdk\python.exe E:/python/py_pro/safly/SaflyDemo.py汪汪汪animal runanimal rundog runProcess finished with exit code 0

多重继承

#!/usr/bin/python#coding:utf-8class A(object):    def testA(self):        print "---A a---"class B(object):    def testB(self):        print "---B b----"    def testA(self):        print "---B a---"class C(A,B):    passclass D(B,A):    passc = C()c.testA()c.testB()print "---------------"d = D()d.testA()d.testB()

输出如下:

E:\python\python_jdk\python.exe E:/python/py_pro/safly/SaflyDemo.py---A a------B b----------------------B a------B b----Process finished with exit code 0
原创粉丝点击