python学习1 使用类
#使用类
class CPerson:
#类变量好比C++中的静态成员变量
population = 0
def SayHi(self):
print('Hello World')
def HowMany(self):
if CPerson.population == 1:
print('I am the only person here.')
else:
print(('We have %d persons here.') % CPerson.population)
#类中有很方法的名字有特殊的意义
#__init__好比C++中的构造函数
def __init__(self, name):
self.name = name
print(('Initializing %s') % self.name)
CPerson.population += 1
#__del__好比C++中的析构函数
def __del__(self):
CPerson.population -= 1
if CPerson.population == 0:
print('I am the last one.')
else:
print(('There are still %d people left.') % CPerson.population)
p = CPerson('123456')
p.SayHi()
p.HowMany()
p0 = CPerson('987654321')
p0.SayHi()
p0.HowMany()
p.SayHi()
p.HowMany()
print('------------------------------------------')
#使用继承,多态现象
class SchoolMember:
def __init__(self, name ,age):
self.name = name
self.age = age
print(('Initialized SchoolMember:%s') % self.name)
def Tell(self):
print(('Name:%s, Age:%d') % (self.name, self.age))
class Teacher(SchoolMember):
def __init__(self, name, age, salary):
SchoolMember.__init__(self, name, age)
self.salary = salary
print(('Initialized Teacher:%s') % self.name)
def Tell(self):
SchoolMember.Tell(self)
print(('Salary:%d') % self.salary)
class Student(SchoolMember):
def __init__(self, name, age, marks):
SchoolMember.__init__(self, name, age)
self.marks = marks
print(('Initalized Student:%s') % self.name)
def Tell(self):
SchoolMember.Tell(self)
print(('Marks:%d') % self.marks)
t = Teacher('liyong', 30, 30000)
s = Student('swap', 22, 75)
members = [t, s]
for member in members:
print('######
相关文档:
python string和PyQt的QString的区别 以下在Python2.6和PyQt4.4.4 for
Python2,6环境下讨论: Python中有两种有关字符的类型:Python string object和Python Unicode
object。主要使用Python string object进行数据输入输出。 PyQt中与之相对应的字符有关类
python string和PyQt的QString的区别
以下在Python2.6和PyQt4 ......
多个执行线程经常要共享数据,如果仅仅读取共享数据还好,但是如果多个线程要修改共享数据的话就可能出现无法预料的结果。
假如两个线程对象t1
和t2
都要对数值num=0
进行增1运算,那么t1
和t2
都各对num
修改10
次的话,那么num
最终的结果应该为20
。但是如果当t1
取得num
的值时(假如此 ......
#shell排序
def ShellPass(mylist, d):
size = len(mylist)
i = d
while i < size:
if mylist[i] < mylist[i - d]:
tmp = mylist[i]
j = i - d
mylist[j + d] = mylist[j]
j = j - d
while j >= 0 and mylist[j] > ......
#!/usr/bin/env python
#coding=utf-8
def buildConnectionString(params):
return ":".join(["%s=%s" %(k, v) for k, v in params.items()])
if __name__ == "__main__":
myParams = {"server":"mpilgrim", \
&nbs ......