2018/05/28
[Source: Rookie Tutorial] (http://www.runoob.com/python3/python3-examples.html)
Continue knocking class-related code
#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" said: I'm%d years old.) "% (Self.name, self.age)) class student (people): Grade = ' Def __init__ (self, n, a, W, g): people.__init__ (SE LF, N, A, W) Self.grade = G def speak (self): print ("%s said: I'm%d years old, I'm reading grade%d"% (Self.name, self.age, SELF.G Rade)) class Speaker (): topic = "NAME =" Def __init__ (self, N, t): Self.name = n self.topic = t def speak (self): print ("My name is%s, I am an orator, I am speaking the subject is%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", +, 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‘
"Persist" Selenium+python study record DAY8