自己动手做爬虫四

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_43999482/article/details/102070495

魔法方法__str__的使用

用于打印对象的描述信息

#str方法的使用:当print打印对象的时候句子的调用__str__

class Person(object):
    #给对象添加属性及对属性进行初始化
    def __init__(self,name,age):
        self.name = name
        self.age = age

    def __str__(self):
        #返回对象的描述信息
        return "我叫:%s    年龄:%d"%(self.name,self.age)
#调用init方法使用位置参数传参
# person = Person("蓝天",18)
#使用关键字传参
person = Person(name="水手",age=18)
# print (person.name,person.age)
print(person)

魔法方法__del__

当对象释放的时候会自动调用__del__方法


#__del__:当对象释放的时候会自动调用__del__方法
#1.程序退出,程序中所使用的对象全部摧毁
#2.当对象的内存没有变量使用的时候,对象摧毁
import time
class Person(object):
    def __init__(self,name,age):
        self.name = name
        self.age = age

    #当引用技术为零时会调用__del__
    def __del__(self):
        print("内存释放",self)
#创建对象
xiao_ming = Person("项目",15)
print(xiao_ming)

#删除变量
del xiao_ming

#引用技术:内存地址被变量是用的次数,当前应用计数为零时表示对象销毁
time.sleep(3)

这里我们要记住,当引用技术不为零时,不会调用__del__

单继承

#单继承救赎子类只继承一个父类
#继承的好处:就是可以复用父类的代码
class Person(object):
    def __init__(self):
        self.name = "三三"
        self.age = 18

    #对象方法,当方法参数有self表示对象方法
    def show(self):
        print(self.name,self.age)

#括号外的是子类,括号内的事父类
#父类(基类),子类(派生类)
class Student(Person):
    pass

xiao_lan = Student()
xiao_lan.show()

多继承

#多继承:相当于继承多个父类
class A(object):
    def show(self):
        print("我是A类")
class B(object):
    def show_info(self):
        print("我是B类")

    def show(self):
        print("我是A类")

class C(A,B):
    pass
c = C()
c.show()
c.show_info()
#方法的调用会遵循mro,类继承顺序,类继承顺序,决定了方法调用时候的查查
print(C.mro())

重写父类的方法

#重写:父类满足不了子类的需求,可以对父类方法进行重写
#重写的特点:1.继承关系, 2.方法名相同
class Person(object):
    def run(self):
        print("跑")

class Student(Person):
    def __init__(self,name,age):
        self.name = name
        self.age = age

    #因为父类满足不了子类需求,对其重写
    def run(self):
        print("%s跑起来"%self.name)

stu = Student("夜夜",13)
stu.run()
#调用方法的时候现在本类找,如果没有再在父类找,会遵循mro的原则

用类名调用父类的方法

class Animal(object):
    #对象方法
    def run(self):
        print("动物跑起来了")

class Dog(Animal):
    def wang(self):
        #在次调用父类的方法
        #1.self,前提类没有父类方法,附则调用自生
        #self.run()
        #2.使用父类的类名,如果使用类名方法要传入实例对象
        Animal.run(self)
        #3.super调用父类方法
        #根据指定类,在类的继承链中获取下一个类
        #1.表示根据指定类在继承链中获取下一个类
        #2.self表示获取self对象类型的类继承链
        #super不一定是你直接继承的父类 
        super(Dog, self).run()
        print(self.__class__.mro())
        print("旺旺")

dog = Dog()
dog.wang()

init方法使用super

class A(object):
    def __init__(self,mame):
        print(A)
        self.name = mame

class B(A):
    #如果子类提供类调用的方法,不会主动调用父类方法
    def __init__(self,age):
        print("B")
        self.age = age
        #调用父类的__init__
        # 1.类名
        # 2.super
        #super(B, self).__init__("ls")
        #简写super = super(B, self)
        super().__init__("asjdk")

    def show(self):
        print("我是B类")

b = B("18")
b.show()
print(b.age,b.name)

私有属性与私有方法

#在属性名和方法名加上“__”那么定义的属性与方法就是私有属性和私有方法
class Person(object):
    def __init__(self,name,age):
        #共有属性
        self.name = name
        #私有属性,只能在本类内部使用,在类外面不能使用
        #注意点:私有属性只能在init中添加
        self.__age = age

    def show(self):
        #在内部使用时没有问题的
        print(self.name,self.__age)
        self.__money()

    def __money(self):
        print("100W")

person = Person("三三",15)
print(person.name)
#私有属性是访问不了的

#查看对象中的属性信息
print(person.__dict__)#可以查看私有方法的
#本意是修改私有属性
#这里不是修改了私有类型,而是给对象添加了以__age的对象属性
#这里不是修改私有属性,只能在init添加
#在Python中没有做到绝对的私有,只是把私有类型进行了一个伪装
person.__age = 22
print(person.__age)
print(person.__dict__)
#查看私有方法
print(Person.__dict__)
# #非常规操作,不建议使用
# person._Person__age = 34
# print(person._Person__age)
# print(person.__dict__)


#person._Person__money()
person.show()

类方法与静态方法


class Person(object):
    #定一个私有类型
    __type = "红种人"
    #定义对象方法
    def show(self):
        print("我是人")

    def __init__(self):
        self.name = "xiaohong"

    @classmethod
    def show_info(cls):
        print(cls)
        print("我是一个类方法")

    @staticmethod
    def show_msg():
        print("我是一个静态方法")

    @classmethod
    def set_type(cls,type):
        cls.__type = type

    @classmethod
    def get_type(cls):
        return cls.__type

    #对象方法是最通用的方法,可以修改对象属性和类属性
    def instance_set_type(self,type,name):
        #获取对象所对应的类
        print(self.name)
        self.__class__.__type = type
        self.name = name

    def instance_get_type(self):
        print(self.name)
        return self.__class__.__type

#创建一个对象
person = Person()
person.show()
person.show_info()
person.show_msg()

person.set_type("白种人")
print(person.get_type())
person.instance_set_type("黄种人","xxx")
print(person.instance_get_type())

#通过类调用静态方法和类方法不需要传入当前类,如果类调用用对象方法需要传入一个实例,一般不这样做
Person.show_msg()

多态

#多态:就是关注的是同一个方法,但是会出现不同的表现形式  提示:在Python中不关心表现形式
class Text(object):
    def show(self):
        print("我是文本文件")

class Image(object):
    def show(self):
        print("我是图片文件")

class Vedio(object):
    def show(self):
        print("我是视频文件")


#关心的的是同一个方法,会出现不同的表现形式,那么这种方法就叫多态
#在Python中多态只关系对象的方法,不关系对象的类型

def show_data(new_fun):
    new_fun.show()

image = Image()
show_data(image)

__new__方法

#__new__:当创建对象的时候hi调用__new__
#__init__:对象创建完成会调用init方法给对象添加对象属性及初始化

#创建对象会自动调用两方法,先是__new__表示对象完成,让后再调用__init__给对象初始化

class Student(object):
    #new 方法里面的参数是一定需要兼容init里面的参数
    def __new__(cls, *args, **kwargs):
        print("创建对象")
        return object.__new__(cls)

    #对象创建完了,给对象添加对象属性
    def __init__(self,name,age):
        self.name = name
        self.age = age
        print("初始化")
        print(self.name,self.age)

stu = Student("三三",15)

单例

#单例:就是在程序中不过创建多少对象,只有一个实例对象
class Person(object):
    #私有的类属性
    __instance = None
    #创建对象
    def __new__(cls, *args, **kwargs):
        if cls.__instance == None:
            #把创建的对象给类属性
            print("创建对象")
            cls.__instance = object.__new__(cls)
        return cls.__instance

    def __init__(self):
        print("初始化")

p1 = Person()
p2 = Person()
print(p1,p2)

slots

#指明创建对象的时候不能再添加其他属性,只能是指定的属性
class Student(object):
    #这样操作可以让对象的属性固定
    __slots__ = ("name","age")
    def __init__(self,name,age):
        self.name = name
        self.age = age

stu = Student("三三",20)
stu.name = "wuwu"
stu.age = 20
print(stu.name,stu.age)

猜你喜欢

转载自blog.csdn.net/weixin_43999482/article/details/102070495
今日推荐