時光很短暫

导航

 

绑定方法

# 绑定方法:绑定给类的,绑定给对象的
class Student():
    school = 'SH'

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

    def tell_info(self, v):

        print('name:%s, age: %s, v:%s' % (self.name, self.age, v))



stu = Student('tom', 19)

# 对象来调用方法, 绑定给对象的方法由对象来调用,特殊之处:把对象自己当成第一个参数传递到方法里面
stu.tell_info(10)  # stu.tell_info(stu)

# 绑定给类的方法
# ip port
class Mysql():
    def __init__(self, ip, port):
        self.ip = ip
        self.port = port

    # 绑定给类的方法
    @classmethod
    def from_func(cls):
        # Oracle()
        return cls('127.0.0.1', '3306')
    
    
    def func(self):
        self.__class__

# obj = Oracle('127.0.0.1', 3306)
#
# obj1 = obj.from_func()
# print(obj1)

# 类调用
Mysql.from_func() # Mysql.from_func(Mysql)

非绑定方法

# 即不绑定给类,也不绑定给对象
class People():
    def __init__(self, name, age):
        self.name = name
        self.age = age
        self.id = self.create_id()

    @staticmethod
    def create_id():
        import uuid
        return uuid.uuid4()


obj = People('tom', 19)
# res=obj.create_id()  # obj.create_id(obj)
# print(res)

print(People.create_id())
# import uuid
# print(uuid.uuid4())

'''
    非绑定方法:静态方法
        1. 加一个装饰器:@staticmethod
        2. 函数已经变成普通函数
        3. 对象和类都可以调用,
'''

隐藏属性

# 隐藏属性是有应用场景的
1. 如何隐藏?
2. 为什么要隐藏?

class Student():
    __country = 'CHINA'  # _Student__country

    def __init__(self, name, age):
        self.__name = name # _Student__name
        self.age = age

    def __func(self): # _Student__func
        print('from func')

    def get_country(self):
        return self.__country  # self._Student__country

    def set_country(self, v):
        if not type(v) is str:
            print('不是str')
            return
        self.__country = v


print(Student.__dict__)

property装饰器

class Student():
    __country = 'CHINA'  # _Student__country

    def __init__(self, name, age):
        self.__name = name # _Student__name
        self.age = age

    def __func(self): # _Student__func
        print('from func')

    # 把函数伪装成属性
    @property
    def country(self):
        return self.__country  # self._Student__country

    @country.setter
    def country(self, v):
        if not type(v) is str:
            return
        self.__country = v
    @country.deleter
    def country(self):
        print('不能删')
        
 stu = Student('tom', 18)

# print(stu.country)

# stu.country = 'xxx'
# print(stu.country)
del stu.country

继承

1. 什么是继承?
    继承就是新建类的一种方式,新建出来的类称为子类或者叫派生类,被继承的类称为父类或者基类
    
    子类可以遗产父类的属性
2. 为什么要用继承?
    类解决了对象与对象之间的代码冗余问题
    继承解决类与类之间的代码冗余问题

3. 怎么用继承?
    经典类:没有继承object类的子子孙孙类都是经典类
    新式类:继承了object类的子子孙孙类都是新式类
    '''只有在python2中才区分经典类和新式类,在python3中都是新式类'''
    
 
class People():
    school = 'SH'
    def __init__(self, name, age, gender):
        self.name = name
        self.age = age
        self.gender = gender

class Student(People):
    def __init__(self, name, age, gender, course=None):
        if course is None:
            course = []
        People.__init__(self, name, age, gender) # 指名道姓的调用
        self.course = course

    def choose_course(self):
        print('aaaa')


class Teacher(People):
    def __init__(self, name, age, gender, level):
        People.__init__(self, name, age, gender)
        self.level = level

    def score(self):
        print('打分')

stu = Student('tom', 19, 'male')
tea = Teacher('ly', 20 ,'female', 10)

print(stu.name)
print(stu.school)
print(tea.school)

 

posted on 2022-02-17 08:38  時光很短暫  阅读(19)  评论(0)    收藏  举报