封装和内置函数( property , classmethod ,staticmethod *)

封装好处:java

【封装】安全

         隐藏对象的属性和实现细节,仅对外提供公共访问方式。函数

【好处】 spa

1. 将变化隔离; .net

2. 便于使用;对象

3. 提升复用性; 继承

4. 提升安全性;ci

【封装原则】get

      1. 将不须要对外提供的内容都隐藏起来;input

      2. 把属性都隐藏,提供公共方法对其访问。

定义:

封装
# 广义上的封装 : 属于一个类的静态和动态属性 老是出如今一个类中
                # 使用的使用永远用类名或者对象名调用
# 狭义上的封装 : 就是把变量\方法私有化,在类的外部以及子类中不能直接使用了

私有变量引用:
# class A:
#     STATIC = 'aaa'  # 静态变量
#     __S = 'bbb'     # 私有的静态变量
#     def wahaha(self):
#         print(A.__S) # _A__S
# print(A.STATIC)
# print(A.__dict__)
# print(A._A__S)      # 在类的外面调用私有的变量
# a = A()
# a.wahaha()
# A.__B = 'ccc'   #在类的外部添加了一个静态变量
# print(A.__dict__)  #咱们不能在一个类的外部建立一个"私有的"变量
# print(A.__B)

 

一:里面调用时候,能够定义成方法,而后打印私有变量

在里面调用私有属性,写一个函数
# class B:
#     def __init__(self,name,pwd):
#         self.name = name
#         self.__pwd = pwd   # 也能够建立对象的私有属性
#
#     def get_pwd(self):
#         print(self.__pwd)
# b = B('alex','alex3714')
# b.qqxing()
# print(b.name)
# print(b._B__pwd)  # 当在类外部的时候,咱们也不能直接使用对象的私有属性
# b.get_pwd()

二: 私有变量小结:

 私有de 静态变量 对象属性 方法
    #私有的 只能在类的内部定义和使用
    # __名字
    # 在类外部使用私有静态变量 _类名__私有的名字
    # 私有的名字 不能被子类继承

Java跟Python对应关系

java
    # private 私有的  - __变量名
    # protect 保护的  - N/A
    # public 公共的   - 正常的变量

实例1

房间类 : 全部人 价格 面积
# class Room:
#     def __init__(self,owner,price,length,width,height):
#         self.owner = owner
#         self.__price_single = price  #单价
#         self.__length = length
#         self.__width = width
#         self.height = height
#
#     def get_area(self):
#         return self.__length * self.__width
#
#     def get_price(self):
#         return self.__price_single * self.get_area()
#
# alex = Room('alex',1000000,2,1,0.8)
# print(alex.get_area())
# print(alex.get_price())

注释:定义方法调用私有变量

将方法假装成属性

实例化时候实例类

可是调用的时候使用方法# print(wang.bmi)

类 可以计算人体的BMI指数
# class Person:
#     def __init__(self,name,height,weight):
#         self.name = name
#         self.__height = height
#         self.__weight = weight
#
#     @property   #将一个方法假装成属性
#     def bmi(self):
#         return self.__weight / (self.__height**2)
#
# wang = Person('王子',1.77,69)
# print(wang.bmi)

说白了就是将不要给用户看的属性假装起来,而后调用方法

 

小应用:定义几个方法调用,调用几个加几个@property

# 圆形类  : r   面积area 周长perimeter
# from math import pi
# class Circle:
#     def __init__(self,r):
#         self.r = r
#     @property
#     def area(self):
#         return self.r*self.r*pi
#     @property
#     def perimeter(self):
#         return 2*pi*self.r
#
# c = Circle(10)
# print(c.area)
# print(c.perimeter)

 

@classmethod 将普通方法装为类方法,其实就是self啦,用仍是类方法调用,说白了就是当成一个参数

lass Goods:
    __discount = 0.8
    def  __init__(self,name,price):
        self.name = name
        self.__price = price

    @property
    def price(self):
        return self.__price*Goods.__discount

    @classmethod  #讲一个普通方法装饰为一个类方法
    def change_discount(cls,new_dis):  # 类方法
        cls.__discount = new_dis        调用类方法  =后面参数
Goods.change_discount(1)
cig = Goods('cigrette',20)
print(cig.price)
cig.change_discount(0.2)
print(cig.price)
# cig.change_discount(1)
# print(cig.price)

注解:

#类方法是被@classmethod装饰的特殊方法
    #被装饰以后,方法默认接收一个 类 做为参数
    # 以后全部的操做都只能和 类中的静态变量相关 而不该该和对象相关
# 类名 和 对象名 均可以直接调用类方法

 

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

    @staticmethod    #装饰一个不须要self参数 也不须要cls参数的函数
    def login(a,b,c):    # 普通的函数
        usr = input('username>>>')
        pwd = input('password>>>')
        if usr == 'alex' and pwd == '123':
            obj = Student(usr)
            return obj
# 学生 login
# 用户名  密码  属性
ret = Student.login(1,2,3)
print(ret)

用这个函数可以将他假装成函数

相关文章
相关标签/搜索