例1、构造函数
#-*-coding:utf-8-*- import sys class Student: def __init__(self,name,age): self.__name=name self.__age=age def getName(self): format="my name is %s my age is %d"%(self.__name,self.__age) print format def __del__(self): print "del" if __name__=="__main__": studeng=Student("chu",35) studeng.getName()
例二:静态成员与私有成员
#-*-coding:utf-8-*- import sys class A: y=2 def __init__(self): self.x=1 self.__z=1 if __name__=="__main__": a=A() print a.x print A.y print a.__z
例三:
#-*-coding:utf-8-*- import sys class A: def prt(self): print "my name is A" def reprt(self): A.prt(self) if __name__=="__main__": a=A() a.prt() a.reprt()
例四:字典的散列查找
#-*-coding:utf-8-*- import sys if __name__=="__main__": dict={"a":"apple","b":"banana","g":"grape","o":"orange"} print dict print dict["a"] dict2={1:"apple",2:"banana",3:"grape",4:"orange"} print dict2 print dict2[1]
时间: 2024-09-29 10:48:45