Python和Java不同的地方
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
01 | class Person: |
02 | '''Represents a person.''' |
03 | population = 0 |
04 | def __init__( self , name): |
05 | '''Initializes the person's data.''' |
06 | self .name = name |
07 | print '(Initializing %s)' % self .name |
08 | # When this person is created, he/she |
09 | # adds to the population |
10 | Person.population + = 1 |
11 | |
12 | def __del__( self ): |
13 | '''I am dying.''' |
14 | print '%s says bye.' % self .name |
15 | Person.population - = 1 |
16 | if Person.population = = 0 : |
17 | print 'I am the last one.' |
18 | else : |
19 | print 'There are still %d people left.' % Person.population |
20 | |
21 | def sayHi( self ): |
22 | '''Greeting by the person. |
23 | Really, that's all it does.''' |
24 | print 'Hi, my name is %s.' % self .name |
25 | |
26 | def howMany( self ): |
27 | '''Prints the current population.''' |
28 | if Person.population = = 1 : |
29 | print 'I am the only person here.' |
30 | else : |
31 | print 'We have %d persons here.' % Person.population |
评论暂时关闭