易截截图软件、单文件、免安装、纯绿色、仅160KB
热门标签: c c# c++ asp asp.net linux php jsp java vb Python Ruby mysql sql access Sqlite sqlserver delphi javascript Oracle ajax wap mssql html css flash flex dreamweaver xml
 最新文章 : Python

Python入门的36个例子 之 29

源代码下载:下载地址在这里
# 033
class Person:
age = 22
def sayHello(self): # 方法与函数的区别在于需要一个self参数
print 'Hello!'
# end of def
# end of class # 这是我良好的编程风格
p = Person()
print p.age
p.sayHello()

output:
22
Hello! ......

Python入门的36个例子 之 30

源代码下载:下载地址在这里
# 034
class Person:
def __init__(self, name):
self.name = name # 这一句中第一个name是类中包含的域,第二个name是传进来的参数
# end of def
def sayHello(self):
print 'Hello!'
# end of def
# end of class
p = Person('Ning')
print p.name
p.sayHello()
output:
Ning
Hello! ......

Python入门的36个例子 之 31

源代码下载:下载地址在这里
# 035
class Person:
population = 0 #这个变量是属于整个类的
def __init__(self, name):
self.name = name
print '初始化 %s' % self.name
Person.population += 1
# end of def
def __del__(self):
print '%s says bye.' % self.name
Person.population -= 1
if Person.population == 0:
print '我是最后一个人了!'
else:
print '还有%d个人。' % Person.population
# end of if
# end of def
def sayHi(self):
print 'Hi, my name is %s' % self.name
# end of def
def howMany(self):
print Person.population
# end of def
# enf of class
ning = Person('Ning')
ning.sayHi()
ning.howMany()
#zhang = Person('Zhang')
#zhang.sayHi()
#zhang.howMany()
#ning.sayHi()
#ning.howMany()

output:
注意!下面是两次运行的结果,可以看出del(析构函数)的运行时间是不确定的。这个现象的原因在于Python的垃圾回收机制的自动回收时间是不确定的。
初始 ......

Python入门的36个例子 之 32

源代码下载:下载地址在这里
A Byte Of Python
中关于继承有这样的文字:
Suppose you want to write a program which has to keep track of the
teachers and students in a college. They have some common
characteristics such as name, age and address. They also have specific
characteristics such as salary, courses and leaves for teachers and,
marks and fees for students.
You can create two independent classes for each type and process them
but adding a new common characteristic would mean adding to both of
these independent classes. This quickly becomes unwieldy.
A better way would be to create a common class called SchoolMember and
then have the teacher and student classes inherit from this class i.e.
they will become sub-types of this type (class) and then we can add
specific characteristics to these sub-types.
这段文字很简单,我开始也这么认为。但当我不小心第二次读这本书的时候才领会到这些文字所要传达的深意。
继承在我看来,或者说在以前的我看来,是为了代码重用,是的,我现在也这么 ......

Python入门的36个例子 之 33

源代码下载:下载地址在这里
# 037
fileName = ''
while 1:
fileName = raw_input("Input a file name:")
if fileName == 'q':
break
try:
f = file(fileName, 'r')
print 'Opened a file.'
except:
print 'There is no file named', fileName
# end of try and except
f.close()
# end of while

output:
Input a file name:haha
There is no file named haha
Input a file name:txt
There is no file named txt
Input a file name:1
There is no file named 1
Input a file name:037_Exception.py
Opened a file.
Input a file name:q ......

Python入门的36个例子 之 34

源代码下载:下载地址在这里
raise有两个参数,第一个是由我们自己定义的异常类型,第二个是关于此异常的少量说明信息。
# 038
def getAge():
age = input('Input your age:')
if (age < 0 or age > 160):
raise 'BadAgeError', 'It is impossible!!!!!'
# end of if
return age
# end of def
print getAge()

output:
以下是两次运行的结构
>>>
Input your age:22
22
>>>
Input your age:911
Traceback (most recent call last):
  File "C:/Documents and Settings/ning/桌面/Python Intro/038_RaiseAnError.py", line 11, in <module>
    print getAge()
  File "C:/Documents and Settings/ning/桌面/Python Intro/038_RaiseAnError.py", line 6, in getAge
    raise 'BadAgeError', 'It is impossible!!!!!'
TypeError: exceptions must be classes or instances, not str
>>> ......
总记录数:695; 总页数:116; 每页6 条; 首页 上一页 [101] [102] [103] [104] 105 [106] [107] [108] [109] [110]  下一页 尾页
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号