【坚持】Selenium+Python学习记录 DAY8

2018/05/ 28

[来源:菜鸟教程](http://www.runoob.com/python3/python3-examples.html)

继续敲类相关的代码

#No.1
class 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.py
ken 说:我 10 岁了,我在读 3 年纪
#No.2
class 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.3
class 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.4
class 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.py
1
2
2
Traceback (most recent call last):
File "test.py", line 14, in <module>
print(counter.__secretCount)
AttributeError: 'JustCounter' object has no attribute '__secretCount'
#No.5
class 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.py
name : 菜鸟教程
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'
上一篇:分类算法之贝叶斯(Bayes)分类器


下一篇:Python基础总结之初步认识---class类的继承(下)。第十五天开始(新手可相互督促