python-物件、私有屬性_方法、繼承(多重繼承)、重寫
阿新 • • 發佈:2019-02-05
物件
#!/usr/bin/python
#coding:utf-8
class 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.weight
print xiaoGou.age
print xiaoGou.name
#新增屬性
xiaoGou.color = "red"
print xiaoGou.color
xiaoGou.color = "green"
print xiaoGou.color
xiaoGou.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.name
print person
輸出如下:
E:\python\python_jdk\python.exe E:/python/py_pro/safly/Python_Demo.py
1
1.5
gou
red
green
4
------------
person
哈哈。。。
Process finished with exit code 0
私有屬性
#!/usr/bin/python
#coding:utf-8
class 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.name
person.setNewAge(19)
print person.getNewAge()
personTemp = person
print 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.py
person
19
True
--NameError: name 'person' is not defined-----
Process finished with exit code 0
繼承、私有方法
#!/usr/bin/python
#coding:utf-8
class 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 = 1
person = 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.py
111
11
-----XiaoMing' object has no attribute '__age'-----
Process finished with exit code 0
重寫方法
#!/usr/bin/python
#coding:utf-8
class 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 run
animal run
dog run
Process finished with exit code 0
多重繼承
#!/usr/bin/python
#coding:utf-8
class 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):
pass
class D(B,A):
pass
c = 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