相关疑难解决方法(0)

构造函数和析构函数如何工作?

我正在尝试理解这段代码:

class Person:
    '''Represents a person '''
    population = 0

    def __init__(self,name):
          //some statements and population += 1
    def __del__(self):
          //some statements and population -= 1 
    def sayHi(self):
        '''grettings from person'''
        print 'Hi My name is %s' % self.name

    def howMany(self):
        '''Prints the current population'''
        if Person.population == 1:
            print 'i am the only one here'
        else:
            print 'There are still %d guyz left ' % Person.population
rohan = Person('Rohan')
rohan.sayHi()
rohan.howMany()


sanju = Person('Sanjivi')
sanju.howMany()

del rohan # am …
Run Code Online (Sandbox Code Playgroud)

python destructor class

3
推荐指数
2
解决办法
8528
查看次数

标签 统计

class ×1

destructor ×1

python ×1