面向对象---封装

 

封装

1.私有方法:

                   (1)子类调不到父类的私有方法,也就是父类的方法不希望子类继承

                   (2)有一些方法的返回值只是用来作为中间结果

class Foo:
    def __jinghong_sb(self):     
        print('Foo')

class Son(Foo):
    def __jinghong_sb(self):
        print('Son')

    def func(self):
        self.__jinghong_sb()     

son = Son()
son.func()
私有方法

 

2.私有属性:分为私有静态属性和私有对象属性

class Teacher:
    __identifier = 'Teacher'   #私有静态属性
    def __init__(self,name,pwd):
        self.name = name
        self.__pwd = pwd     #私有属性
        self.__p()

    def __p(self):          #私有方法
        return hash(self.__pwd)

    def login(self,password):
        return hash(password) == self.__p()
alex = Teacher('alex','3714')
ret = alex.login('3714')
print(ret)
私有属性私有方法

 

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 Person:
    def __init__(self,name,height,weight):
        self.name=name
        self.__height=height
        self.__weight=weight
    def get_bmi(self):
        return self.__weight/(self.__height**2)
    def change_weight(self,new_weight):
        if new_weight>20:
            self.__weight=new_weight
        else:
            print('体重过轻')
ss=Person('aa',1.60,49)
print(ss.get_bmi())
BMI指数

 

房屋:业主  长、宽(隐藏)  面积
class House:
    def __init__(self,name,length,width):
        self.name=name
        self.__length=length
        self.__width=width
    def area(self):
        return self.__length*self.__width

qq=House('aa',224,555)
print(qq.name,qq.area() )
房间面积问题

 

3.用装饰器描述的几个方法:

(1)@property:

class Shop:
    discount=0.75
    def __init__(self,name,price):
      self.name=name
      self.__price=price
    @property
    def price(self):
        return self.__price*Shop.discount
    @price.setter
    def price(self,new_price):
        self.__price=new_price
apple=Shop('apple',5)
print(apple.price)
apple.price=6
print(apple.price)
商品打折问题
class Foo:
    @property
    def AAA(self):
        print('get的时候运行我啊')

    @AAA.setter
    def AAA(self,value):
        print('set的时候运行我啊')

    @AAA.deleter
    def AAA(self):
        print('delete的时候运行我啊')

f1=Foo()
f1.AAA
f1.AAA='aaa'
del f1.AAA
f1.AAA
property方法

 

(2)@classmethod类方法:必须传一个类,方法不需要 使用对象的属性,但可以使用类的属性

class A:
    role = 'a'
    @classmethod
    def class_method(cls):
        print(cls.role)

A.class_method()
A.role
classmethod

 

(3)@staticmethod静态方法:没有必须传的参数,方法不需要用对象的属性和类的方法

class Manager:

    @staticmethod    #静态方法
    def create_student(): pass

Manager.create_student()
print(Manager.create_student)
staticmethod

 

(4)普通方法(绑定(对象)方法):必须传一个对象,可以使用对象的属性和类的属性

 

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

    def func(self):   #self形式参数  普通方法、绑定(对象)方法
        print('func')

a = A('alex')
print(a.func)
普通方法

 

4. 没有方法且属性不会变化的类,特点:定义简单,不能改变

from collections import namedtuple
Point = namedtuple('point',['x','y'])
t1 = Point(1,2)
print(t1.x)
print(t1.y)

 

 

               

 

posted @ 2017-10-30 14:35  星雨5213  阅读(121)  评论(0)    收藏  举报