python和java不同的地方

粗略看了下a byte of python,总结一下大致与java不同的地方:

1、没有{},全部使用:和缩进代替

2、注释使用#

3、doc使用'''或者"""

4、变量类型比较简单,类似于js,不过定义更像vb或者groovy

方法使用def来定义,只有整形,长整形,复数,浮点4种类型的数

5、比java更是一切都是对象,连int也是

6、输出更像是c

7、数组,list,map比较像js

8、有 this,改叫self了,而且还必须手动传,必须在第一个。。。

9、构造叫init,回收叫del

10、继承使用(),需要手动地调用父类的构造

 

随便放段code

 

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
posted @ 2010-05-08 17:24  雪霁霜飞  阅读(418)  评论(0编辑  收藏  举报