join_mark

 

python 类的封装

#封装是为了明确内外,内部的实现逻辑,外部无法知晓,并且为封装到内部的逻辑提供一个访问接口给外部使用
#"_"单下划线开头的变量或函数
#"__"双下划线开头,python默认给变量和函数重名字

# class People:
# # star='earth'
# # _star = 'earth' #python约定以‘_’开头是私有的
# __star = 'earth' #python约定以‘__’开头是私有的
# def __init__(self,id,name,age,salary):
# self.id=id
# self.name=name
# self.age=age
# self.salary=salary
# def get_rd(self):
# print('我是私有方法,我找到的id是[%s]' %self.id)
# #定义访问函数(接口函数)
# def get_star(self):
# print(self.__star)
#
# p1=People('39098','jack','19',1020220020)
# # print(p1._star) #属性是_下划线开头的,你就应该知道不应该在外部使用,但也是可以访问的
# print(People.__dict__) #以__star变量属性,python全自动重命名了,->'_People__star': 'earth'
# print(p1._People__star) #
# p1.get_star()



#------------------------ 封装实例----------------------------------------
# class Room:
# def __init__(self,name,owener,width,length,high):
# self.naem=name
# self.owener=owener
# self.__width=width #封装
# self.__length=length #封装
# self.__high=high
#
# def tell_are(self): #提供接口函数,访问
# return self.__width * self.__length * self.__high
#
#
# r1=Room('房子','jack',10,20,5)
# print(r1.tell_are())

posted on 2018-07-21 00:26  join_mark  阅读(182)  评论(0)    收藏  举报

导航