zoukankan      html  css  js  c++  java
  • 面向对象的封装&定制数据类型

    第一层面的封装:类就是麻袋,本身就是封装

    第二层面的封装:类中定义私有的,只能在类的内部使用,外部无法访问(类似 __name)

     1 class Room:
     2     def __init__(self,name,num,length,weigth,hegith):
     3         self.name=name
     4         self.num=num
     5         self.__length=length   #类的私有属性
     6         self.__weigth=weigth
     7         self.__hegith=hegith
     8 
     9     def area(self): #用一个接口方法来实现类的私有属性的调用
    10         return self.__length*self.__weigth*self.__hegith
    11 
    12 r1=Room('小芬','021',11,13,5)
    13 print(r1.name)
    14 print(r1.area())

     数据类型的处理实现:

     1 class List(list):
     2     def check(self,obj): #定制数据类型
     3         if type(obj) is str:
     4              self.append(obj)
     5             # list.append(obj)
     6             # super().append(obj)
     7         else:
     8             print('数据类型有误,必须是str')
     9 
    10     def show_cen(self):
    11         new=int(len(self)/2)
    12         return self[new]
    13 
    14 lt=List('dfkrev')
    15 # print(lt.show_cen())
    16 lt.check('excet')
    17 print(lt)
  • 相关阅读:
    国际区号选取组件
    python和js执行字符串函数
    mysql存储过程循环遍历某个表以及事件
    mysql创建远程用户
    ubuntu改文件夹权限
    mysql最大连接数
    MSYS
    Eclipse Java 可视化插件
    你不知道的继承
    vue3.x
  • 原文地址:https://www.cnblogs.com/wen-kang/p/9246204.html
Copyright © 2011-2022 走看看