好记性不如铅笔头

python && jython, 编程

Python面向对象编程基础笔记

本笔记来源自【 http://sebug.net/paper/python/ch11.html#s01 】,有删改。

创建一个类以及类实例方法

类的方法与普通的函数只有一个特别的区别——它们必须有一个额外的第一个参数名称,但是在调用这个方法的时候你不为这个参数赋值,Python会提供这个值。这个特别的变量指对象本身,按照惯例它的名称是self。

class Person:
    def sayHi(self):
        print 'Hello, how are you?'

p = Person()
p.sayHi()

使用__init__方法及__del__方法

class Person:
    def __init__(self, name):
        self.name = name
    def sayHi(self):
        print 'Hello, my name is', self.name
    def __del__(self):
        pass;

p = Person('Swaroop')
p.sayHi()

类变量与成员变量

class Person:
	#类变量
    population = 0

#构造函数
    def __init__(self, name):
    	#成员变量以及调用方式
        self.name = name
        print '(Initializing %s)' % self.name
				
	#类变量的调用方式
        Person.population += 1
#析构函数
    def __del__(self):
        '''I am dying.'''
        print '%s says bye.' % self.name

        Person.population -= 1

        if Person.population == 0:
            print 'I am the last one.'
        else:
            print 'There are still %d people left.' % Person.population

    def sayHi(self):
        '''Greeting by the person.

        Really, that's all it does.'''
        print 'Hi, my name is %s.' % self.name

    def howMany(self):
        '''Prints the current population.'''
        if Person.population == 1:
            print 'I am the only person here.'
        else:
            print 'We have %d persons here.' % Person.population

swaroop = Person('Swaroop')
swaroop.sayHi()
swaroop.howMany()

kalam = Person('Abdul Kalam')
kalam.sayHi()
kalam.howMany()

swaroop.sayHi()
swaroop.howMany()

输出:

$ python objvar.py
(Initializing Swaroop)
Hi, my name is Swaroop.
I am the only person here.
(Initializing Abdul Kalam)
Hi, my name is Abdul Kalam.
We have 2 persons here.
Hi, my name is Swaroop.
We have 2 persons here.
Abdul Kalam says bye.
There are still 1 people left.
Swaroop says bye.
I am the last one.

访问权限

Python中所有的类成员(包括数据成员)都是公共的 ,所有的方法都是 有效的 。
只有一个例外:如果你使用的数据成员名称以 双下划线前缀 比如__privatevar,Python的名称管理体系会有效地把它作为私有变量。
这样就有一个惯例,如果某个变量只想在类或对象中使用,就应该以单下划线前缀。而其他的名称都将作为公共的,可以被其他类/对象使用。记住这只是一个惯例,并不是Python所要求的(与双下划线前缀不同)。

继承

class SchoolMember:
    def __init__(self, name, age):
        self.name = name
        self.age = age
        print '(Initialized SchoolMember: %s)' % self.name

    def tell(self):
        print 'Name:"%s" Age:"%s"' % (self.name, self.age),

#继承的语法
class Teacher(SchoolMember):
    def __init__(self, name, age, salary):
        #调用父类的构造函数
        SchoolMember.__init__(self, name, age)
        self.salary = salary
        print '(Initialized Teacher: %s)' % self.name

    def tell(self):
	    #重写父类的函数,也可以看到如何调用父类的函数
        SchoolMember.tell(self)
        print 'Salary: "%d"' % self.salary

class Student(SchoolMember):
    def __init__(self, name, age, marks):
        SchoolMember.__init__(self, name, age)
        self.marks = marks
        print '(Initialized Student: %s)' % self.name

    def tell(self):
        SchoolMember.tell(self)
        print 'Marks: "%d"' % self.marks

t = Teacher('Mrs. Shrividya', 40, 30000)
s = Student('Swaroop', 22, 75)

print # prints a blank line

members = [t, s]
for member in members:
    member.tell() # works for both Teachers and Students

多重继承及super调用

暂时用不到,随用随学吧。

http://www.cnblogs.com/lemoncolaz/p/3168574.html 】
http://blog.csdn.net/caz28/article/details/8270709
http://www.zhihu.com/question/20040039

Leave a Reply

20 − 12 =

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据