1
2
3
4
5
6
7
8
9
10
11
|
程序代码如下:
class
dog: #定义一个类
def name(self): #定义类的方法
print
"Hello master, my name is Python."
D = dog() #类的实例化,产生类的对象,如果不实例化,将无法访问该类
D.name() #使用类的方法
执行情况如下:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day4$ python myclass4.py
Hello master, my name
is
Python.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
程序代码如下:
class
Dog:
class_object =
'food'
#这是一个类变量
def sayHi(self):
print
'Hi master , I am your little dog, who do you want me to bite...'
favorate_food =
'bone'
self.FavorFood = favorate_food #将favorate_food变为全局变量,让其它方法可以引用
def eat(self, food_type):
if
food_type == self.FavorFood: #不同类方法之间的变量调用,需要通过类本身
print
'I like it very much..thanks'
else
:
print
'Do not give me this bull shit...'
d = Dog()
d.sayHi()
''
'如果不调用sayHi(),则无法将favorrate_food变成一个类变量,所谓类变量,
即是类下定义的第一级变量
''
'
d.eat(
'bone'
)
执行情况如下:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day4$ python class7.py
Hi master , I am your little dog, who
do
you want me to bite...
I like it very much..thanks
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
程序代码如下:
class
Dog:
class_object =
'food'
def sayHi(self):
print
'Hi master , I am your little dog, who do you want me to bite...'
favorate_food =
'bone'
def eat(self, food_type):
if
food_type == self.sayHi().favorate_food: #这样使用是不行的
print
'I like it very much..thanks'
else
:
print
'Do not give me this bull shit...'
d = Dog()
d.sayHi()
d.eat(
'bone'
)
执行情况如下:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day4$ python class7.py
Hi master , I am your little dog, who
do
you want me to bite...
Hi master , I am your little dog, who
do
you want me to bite...
Traceback (most recent call last):
File
"class7.py"
, line
16
,
in
<module>
d.eat(
'bone'
)
File
"class7.py"
, line
9
,
in
eat
if
food_type == self.sayHi().favorate_food:
AttributeError:
'NoneType'
object has no attribute
'favorate_food'
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
程序代码如下:
class
Person:
def __init__(self,name,age): #构造函数,只要把类实例化,就会执行此函数
print
'I am being called right now...'
self.Name = name
self.Age = age
def sayHi(self): #类的普通方法
print
'Hi my name is %s, i am %s years old'
% (self.Name,self.Age)
def __del__(self): #解构函数,当实例在内存中释放时,才会执行此函数
print
'I got killed just now...bye...'
, self.Name
p = Person(
'Alex'
,
29
)
p.sayHi()
#del p
print
'*'
*
60
p2 = Person(
'Jack'
,
40
)
p2.sayHi()
#del p2
执行情况如下:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day4$ python person_class5.py
I am being called right now...
Hi my name
is
Alex, i am
29
years old
************************************************************
I am being called right now...
Hi my name
is
Jack, i am
40
years old
I got killed just now...bye... Jack
I got killed just now...bye... Alex
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
程序代码如下:
class
Person:
def __init__(self,name,age):
print
'I am being called right now...'
self.Name = name
self.Age = age
def sayHi(self):
print
'Hi my name is %s, i am %s years old'
% (self.Name,self.Age)
def __del__(self):
print
'I got killed just now...bye...'
, self.Name
p = Person(
'Alex'
,
29
)
p.sayHi()
del p #其它不变,只是在做类的另一个实例前,删除该实例
print
'*'
*
60
p2 = Person(
'Jack'
,
40
)
p2.sayHi()
#del p2
执行情况如下:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day4$ python person_class5.py
I am being called right now...
Hi my name
is
Alex, i am
29
years old
I got killed just now...bye... Alex
************************************************************
I am being called right now...
Hi my name
is
Jack, i am
40
years old
I got killed just now...bye... Jack
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
程序代码如下:
class
Person:
def __init__(self,name,age):
print
'I am being called right now...'
self.Name = name
self.Age = age
def sayHi(self):
print
'Hi my name is %s, i am %s years old'
% (self.Name,self.Age)
self.__talk() #可以引用类中的私有属性
def __talk(self): #私有属性的定义方法
print
"I'm private..."
p=Person(
'Alex'
,
29
)
p.sayHi()
执行情况如下:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day4$ python private6.py
I am being called right now...
Hi my name
is
Alex, i am
29
years old
I'm
private
...
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
程序代码如下:
class
Person:
def __init__(self,name,age):
print
'I am being called right now...'
self.Name = name
self.Age = age
def sayHi(self):
print
'Hi my name is %s, i am %s years old'
% (self.Name,self.Age)
self.__talk()
def __talk(self):
print
"I'm private..."
p=Person(
'Alex'
,
29
)
p.sayHi()
p.__talk() #在类外通过普通方式调用类的私有属性
执行情况如下:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day4$ python private6.py
I am being called right now...
Hi my name
is
Alex, i am
29
years old
I'm
private
...
Traceback (most recent call last):
File
"private6.py"
, line
17
,
in
<module>
p.__talk()
AttributeError: Person instance has no attribute
'__talk'
#会出现找不到类方法的错误提示
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
程序代码:
class
Person:
def __init__(self,name,age):
print
'I am being called right now...'
self.Name = name
self.Age = age
def sayHi(self):
print
'Hi my name is %s, i am %s years old'
% (self.Name,self.Age)
self.__talk()
def __talk(self):
print
"I'm private..."
p=Person(
'Alex'
,
29
)
p.sayHi()
print
'*'
*
30
p._Person__talk()
执行情况如下:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day4$ python private6.py
I am being called right now...
Hi my name
is
Alex, i am
29
years old
I'm
private
...
******************************
I'm
private
...
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
程序代码如下:
class
person: #父类
def tell(self, name):
print
'hi my name is'
, name
class
student(person): #子类,继承父类
def study(sefl):
print
'I am studying Py right now.'
s = student()
s.study()
s.tell(
'MengFanHao'
) #子类继承父类后便可以直接调用父类的方法
#在有多个需要同时执行相同功能的子类情况下,使用类的继承可以节省代码空间
执行情况如下:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day4$ python cla_with_no_arg9.py
I am studying Py right now.
hi my name
is
MengFanHao
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
class
SchoolMember: #父类,学校成员
school_name =
'Oldboy Linux edu.'
#第一级变量,即类属性
def __init__(self,name,gender,nationality =
'CN'
): #构造函数
self.name = name
self.gender = gender
self.nation = nationality
def tell(self): #普通的类方法
print
'Hi, my name is %s , I am from %s'
% (self.name,self.nation)
class
Student(SchoolMember): #子类,学生,继承父类学校成员的相关属性
def __init__(self, Name, Gender, Class, Score, Nation =
'US'
): #子类下的方法
SchoolMember.__init__(self, Name, Gender, Nation) #让父类使用子类传递过去的参数
self.Class = Class
self.Score = Score
def payTuition(self, amount): #子类下的方法
if
amount <
6499
:
print
'Get the fuck off...'
else
:
print
'Welcome onboard!'
class
Teacher(SchoolMember): #子类,老师,继承父类学校成员的相关属性
def __init__(self, Name, Gender, Course, Salary, Nation =
'FR'
):
SchoolMember.__init__(self, Name, Gender, Nation)
self.course = Course
self.Salary = Salary
def teaching(self):
print
'I am teaching %s, i am making %s per month !'
% (self.course, self.Salary)
S1 = Student(
'WangFanHao'
,
'Male'
,
'Python'
,
'C+'
,
'JP'
) #实例化一个子类对象,学生
S1.tell() #直接继承父类中的tell()方法
S1.payTuition(
4999
) #使用子类Student()自身中的类方法
print S1.school_name #直接继承父类的一个属性
print
'*'
*
60
S2 = Student(
'ShitTshirt'
,
'Male'
,
'Linux'
,
'B'
)
S2.tell()
S2.payTuition(
6500
)
#S2.age =
29
#print S2.age
print
'*'
*
60
T1 = Teacher(
'Alex'
,
'Male'
,
'C++'
,
5000
) #实例化一个子类对象,学生
T1.tell() #直接继承父类中的tell()方法
T1.teaching() #直接继承父类的一个属性
print
'S1.name:'
, S1.name #测试用,观察输出结果
print
'S2.name:'
, S2.name
print
'T1.name:'
, T1.name
执行情况如下:
xpleaf@xpleaf-machine:/mnt/hgfs/Python/day4$ python class_continue8.py
Hi, my name
is
WangFanHao , I am from JP
Get the fuck off...
Oldboy Linux edu.
************************************************************
Hi, my name
|