文件指针和类

文件指针
…/:返回上一级/向下一级找
tell():返回指针所在的位置
seek():改变指针的位置,定位到文件的末尾 0—代表开头位置 1—代表当前位置 2—代表末尾位置
writelines():按行写入
系统相关操作
import os
重命名:os.rename(需要修改的文件名,修改后的文件名)
删除指定文件:os.remove(’…/…/某文件’)

面向对象与面向过程
面向过程:根据业务逻辑从上到下写代码
面向对象:将数据和函数一起封装,减少重复的代码

类和对象
结构:
类的名称:类名,大驼峰式命名规则,类名跟在class后面
类的属性:一组数据
类的方法(函数):对其的一些操作(行为)

类可分为经典类写法和新式类写法
经典类写法

class Botany:
    def striking(self):
        print('他正在打人')

    def explode(self):
        print('他会爆炸')

    def take_a_beating(self):
        print('他会被挨揍')

    def Plant_the_Sun(self):
        print('他会种太阳')

botany1 = Botany()
botany1.striking()
botany1.name='Peashooter'
print(botany1.name)

botany2=Botany()
botany2.explode()
botany2.name='The explosion of potatoes'
print(botany2.name)

新式类写法

class Botany(object):
    def __init__(self,name,color):
        self.name = name
        self.color=color

    def striking(self):
        print('他正在打人')

    def explode(self):
        print('他会爆炸')

    def take_a_beating(self):
        print('他会被挨揍')

    def Plant_the_Sun(self):
        print('他会种太阳')

    def introduce(self):
        print('%s他的名字,%s他的颜色' % (botany1.name, botany1.color))

botany1=Botany('Peashooter','green')
#这里面的参数自动传参给__init__方法,所有的参数一一对应
print(botany1.name)
print(botany1.color)

init(self):初始化实例对象
new():new方法创建对象并抛出
魔法方法:
new在init前面调用
self:代表的是一个实例对象。

动态添加临时属性,并调用
(1)调用的时候直接:实例.属性
(2)通过类里面的方法调用实例属性

调用属性

    def introduce(self):
        print('%s他的名字,%s他的颜色' % (botany1.name, botany1.color))

botany1=Botany('Peashooter','green')
print(botany1.name)
print(botany1.color)
botany1.introduce()

猜你喜欢

转载自blog.csdn.net/Pseudolover/article/details/88420577