廣義上的封裝 : 屬於一個類的靜態和動態屬性 老是出如今一個類中
#使用永遠在類的內部用類名或者對象名調用
狹義上的封裝 : 就是把變量\方法私有化,在類的外部以及子類中不能直接使用了
在python中用雙下劃線開頭的方式將屬性隱藏起來
在內部中調用會自動變形:__x都會自動變造成:_類名__x的形式
class A:
start="aaa" # 靜態變量
__s="bbb" # 私有的靜態變量
def wahaha(self):
print(A.__s) #A._A__s
1.在外部是沒法經過__x這個名字找到變量
print(A.__s) AttributeError
print(A._A__s) #bbb 在類的外面調用私有的變量 但不使用此方法
2.不能在外部建立一個私有的變量
A.__B="ccc"
print(A.__dict__) #在A中並無_A_B:CCC
class B:
def __init__(self,name,pwd):
self.name = name
self.__pwd = pwd # 也能夠建立對象的私有屬性
def get_pwd(self):
print(self.__pwd)
b = B('alex','alex3714')
print(b.name) #alex
print(b.__pwd) AttributeError: 屬性錯誤
print(b._B__pwd) #alex3714
class C:
def __ppt(self): # 私有的方法
print('ppt')
self.age = 83
def open(self):
self.__ppt()
print('打開文件')
c = C()
c.open()
print(c.age)
私有的 靜態變量 對象屬性 方法:
1.只能在類的內部定義和使用
2.在類外部使用私有靜態變量 _類名__私有的名字
3.私有的名字 不能被子類繼承
private 私有的 - __變量名
protect 保護的 - N/A(python中沒有)
public 公共的 - 正常的變量
class A: def __fa(self): #在定義時就變形爲_A__fa print('from A') def test(self): self.__fa() #只會與本身所在的類爲準,即調用_A__fa class B(A): def __fa(self): print('from B') b=B() b.test() #from A
1.property :將一個方法假裝成屬性。在用時用對象屬性的方式調用
2.classmethod:將一個普通方法裝飾爲一個類方法.操做都只能和類中的靜態變量相關
3.staticmethod:將一個方法裝飾成普通函數。在類中裝飾一個不須要self參數 也不須要cls參數的函數
class Circle:
def __init__(self,r):
self.r=r
@property
def area(self):
return self.r**2
@property
def prmite(self):
return self.r*2
c=Circle(5)
print(c.area()) TypeError:此時area是屬性
print(c.area) # 25 用屬性方法調用。不能用c.area
#####property屬性和setter.delete合用### class Person: def __init__(self,name): self.__name=name @property def name(self): return self.__name @name.setter def name(self,new_name): if type(new_name)==str: self.__name=new_name @name.deleter def name(self): del self.__name alex=Person("alex") print(alex.name) alex.name="alex_sb" print(alex.name) del alex.name 商品 價格 原價 打折 = 折後價 class Goods: def __init__(self,name,price,discount): self.name = name self.__price = price self.discount = discount @property def price(self): return self.__price * self.discount apple = Goods('apple',5,0.8) print(apple.price)
類方法是被@classmethod裝飾的特殊方法:
1.被裝飾以後,方法默認接收一個 類 做爲參數
2.以後全部的操做都只能和類中的靜態變量相關 而不該該和對象相關
3.類名 和 對象名 均可以直接調用類方法
class 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)
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 ret = Student.login(1,2,3) print(ret)
房間類 : 全部人 價格 面積
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())