面向对象2

1. 绑定方法

绑定方法之绑定给对象的

class ForObj():
    def __init__(self,name):
        self.name = name

    # 绑定给对象的方法,对象调用的时候,自动将类名当作参数传递给方法。
    def tell_info(self):
        print('name:{}'.format(self.name))
	
obj = ForObj('jack')
print(obj.name)  # jack
obj.tell_info()  # name:jack

绑定给类的:

IP = '127.0.0.1'
PORT = 3306


class Mysql():
    def __init__(self, ip, port):
        self.ip = ip
        self.port = port

    @classmethod # 该方法绑定给类了
    def from_conf(cls):
        obj = cls(IP, PORT)
        return obj

obj = Mysql(IP, PORT)

obj2 = Mysql.from_conf()
print(obj.ip)

2. 非绑定方法(静态方法)

既不绑定给类,也不绑定给对象

import uuid
# 生成随机字符串
print(uuid.uuid4())

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

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

stu = Student('lili')
print(stu.create_id())
print(Student.create_id())

3. 如何隐藏属性

# 1. 如何隐藏
"""
隐藏的属性:
	1.在类定义阶段,发生了语法上的变形
	    _类名__属性名
	2.隐藏对外不对内
	    内部可以拿到是因为,同样进行了变形
	3.只有在类定义阶段发生变形,其他阶段都不发生变形
"""

# 2. 为何隐藏
"""
	1. 类中的隐藏属性,类外部可以使用(但不是w为了让外部用的),
	如果外部想用,可以在类内部开放接口。
	2. 可以达到对外部数据的严格控制。
"""
class Student():
    __school = 'SH' # _Student__school  => _类名__属性名
    def __init__(self, name, age):
        self.__name = name
        self.age = age

    def __tell_info(self):  # _Student__tell_info  => _类名__函数名
        print('name: %s, age: %s' % (self.__name, self.age))

    def get_school(self):
        return self.__school  # self._Student__school

    def set_school(self, v):
        if type(v) is not str:
            print('数据类型不合法')
        # if isinstance(v, str):
            return
        self.__school = v

4. property装饰器

class Student():
    __school = 'SH'  # _Student__school  => _类名__属性名

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

    def __tell_info(self):  # _Student__tell_info  => _类名__函数名
        print('name: %s, age: %s' % (self.__name, self.age))

    @property  # 把方法伪装成属性
    def name(self):
        return "name:%s" % self.__name

    @name.setter
    def name(self, v):
        if type(v) is not str:
            print('数据类型不合法')
        # if isinstance(v, str):
            return
        self.__name = v

    @name.deleter
    def name(self):
        print('不让删')

stu = Student('ly', 18)
print(stu.get_name())

stu.name = 123
print(stu.name)

del stu.name

了解:

class Student():
    __school = 'SH'  # _Student__school  => _类名__属性名

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

    def __tell_info(self):  # _Student__tell_info  => _类名__函数名
        print('name: %s, age: %s' % (self.__name, self.age))

    def get_name(self):
        return "name:%s" % self.__name

    def set_name(self, v):
        if type(v) is not str:
            print('数据类型不合法')
            # if isinstance(v, str):
            return
        self.__name = v

    def del_name(self):
        print('不让删')
    
    # 了解
    name = property(get_name, set_name, del_name)


stu = Student('ly', 18)

stu.name
stu.name = 'aaa'
print(stu.name)
del stu.name


posted @ 2021-12-03 18:37  Joshua_jiaxue  阅读(35)  评论(0编辑  收藏  举报