day15-总结

1.类中的方法

(1)对象方法
a.怎么声明:直接声明在类中的函数
b.怎么调用:通过对象来调用
c.特点:有个指向当前对象的默认参数self;调用的时候不需要传参
d.什么时候用:实现函数的功能需要用到对象属性

(2)类方法
a.怎么声明:声明函数前加'@classmethod'
b.怎么调用:通过类来调用,'类.方法名()'
c.特点:有个默认参数cls,这个参数在通过类调用的时候不需要传参;指向当前类(谁调用指向谁)
类能做的事情,cls都能做
d.什么时候用:实现函数的功能不需要对象属性的前提下,需要类的字段(需要类),这个时候就用类方法

(3)静态方法
a.怎么声明:声明函数前加'@staticmethod'
b.怎么调用:通过类来调用,'类.方法名()'
c.特点:没有特点(没有默认参数)
d.什么时候用:实现函数的功能既不需要对象属性也不需要类的字段,这个时候就用静态方法

class Student:
    num = 30
    # 对象属性

    def __init__(self, name, tel, age=18):
        self.name = name
        self.age = age
        self.tel = tel
        self.study_id = '001'
    # 对象方法

    def study(self):
        print('%s在学' % self.name)

    # 类方法
    @classmethod
    def count(cls):
        print('******')

    # 静态方法
    @staticmethod
    def static_func():
        print('静态方法')


stu1 = Student('小明', '001292')
# 对象方法通过对象调用
stu1.study()
# 类方法通过类调用
Student.count()
# 类调用静态方法
Student.static_func()

2.访问权限

(1)公开 public:属性和方法可以在类的内部使用,也可以被继承,也可以在类的外部使用
(2)保护 protect:属性和方法可以在类的内部使用,可以被继承,但不可以在类的外部使用
(3)私有 private:属性和方法可以在类的内部使用,不可以被继承,也不可以在类的外部使用
python中本质上所有的属性和方法都是公开的,私有化是假的私有化

3.私有化

语法:在声明属性或者方法时,在属性名前或者方法名前加'';
注意:只能'
'开头,不能'__'结尾

私有化的原理:只是单纯的在私有属性或者方法名前加'_类名',导致直接使用原名使用不了属性和方法

class Person:
    num = 61
    __num = 1000

    def __init__(self, name, age, gender):
        self.name = name
        self.age = age
        self.__gender = gender

    def eat(self, food: str):
        print('%s在吃%s' % (self.name, food))
        print(self.__gender)

    @classmethod
    def show_num(cls):
        print('人类数量%d' % cls.num, cls.__num)


p1 = Person('xm', 18, '男')
print(Person.num)
p1.eat('屎')
Person.show_num()

4.什么时候使用getter和setter

(1)getter - 希望获取一个属性值之前做别的事情,就给这个属性添加getter
(2)setter - 希望给属性赋值之前做点儿别的事情,就给这个属性添加setter

5.怎么给属性添加getter和setter

怎么添加getter
a.在需要添加getter的属性前加_
b.在@property后面声明一个函数:
函数名和去掉的属性名一致,并且函数需要一个返回值表示获取属性的值
c.在外面通过不带
的属性名去获取对应的属性值

怎么添加setter
a.如果想要给属性添加setter必须先给它添加getter
b.在@getter名.setter后声明函数;
函数名和去掉_的属性名一致,这个函数不需要返回值,但是需要一个参数

class ReadError(Exception):
    def __str__(self):
        print('该数据是只读的!')


class Rect:
    def __init__(self, width, length):
        self.width = width
        self.length = length
        self._area = 0
        self._perimeter = 0
        self._age = 0

    # 添加getter
    @property
    def area(self):
        return self.width*self.length

    @property
    def perimeter(self):
        return 2*(self.width + self.length)
    # 添加setter

    @property
    def age(self):
        return self._age

    @age.setter
    def age(self, value):
        if type(value) != int:
            print('数据有误!数据不是整型')
            raise ValueError
        if 0 < value <= 200:
            self._age = value
        else:
            print('数据有误!年龄值超过范围')

    @perimeter.setter
    def perimeter(self, value):
        raise ReadError


r1 = Rect(4, 6)
print(r1.area)
r2 = Rect(10, 8)
print(r2.area)
print(r2.perimeter)
r1.age = 100
print(r1.age)

6.什么是继承

让子类直接拥有父类的属性和方法,这个过程就是继承。

7.怎么继承

python中所有的类默认都是继承自object。(object是基类)

(1)语法:
class 类名(父类1,父类2,...):
类的内容

class Person:
    num = 61
    # 注意:__slots__只能约束当前类的对象,不能约束子类
    __slots__ = ('name', 'age', 'gender')

    def __init__(self, name='傻狗', age=18, gender='男'):
        self.name = name
        self.age = age
        self.gender = gender

    def eat(self, food):
        print('%s在吃%s' % (self.name, food))


class Student(Person):
    pre = 'stu'
    # 在这儿去调用父类中的__init__方法

    def __init__(self):
        super().__init__()
        self.study_id = '001'
        self.score = 0

    @staticmethod
    def study():
        print('good dog, nice work')


print(Student.num)
stu = Student()
print(stu.name, stu.age, stu.gender)
stu.eat('hotpot')

8.子类添加内容

(1)添加字段和方法
直接在子类中声明新的字段或者方法
(2)添加对象属性

补充:
a.类中的函数的调用:先看当前类中有没有这个方法,如果有直接调用,如果没有就看父类有没有这个方法
有的话就调用,如果没有就找父类的父类有没有该方法,以此类推,如果找到基类都没有,程序报错

b.super()的使用
在子类的对象方法和类方法中都可以用super()去调用父类的对象方法和类方法
在子类中的静态方法中不能使用super()

class Animal:
    def __init__(self, age, gender):
        self.age = age
        self.gender = gender


class Cat(Animal):
    def __init__(self, type, color, age, gender):
        super().__init__(age, gender)
        self.type = type
        self.color = color


cat1 = Cat('加菲', 'yellow', 2, 'female')
print(cat1.__dict__)

你可能感兴趣的:(day15-总结)