简明Python教程学习笔记7

11、面向对象的编程

(1)简介

  略

(2)self

  假如你有一个类称为MyClass和这个类的一个实例MyObject

  当你调用这个对象的方法MyObject.method(arg1, arg2)的时候,

  这会由Python自动转为MyClass.method(MyObject, arg1, arg2)——这就是self的原理了。

(3)类

  略

(4)对象的方法

  略

(5)__init__方法

  略

(6)类与对象的变量

  类的变量 由一个类的所有对象(实例)共享使用。只有一个类变量的拷贝,所以当某个对象对类的变量做了改动的时候,这个改动会反映到所有其他的实例上。

  对象的变量 由类的每个对象/实例拥有。因此每个对象有自己对这个域的一份拷贝,即它们不是共享的,在同一个类的不同实例中,虽然对象的变量有相同的名称,但是是互不相关的。

 1 # coding=utf-8
 2 class Person(object):
 3     """Represents a person."""
 4     population = 0  # 类变量
 5 
 6     def __init__(self, name):
 7         """Initializes the person's data."""
 8         self.name = name  # 实例变量
 9         print "(Initializing %s)" % self.name
10 
11         # When this person is created, he/she
12         # adds to the population
13         Person.population += 1
14 
15     def __del__(self):
16         """I am dying."""
17         print "%s says bye." % self.name
18 
19         Person.population -= 1
20 
21         if Person.population == 0:
22             print "I am the last one."
23         else:
24             print "There are still %d people left." % Person.population
25 
26     def say_hi(self):
27         """Greeting by the person.
28         
29         Really, that's all it does."""
30         print "Hi, my name is %s." % self.name
31 
32     def how_many(self):
33         """Prints the current population."""
34         if Person.population == 1:
35             print "I am the only person here."
36         else:
37             print "We havve %d persons here." % Person.population
38 
39 
40 if __name__ == "__main__":
41     print "*" * 20 + "Swaroop Begin" + "*" * 20
42     swaroop = Person("Swaroop")
43     swaroop.say_hi()
44     swaroop.how_many()
45 
46     print "*" * 20 + "Kalam Begin" + "*" * 20
47     kalam = Person("Abdul Kalam")
48     kalam.say_hi()
49     kalam.how_many()
50 
51     print "*" * 20 + "say_hi && how_many" + "*" * 20
52     swaroop.say_hi()
53     swaroop.how_many()
54 
55     print "*" * 20 + "End" + "*" * 20
对象方法

输出:

(7)继承

 1 # -*- coding:utf-8 -*-
 2 
 3 
 4 class SchoolMember(object):
 5     """Represents any school member."""
 6 
 7     def __init__(self, name, age):
 8         self.name = name
 9         self.age = age
10         print "(Initialized SchoolMember:%s)" % self.name
11 
12     def tell(self):
13         """Tell my details."""
14         print "Name:\"%s\" Age:\"%s\"" % (self.name, self.age)
15 
16 
17 class Teacher(SchoolMember):
18     """Represents a teacher."""
19 
20     def __init__(self, name, age, salary):
21         SchoolMember.__init__(self, name, age)  # 调用父类的初始化
22         self.salary = salary
23         print "(Initialized Teacher:%s" % self.name
24 
25     def tell(self):
26         SchoolMember.tell(self)  # 通过类调用父类的方法,不需要创建对象
27         print "Salary:\"%d\"" % self.salary
28 
29 
30 class Student(SchoolMember):
31     """Represents a student."""
32 
33     def __init__(self, name, age, marks):
34         SchoolMember.__init__(self, name, age)
35         self.marks = marks
36         print "(Initialized Student:%s)" % self.name
37 
38     def tell(self):
39         SchoolMember.tell(self)
40         print "Marks:\"%d\"" % self.marks
41 
42 
43 if __name__ == "__main__":
44     print "**" * 20 + "Teacher" + "**" * 20
45     t = Teacher("Mrs.Shrividya", 40, 30000)
46 
47     print "**" * 20 + "Student" + "**" * 20
48     s = Student("Swaroop", 22, 75)
49 
50     print "**" * 44
51     members = [t, s]
52     for member in members:
53         member.tell()
继承

输出:

 

posted @ 2018-01-23 09:47  七甲八甲  阅读(240)  评论(0编辑  收藏  举报