| ���� Python �̳� | ��11�� �������ı�� | |
|---|---|---|
| ��һҳ | �������ķ��� | ��һҳ |
�����Ѿ��������������Ĺ��ܲ��֣�������������һ���������ݲ��֡���ʵ�ϣ�����ֻ������Ͷ�������ƿռ� �� ����ͨ����������Щ����ֻ����Щ��������ǰ������Ч��
���������͵� �� ������ı����Ͷ���ı��������Ǹ�������Ƕ��� ӵ�� ������������֡�
��ı��� ��һ��������ж���ʵ��������ʹ�á�ֻ��һ��������Ŀ��������Ե�ij���������ı������˸Ķ���ʱ������Ķ��ᷴӳ������������ʵ���ϡ�
����ı��� �����ÿ������/ʵ��ӵ�С����ÿ���������Լ���������һ�ݿ����������Dz��ǹ����ģ���ͬһ����IJ�ͬʵ���У���Ȼ����ı�������ͬ�����ƣ������ǻ�����صġ�ͨ��һ�����ӻ�ʹ����������⡣
#!/usr/bin/python
# Filename: objvar.py
class Person:
'''Represents a person.'''
population = 0
def __init__(self, name):
'''Initializes the person's data.'''
self.name = name
print '(Initializing %s)' % self.name
# When this person is created, he/she
# adds to the population
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()
��Դ�ļ���code/objvar.py��
$ 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.
����һ���ܳ������ӣ�������������˵���������ı����ı��ʡ����population����Person�࣬�����һ����ı�����name�������ڶ�����ʹ��self��ֵ������Ƕ���ı�����
�۲���Է���__init__������һ����������ʼ��Personʵ��������������У�������population����1��������Ϊ����������һ���ˡ�ͬ�����Է��֣�self.name��ֵ����ÿ������ָ���������������Ϊ����ı����ı��ʡ�
��ס����ֻ��ʹ��self�������ο�ͬһ������ı����ͷ������ⱻ��Ϊ ���Բο� ��
����������У����ǻ�����docstring������ͷ���ͬ�����á����ǿ���������ʱʹ��Person.__doc__��Person.sayHi.__doc__���ֱ�������뷽�����ĵ��ַ�����
����ͬ__init__����һ��������һ������ķ���__del__�����ڶ������ŵ�ʱ���á��������ż������ٱ�ʹ�ã�����ռ�õ��ڴ潫���ظ�ϵͳ�����á�������������棬����ֻ�Ǽذ�Person.population��1��
�������ٱ�ʹ��ʱ��__del__�������У����Ǻ��ѱ�֤������������� ʲôʱ�� ���С��������Ҫָ���������У���͵�ʹ��del��䣬����ͬ��������ǰ��������ʹ�õ�������
��C++/Java/C#����Ա��ע��
Python�����е����Ա���������ݳ�Ա������ ������ �����еķ������� ��Ч�� ��
ֻ��һ�����⣺�����ʹ�õ����ݳ�Ա������ ˫�»���ǰ ����__privatevar��Python�����ƹ�����ϵ����Ч�ذ�����Ϊ˽�б�����
��������һ�����������ij������ֻ������������ʹ�ã���Ӧ���Ե��»���ǰ�������������ƶ�����Ϊ�����ģ����Ա�������/����ʹ�á���ס��ֻ��һ��������������Python��Ҫ��ģ���˫�»���ǰ��ͬ����
ͬ����ע��__del__������ destructor �ĸ������ơ�
| ��һҳ | ��һ�� | ��һҳ |
|---|---|---|
| __init__���� | ��ҳ | �̳� |