python简单学习:类、类与对象、继承、读取文件

来源:互联网 发布:vb select语句 编辑:程序博客网 时间:2024/06/05 23:52

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。

classTest:defprt(self):print(self)print(self.__class__)t =Test()t.prt()

以上实例执行结果为:

<__main__.Test instance at 0x10d066878>__main__.Test

从执行结果可以很明显的看出,self 代表的是类的实例,代表当前对象的地址,而 self.class 则指向类。

self 不是 python 关键字,我们把他换成 runoob 也是可以正常执行的:

实例

classTest:defprt(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=nameself.age=ageprint('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 = salaryprint('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= marksprint('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)printmembers=[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.closef=open('poem.txt')while True:line=f.readline()#读取文件每一行if len(line)==0:breakprint (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 shopListf=open(shopListFile,'rb+')storedlist=p.load(f)#使用pickle模块的load函数的返回来取回对象,取储存print (storedlist)