標籤:lin tee pytho method sel 來源 www test cti
2018/05/ 28
[來源:菜鳥教程](http://www.runoob.com/python3/python3-examples.html)
繼續敲類相關的代碼
#No.1class people: name = ‘‘ age = 0 __weight = 0 def __init__(self, n, a, w): self.name = n self.age = a self.__weight = w def speak(self): print("%s 說:我 %d 歲。" %(self.name, self.age))class student(people): grade = ‘‘ def __init__(self, n, a, w, g): people.__init__(self, n, a, w) self.grade = g def speak(self): print("%s 說:我 %d 歲了,我在讀 %d 年紀" %(self.name, self.age, self.grade))s = student(‘ken‘, 10, 60, 3)s.speak()
resut:d:\fly\Python (master -> origin)λ python test.pyken 說:我 10 歲了,我在讀 3 年紀
#No.2class people: name = ‘‘ age = 0 __weight = 0 def __init__(self, n, a, w): self.name = n self.age = a self.__weight = w def speak(self): print("%s 說:我 %d 歲。" %(self.name, self.age))class student(people): grade = ‘‘ def __init__(self, n, a, w, g): people.__init__(self, n, a, w) self.grade = g def speak(self): print("%s 說: 我 %d 歲了,我在讀 %d 年級"%(self.name, self.age, self.grade))class speaker(): topic = ‘‘ name = ‘‘ def __init__(self, n, t): self.name = n self.topic = t def speak(self): print("我叫 %s, 我是一個演說家, 我演講的主題是 %s" %(self.name, self.topic))class sample(speaker, student): a = ‘‘ def __init__(self, n, a, w, g, t): student.__init__(self, n, a, w, g) speaker.__init__(self, n, t)test = sample("Tim", 25, 80, 4, "Python")test.speak()
resut:d:\fly\Python (master -> origin)λ python test.py我叫 Tim, 我是一個演說家, 我演講的主題是 Python
#No.3class parent: def myMethod(self): print(‘調用父類方法‘)class child(parent): def myMethod(self): print(‘調用子類方法‘)c = child()c.myMethod()super(child, c).myMethod()
resut:d:\fly\Python (master -> origin)λ python test.py調用子類方法調用父類方法
#No.4class JustCounter: __secretCount = 0 publicCount = 0 def count(self): self.__secretCount += 1 self.publicCount += 1 print(self.__secretCount)counter = JustCounter()counter.count()counter.count()print(counter.publicCount)print(counter.__secretCount)
resut:d:\fly\Python (master -> origin)λ python test.py122Traceback (most recent call last): File "test.py", line 14, in <module> print(counter.__secretCount)AttributeError: ‘JustCounter‘ object has no attribute ‘__secretCount‘
#No.5class Site: def __init__(self, name, url): self.name = name self.__url = url def who(self): print(‘name : ‘, self.name) print(‘url : ‘, self.__url) def __foo(self): print("這是私人方法") def foo(self): print(‘這是公用方法‘) self.__foo()x = Site(‘菜鳥教程‘, ‘www.runoob.com‘)x.who()x.foo()x.__foo()
resut:d:\fly\Python (master -> origin)λ python test.pyname : 菜鳥教程url : www.runoob.com這是公用方法這是私人方法Traceback (most recent call last): File "test.py", line 20, in <module> x.__foo()AttributeError: ‘Site‘ object has no attribute ‘__foo‘
【堅持】Selenium+Python學習記錄 DAY8