封裝
python沒有真正意義的隱藏
#
class Foo:
__x=1 #_Foo__x
def __test(self): #_Foo__test
print('from test')
print(Foo.__dict__)
print(Foo._Foo__x) #依然可以用這種方式訪問到
# class Chinese:
# country='China'
# def __init__(self,name,age):
# self.name=name
# self.age=age
#
# def talk(self):
# print('say chinese',self)
#
# p1=Chinese('lh',22)
# print(p1.country)
# print(p1.talk())
# print(Chinese.country)
# print(Chinese.__init__)
# print(p1.__init__) #
多態
# class Animal:
# def talk(self):
# print('正在叫')
#
#
# class People(Animal):
# def talk(self):
# print('say hello')
#
#
# class Pig(Animal):
# def talk(self):
# print('哼哼哼')
#
#
# class Dog(Animal):
# def talk(self):
# print('汪汪汪')
#
# class Cat(Animal):
# def talk(self):
# print('喵喵喵')
#
#
# peo1=People()
# pig1=Pig()
# dog1=Dog()
# cat1=Cat()## #
property
# class People:
# def __init__(self, name, weight, height):
# self.name = name
# self.weight = weight
# self.height = height
# @property # 使用property裝飾器可以使對象象調用變量一樣調用函數,在對象看來不知道內部情況,只是一個變量
# def bmi(self):
# return self.weight / (self.height ** 2)
# p = People('lh', 58, 1.73)
# p.height = 1.75
# print(p.bmi) #
綁定方法和非綁定方法
# class People:
# def __init__(self, name, age):#函數上方不加裝飾器的爲對象綁定方法
# self.__name = name
# self.__age = age
#
# def tell_info(self):
# print('人的名字是:%s,人的年齡是:%s' %(
# self.__name,
# self.__age))
# class Foo:
# @classmethod #使用該裝飾器爲類綁定方法,把類作爲第一個參數傳入
# def test(cls):
# print(cls)
# f=Foo()
# Foo.test()
# print(Foo)
# f.test()
# class Foo:
# @staticmethod #此處爲非綁定方法,一般作爲類的工具包
# def test(x,y):
# print('test',x,y)
# Foo.test(1,3)
# f=Foo()
# f.test(1,10)
事例
# import settings
#
# # class MySQL:
# def __init__(self, host, port):
# self.host = host
# self.port = port
# print('conneting...')
#
# @classmethod
# def from_conf(cls):
# return cls(settings.HOST,settings.PORT)
#
# def select(self):
# print(self)
# print('select function')
#
# # conn=MySQL('192.168.0.1',3306)
# # conn.select()
#
# conn1=MySQL('192.168.0.1',3306)
# conn2=MySQL.from_conf()
# class People:
# def __init__(self, name, age, sex):
# self.__name = name
# self.__age = age
# self.__sex = sex
#
# def tell_info(self):
# print('他的名字是: %s,年齡是: %s,性別是:%s' % (
# self.__name,
# self.__age,
# self.__sex))
#
#
# p = People('lh', 22, 'male')
# print(p.__dict__) # {'_People__age': 22, '_People__name': 'lh', '_People__sex': 'male'}
# p.tell_info()
在子類中有和父類同名方法時,使用該方法強制調用父類方法
# class Parent:
# def foo(self):
# print('from parent.foo')
# self.__bar() # 加__會轉換成self._Parent__bar()
#
# def __bar(self): #_Parent__bar,此時不會被子類中的方法覆蓋
# print('from parent.bar')
#
# class Sub(Parent):
# def __bar(self):#_Sub
# print('from SUB.bar!!!')
#
# s=Sub()
# s.foo() #對象中沒有foo方法,去父類中找,然後調用bar,子類有foo所以調用自己的
# s._Parent__bar() # 不能直接用s.bar()
對外提供修改變量接口
# class People:
# def __init__(self, name, age, sex):
# self.__name = name
# self.__age = age
# self.__sex = sex
#
# def tell_info(self):
# print('他的名字是: %s,年齡是: %s,性別是:%s' % (
# self.__name,
# self.__age,
# self.__sex))
#
# def set_info(self, x, y, z):
# if not isinstance(x,str):
# raise TypeError('名字必須是字符串類型')
# if not isinstance(y,int):
# raise TypeError('int Error')
# self.__name = x
# self.__age = y
# self.__sex = z
#
# p = People('lh', 100, 'male')
# p.tell_info()
# # p.set_info('ss ')
# p.set_info('劉昊',23,'male')
# p.tell_info()
property屬性
# class Foo:
# def __init__(self,name):
# self.__name=name
#
# @property
# def test(self):
# print('from foo')
#
# @property#提供訪問的接口
# def name(self):
# return self.__name
#
# @name.setter #再次賦值
# def name(self,value):
# self.__name=value
#
#
# f=Foo('lh')
# f.test#加上property可以看做屬性,不用加()直接執行
# f.name='gogogo'
# print(f.name)
綁定到對象的方法
# class Foo:#在類裏定義的方法都是綁定方法,綁定到對象
# def test1(self):
# print('test1',self)
#
# def test2(self):
# print('test2',self)
#
# def test3():
# print('test3')
# f=Foo()
# f.test1()
# f.test2()
# f.test3()