面向对象的三大特性————封装

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

封装的原则:1,将不需要对外提供的内容都隐藏起来

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

1.1私有变量和私有方法

class Person:
    __key = 123  # 私有静态属性
    def __init__(self,name,passwd):
        self.name = name
        self.__passwd = passwd   # 私有属性

    def __get_pwd(self):         # 私有方法
        return self.__passwd   #只要在类的内部使用私有属性,就会自动的带上_类名

    def login(self):          # 正常的方法调用私有的方法
        self.__get_pwd()

alex = Person('alex','alex3714')
print(alex._Person__passwd)   # _类名__属性名
print(alex.get_pwd())
# 所有的私有 都是在变量的左边加上双下划綫
    # 对象的私有属性
    # 类中的私有方法
    # 类中的静态私有属性
# 所有的私有的 都不能在类的外部使用

 父类的私有属性不能被子类继承

# class Foo:
#     __key = '123'       # _Foo__key
#
# class Son(Foo):
#     print(Foo.__key)     # _Son__key
# class Room:
#     def __init__(self,name,length,width):
#         self.__name = name
#         self.__length = length
#         self.__width = width
#     def get_name(self):
#         return self.__name
#     def set_name(self,newName):
#         if type(newName) is str and newName.isdigit() == False:
#             self.__name = newName
#         else:
#             print('不合法的姓名')
#     def area(self):
#         return self.__length * self.__width
#
# jin = Room('金老板',2,1)
# print(jin.area())
# jin.set_name('2')
# print(jin.get_name())
扩展

2,面向对象-----内置函数(装饰器)

2.1property

property是一种特殊的属性,访问它时会执行一段功能(函数)然后返回值
例一:BMI指数(bmi是计算而来的,但很明显它听起来像是一个属性而非方法,如果我们将其做成一个属性,更便于理解)

成人的BMI数值:
过轻:低于18.5
正常:18.5-23.9
过重:24-27
肥胖:28-32
非常肥胖, 高于32
  体质指数(BMI)=体重(kg)÷身高^2(m)
  EX:70kg÷(1.75×1.75)=22.86

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)

p1=People('egon',75,1.85)
print(p1.bmi)
View Code
# from math import pi
# class Circle:
#     def __init__(self,r):
#         self.r=r
#     @property
#     def per(self):
#         return 2*pi*self.r
#     @property
#     def area(self):
#         return pi*(self.r**2)
# l=Circle(2)
# print(l.per)
# print(l.area)
计算圆

修改(setter)

# class Person:
#     def __init__(self,name):
#         self.__name=name
#     @property
#     def name(self):
#         return self.__name+"h"
#     @name.setter               #修改名称
#     def name(self,new_name):
#         self.__name=new_name
# jin=Person("jin")
# print(jin.name)
                                              # jin._Person__name="al"
# print(jin.name)
# jin.name="ou"
# print(jin.name)

删除 deleter

class Goods:

    def __init__(self):
        # 原价
        self.original_price = 100
        # 折扣
        self.discount = 0.8

    @property
    def price(self):
        # 实际价格 = 原价 * 折扣
        new_price = self.original_price * self.discount
        return new_price

    @price.setter
    def price(self, value):
        self.original_price = value

    @price.deleter
    def price(self):
        del self.original_price


obj = Goods()
obj.price         # 获取商品价格
obj.price = 200   # 修改商品原价
print(obj.price)
del obj.price
View 

2.2classmethod(类的方法)

class Goods:
    __discount=0.6
    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_discount):   #修改静态属性
        cls.__discount=new_discount
apple=Goods("苹果",10)
print(apple.price)
Goods.change__discount(0.5)
print(apple.price)

@classmethod,把一个方法变成一个类中的方法,这个方法就直接可以被调用,不需要依托任何对象。
当某个方法的操作只涉及静态属性的时候就应该使用classmethod这个装饰方法
类方法有一个默认参数 cls代表这个类
View Code

2.3 staticmethod(静态方法)

# class Login:
#     def __init__(self,name,passwd):
#         self.name=name
#         self.passwd=passwd
#     @staticmethod
#     def get_usr_pwd():
#         usr=input("user:")
#         pwd=input("pwd:")
#         Login(usr,pwd)
# jin=Login("AL",123)
# Login.get_usr_pwd()
# jin.get_usr_pwd()

在完全面向对象程序中,如果一个函数既和对象没有关系也和类没有关系,那么就用staticmethod将这个方法变成一个静态方法。
静态方法没有默认参数像函数一样。
View Code
class Staticmethod_Demo():
    role = 'dog'

    @staticmethod
    def func():
        print("当普通方法用")

Staticmethod_Demo.func()
例二

注:类方法和静态方法都是类调用的,对象也可以调用类方法和静态方法,但不推荐用对象调用。

原文地址:https://www.cnblogs.com/glf1160/p/8311108.html