用python编制一个的类_python之面向对象编制之封装

1.什么是封装

封:属性对外是隐藏的,单对内是开放的

装:申请一个名称空间,往里装入一系列名字/属性

2.为什么要封装

封装数据属性的目的

首先定义属性的目的就是为了给类外部的使用而使用

隐藏之后是为了不让外部直接使用,需要内部开辟一个接口

然后让类外部的使用通过接口间接的操作隐藏的属性

精髓在于:我们可以在接口之上附加任意逻辑,从而严格控制使用者对属性操作

封装函数属性

首先定义属性的目的就是为了给类外部的使用而使用的

隐藏函数属性是为了不让外部直接使用,需要类的内部开辟一个接口

然后在接口内去调用隐藏的功能

精髓在于:隔离了复杂度

3.如何封装

如何隐藏:在属性前面加上__开头

1.这种隐藏仅仅只是之中语法上的变形操作

2.这种语法上的变形只在类定义阶段发生一次,因为类体代码仅仅只在类定义阶段检测一次

3.这种隐藏是对外不对内的,即在类的内部可以直接访问,而在类的外侧无法直接访问

原因是:

在类定义阶段,类的体内代码统一发生一次变形

4.如果不想让子类的方法覆盖父类,可以将该方法前加一个__开头

一.数据属性发封装

例:

# class People:#隐藏仅仅只是一种语法上的变形操作

# __country='China' #_People__country='China'

# __n=100 #_People__n=100

# def __init__(self,name,age,sex):

# self.__name=name #self._People__name=name

# self.age=age

# self.sex=sex

#

# def eat(self):

# print('eat.....')

# print(People.__country) #People._People__country

# print(self.__name) #self._People__name

# People.eat(123)

# print(People.__country)

# peo1=People('egon',18,'male')

# peo1.eat()

# print(peo1.__name) #外面不能调用,属性的名字已经发生改变了

# print(People.__dict__)

# print(People.__country)

# print(People._People__country)

# People.__x=11

# print(People.__dict__)

例2:内部属性全部隐藏,设置可以接口查询,和修改的接口,设置操作权限

class People:

def __init__(self,name,age):

self.__name=name

self.__age=age

def tell_info(self):

print('%s:%s' %(self.__name,self.__age))

def set_info(self,name,age):

if type(name) is not str:

# print('用户名必须为str类型')

# return

raise TypeError('用户名必须为str类型')

if type(age) is not int:

# print('年龄必须为int类型')

# return

raise TypeError('年龄必须为int类型')

self.__name=name

self.__age=age

peo1=People('egon',18)

# peo1.name=123 #外部不能直接修改。直接报错

# peo1.age

# peo1.tell_info()

peo1.set_info('egon',19)#设置一个开放接口,

# peo1.tell_info()

例2.封装加继承属性的查找

# class Foo:

# def __f1(self): #_Foo__f1

# print('Foo.f1')

#

# def f2(self):

# print('Foo.f2')

# self.__f1() #self._Foo__f1

#

# class Bar(Foo):

# def __f1(self): #_Bar__f1

# print('Bar.f1')

#

# obj=Bar()

# obj.f2()

二.函数属性的封装

property装饰器用于将被装饰的方法伪装成一个数据属性,在使用时可以不加括号而直接引用

class People:

def __init__(self,name,weight,height):

self.name=name

self.weight=weight

self.height=height

@property

def bmi(self):

return self.weight / (self.height**2)

peol=People('egon',75,1.8)

print(peol.bmi)

class People:

def __init__(self, name, weight, height):

self.__name = name #数据熟悉隐藏+函数属性隐藏

self.__weight = weight

self.__height = height

@property

def bmi(self):

return self.__weight / (self.__height ** 2)

peol=People('egon',75,1.8)

print(peol.__dict__)

print(peol.bmi)

例,函数封装之 查看、修改、删除,用法:

class People:

def __init__(self,name):

self.__name=name

@property # 查看obj.name

def name(self):

return '' %self.__name

@name.setter #修改obj.name=值

def name(self,name):

if type(name) is not str:

raise TypeError('名字必须是str类型傻叉')

self.__name=name

@name.deleter #删除del obj.name

def name(self):

# raise PermissionError('不让删')      #raise是个异常报错提示命令

print('不让删除傻叉')

# del self.__name

peo1=People('egon')

# print(peo1.name)

# print(peo1.name) #查看

# peo1.name='EGON'      #修改

# print(peo1.name)

del peo1.name #删除#raise是个异常报错提示命令

print('不让删除傻叉')

# del self.__name

peo1=People('egon')

# print(peo1.name)

# print(peo1.name) #查看

# peo1.name='EGON'      #修改

# print(peo1.name)

del peo1.name #删除

2.函数封装:简洁使用方法

class People:

def __init__(self,name):

self.__name=name

def tell_name(self):

return '' %self.__name

def set_name(self,name):

if type(name) is not str:

raise TypeError('名字必须是str类型傻叉')

self.__name=name

def del_name(self):

print('不让删除傻叉')

name=property(tell_name,set_name,del_name) #python中简洁使用方法

peo1=People('egon')

print(peo1.name)

peo1.name='EGON'

print(peo1.name)

del peo1.namename=property(tell_name,set_name,del_name) #python中简洁使用方法

peo1=People('egon')

print(peo1.name)

peo1.name='EGON'

print(peo1.name)

del peo1.name

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值