美文网首页
入门(三)类

入门(三)类

作者: 易路先登 | 来源:发表于2021-12-01 17:54 被阅读0次

    1、定义类

    class Student:
            pass
    
    stu1 = Student()
    stu1.name = 'zhangsan'
    print(stu1)
    

    (1)、实例函数

    class Student:
        def say_hi(self):
            print('Hi:' + self.username)
    
    
    stu1 = Student()
    
    stu1.username = "zhangsan"
    
    stu1.say_hi()
    

    (2)、类属性和类方法

    class Student:
        book = 'xixiangji'
    
        def say_hi(self):
            print('Hi:' + self.username)
    
        @classmethod
        def show(cls):
            print(cls.book)
    
    
    stu1 = Student()
    
    stu1.username = "zhangsan"
    
    stu1.show()
    

    使用@classmethod修饰的方法是类方法
    实例可以调用类属性和类方法,类方法不能调用实例方法
    (3)、静态方法
    使用@staticmethod定义的就是静态方法,没有任何必选参数

    class Student:
        book = 'xixiangji'
    
        @staticmethod
        def show2(msg):
            print(Student.book + msg)
    
    
    stu1 = Student()
    
    Student.show2('22')
    stu1.show2('33')
    

    (4)、构造方法

    不支持重载

    class Student:
    
        def __init__(self, name, age):
            self.name = name
            self.age = age
    
    
        def show(self):
            print('%s,年龄为%s' % (self.name, self.age))
    
    
    stu1 = Student('zhangsan', '18')
    stu1.show()
    

    (5)、封装

    封装,就是将不想暴露的属性和方法想办法藏起来

    • 私有属性
    class Student:
    
        __age = 10
    
        def __init__(self, name, age):
            self.name = name
            self.__age = age
    
        def show(self):
            print('%s,年龄为%s' % (self.name, self.__age))
    
    
    stu1 = Student('zhangsan', '18')
    stu1.show()
    print(stu1._Student__age)#通过stu1.__age是访问不到私有属性的
    

    (6)、继承

    class Person:
        def __init__(self, name):
            self.name = name
    
        def run(self):
            print('person:' + self.name + '正在吃饭')
    
    
    class Student(Person):
        def __init__(self, name, email):
            # 调用父类的构造方法
            # Person.__init__(name) #可以通过这种方式调用父类的构造函数
            super().__init__(name)  # 也可以通过这种方式调用父类的构造函数
            self.email = email
    
        def study(self):
            print('student:' + self.name +'正在学习')
    
    
    stu = Student('lisi', 'lisi@163.com')
    stu.run()
    stu.study()
    

    重写

    class Student(Person):
        def __init__(self, name, email):
            # 调用父类的构造方法
            # Person.__init__(name) #可以通过这种方式调用父类的构造函数
            super().__init__(name)  # 也可以通过这种方式调用父类的构造函数
            self.email = email
    
        def study(self):
            print('student:' + self.name + '正在学习')
    
        def run(self):
            print('student:' + self.name + '正在奔跑')
    
    
    stu = Student('lisi', 'lisi@163.com')
    stu.run()
    stu.study()
    

    判断一个类是否是另一个类的子类

    issubclass(A,B)
    

    (7)、多继承

    class A:
        def a(self):
            print('a')
    
    
    class B:
        def b(self):
            print('b')
    
    
    class C(A,B):
        def c(self):
            print('c')
    
    
    c = C()
    c.a()
    c.b()
    c.c()
    
    print(C.__bases__)  # 求C的直接父类
    

    (8)、多态

    一个对象以多种形态呈现就交多态

    class Animal:
        def __init__(self, name):
            self.name = name
    
        def cry(self):
            print('动物的哭泣')
    
    
    class Dog(Animal):
        def __init__(self, name, age):
            super().__init__(name)
            self.age = age
    
        def cry(self):
            print('狗在哭泣')
    
    
    class Cat(Animal):
        def __init__(self, name, sex):
            super().__init__(name)
            self.sex = sex
    
        def cry(self):
            print('猫在哭泣')
    
    
    def show(animal):
        print(animal.name)
        animal.cry()
    
    
    b = Dog('dog', 6)
    c = Cat('Cat', '雌')
    
    show(b)
    show(c)
    

    2、魔术方法

    类中的一些特殊的方法,不需要手动调用,在特定情况下,会自动执行

    (1)、__init__构造函数,实例化时自动调用
    (2)、__str__将对象转换为字符串调用,类似于java中的toString()

    class Person(object):
    
        def __init__(self, name, age):
            self.name = name
            self.age = age
    
        # 将对象转换为字符串时调用,类似于java中的toString()
        def __str__(self):
            return 'Person [name=%s, age=%d]' % (self.name, self.age)
    
    
    p1 = Person('李时珍', 34)
    print(p1)
    

    (3)、__len__在对象使用len()时调用

    class Person(object):
    
        def __init__(self, name, age):
            self.name = name
            self.age = age
    
    
        # 在对象使用len()函数时调用
        def __len__(self):
            return len(self.name)
    

    (4)、__repr__在对象调用repr()函数时调用

    class Person(object):
    
        def __init__(self, name, age):
            self.name = name
            self.age = age
    
        # 在对象调用repr()函数时调用
        def __repr__(self):
            return 'hello person'
    
    
    p1 = Person('李时珍', 34)
    print(repr(p1))
    

    (5)、__bool__在对象调用bool()时调用

    class Person(object):
    
        def __init__(self, name, age):
            self.name = name
            self.age = age
    
        # 在对象调用repr()函数时调用
        def __bool__(self):
            return self.age > 18
    
    
    p1 = Person('李时珍', 34)
    print(bool(p1))
    

    (6)、__gt__在对象进行比较时调用

    class Person(object):
    
        def __init__(self, name, age):
            self.name = name
            self.age = age
    
        # 在对象调用repr()函数时调用
        def __gt__(self, other):
            return self.age > other.age
    
    
    p1 = Person('李时珍', 34)
    p2 = Person('李四光', 18)
    print(p1 > p2)
    

    相关文章

      网友评论

          本文标题:入门(三)类

          本文链接:https://www.haomeiwen.com/subject/dfxsxrtx.html