python简单学习:类、类与对象、继承、读取文件
python类的简单学习
class Person: #创建一个新类 pass #空白块 p=Person() #创建一个对象/实例 print (p) #类中包含方法,与函数的区别是有一个额外的self变量 class Person1: def __init__(self,name):#__init__方法在类的一个对象被建立时,马上运行,可以对对象做一些初始化。名称的开始和结尾都是双下划线 self.name=name; def sayHello(self): #sayoHello方法没有任何参数,仍仍在函数定义时有self print("Hello,come on!%s",self.name) p1=Person1("hello")#类似于构造函数 p1.sayHello()类与对象:有两种类型的域:类的对象和对象的变量,根据是类还是对象拥有这个变量而区分。
类的变量由一个类的所有对象(实例)共享使用,只有一个类变量的拷贝,当某个对象对类的变量做了改动时,这个改动会反应到其他实例中。
对象的变量是由类的每个对象/实例拥有,每个对象有自己对这个域的拷贝,但不是共享的。
self代表类的实例,而非类
类的方法与普通的函数只有一个特别的区别——它们必须有一个额外的第一个参数名称, 按照惯例它的名称是 self。
class Test: def prt(self): print(self) print(self.__class__) t = Test()t.prt()以上实例执行结果为:
<__main__.Test instance at 0x10d066878> __main__.Test
从执行结果可以很明显的看出,self 代表的是类的实例,代表当前对象的地址,而 self.class 则指向类。
self 不是 python 关键字,我们把他换成 runoob 也是可以正常执行的:
实例
class Test: def prt(runoob): print(runoob) print(runoob.__class__) t = Test()t.prt()以上实例执行结果为:
<__main__.Test instance at 0x10d066878> __main__.Test
#类与对象 class Person: """Represents a person""" population=0; #population属于Person类,是一个类的变量 def __init__(self,name):#name变量属于对象(使用self赋值),是对象的变量 self.name=name; print("Initializing %s"%self.name) Person.population+=1; def __del__(self):#对象消失时调用,消逝后对象不再被使用,所占用的内存将返回给系统 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) kalman=Person("Awiln") swar=Person("Vlown") #self.name根据每个对象指定,作为对象的变量 #只能使用self变量来参考同一个同一个对象的变量和方法,被称为属性参考。 #当对象不再被使用时,__def__方法运行,但是很难保证这个方法究竟什么时候运行,要指明它的运行,使用del语句相当于析构函数。
继承
#一个子类型在任何需要父类型的场合可以被替换成父类型,即对象可以被视作父类的实例,这种现象被称为多态现象。 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:%s"%(self.name,self.age)) class Teacher(SchoolMember):#继承,导出类子类 """Represent a teacher""" 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): """docstring fos Student""" def __init__(self, name,age,marks): SchoolMember.__init__(self,name,age) self.marks= marks print("Initialized Student:%s"%self.name) def tell(self): SchoolMember.tell(self) print("Marks:%d"%self.marks) t=Teacher("Teacher Wang",40,30000) s=Student("Student Li",22,75) print members=[t,s] for members in members: members.tell() #这里是多重继承
file:读取文件
poen="""Programming is fun When the work is done if you wanna make your work alse fun: use Python""" f=open("poem.txt","w",1)#打开一个文件,创建file对象,打开后相关的方法才可以调用它进行读写 #第二个参数决定了打开文件的模式:r只读,指针放在文件开头;rb二进制打开,只读 #r+打开文件用于读写;rb+二进制打开一个文件用于读写 ...更多参考教程 #第三个参数buffering,设为0,就不会有内存;设为1,访问文件时会寄存行;设为大于1的整数,表明寄存区的缓冲大小;设为负值,缓冲大小为系统默认。 f.write(poen) f.close f=open("poem.txt") while True: line=f.readline()#读取文件每一行 if len(line)==0: break print (line) f.close()储存
#pickle模块,储存器 #可以在文件中储存任何python对象,又可以完整无缺的取出来:持久的储存对象 import pickle as p #使用模块的一种方式 shopListFile="shopList.data" shopList=["A","B","C"] f=open(shopListFile,"wb+") #wb+以二进制格式打开一个文件用于读写,若存在则覆盖 p.dump(shopList,f)#储存:以写模式打开一个file对象,调用储存器模块的dump函数,把对象储存到打开的文件中 f.close() del shopList f=open(shopListFile,"rb+") storedlist=p.load(f)#使用pickle模块的load函数的返回来取回对象,取储存 print (storedlist)
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇: C#新建数组
- 下一篇:没有了