1. 程式人生 > 實用技巧 >面向物件高階及一些方法

面向物件高階及一些方法

property的應用

# 例1

# class People:

# 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)

#
#

# p = People('egon', 1.81, 70)

# p.height = 1.84

print(p.bmi())

例2:

class Student:
__school = "oldboy" # _Student__school = "oldboy"

def __init__(obj, x, y, z):
obj.__name = x
obj.__age = y
obj.gender = z

@property
def name(self):
print("訪問控制")
return self.__name

@name.setter
def name(self, x):
print("賦值控制")
self.__name = x

@name.deleter
def name(self):
print("刪除控制")
del self.__name


stu_obj1 = Student("馮瘋子", 18, "female")

stu_obj1.name
# 1\在子類派生的新方法中重用父類的功能

# 方式一:指名道姓地呼叫某一個類的函式

特點:不依賴於繼承關係

#

# class OldboyPeople:

# school = "oldboy"

# # 空物件,"艾利克斯",73,'male'

# def __init__(self,name,age,gender):

# self.name = name

# self.age = age

# self.gender = gender

#

# def f1(self):

# print('1111111')

#

# class Student(OldboyPeople):

# # 空物件,"艾利克斯",73,'male',1001,"python全棧開放"

# def __init__(self,name,age,gender,stu_id,course):

# OldboyPeople.__init__(self,name,age,gender) # OldboyPeople.__init__(空物件,"艾利克斯",73,'male')

# self.stu_id = stu_id

# self.course = course

#
#

# def choose(self):

# print('%s 正在選課' %self.name)

#

# def f1(self):

# OldboyPeople.f1(self)

# print("22222")

#

# stu1=Student("艾利克斯",73,'male',1001,"python全棧開放")

# # tea1=Teacher("egon",18,'male',2000,10)

#
#

# stu1.f1()

# 2 在子類派生的新方法中重用父類功能的方式二

# 方式一:呼叫super(自己的類名,self)會返回一個特殊的物件,super(自己的類名,self).屬性,會參照屬性查詢發起的那個類的mro列表去它父類中查詢屬性

# 特點:嚴格依賴於繼承關係

# class OldboyPeople:

# school = "oldboy"

# # 空物件,"艾利克斯",73,'male'

# def __init__(self,name,age,gender):

# self.name = name

# self.age = age

# self.gender = gender

#

# def f1(self):

# print('1111111')

#

# class Student(OldboyPeople):

# def __init__(self,name,age,gender,stu_id,course):

# # OldboyPeople.__init__(self,name,age,gender) # OldboyPeople.__init__(空物件,"艾利克斯",73,'male')

# super(Student,self).__init__(name,age,gender)

# self.stu_id = stu_id

# self.course = course

#
#

# def choose(self):

# print('%s 正在選課' %self.name)

#

# def f1(self):

# # OldboyPeople.f1(self)

# # super().f1()

# print("22222")

#

# # print(Student.mro())

# stu1=Student("艾利克斯",73,'male',1001,"python全棧開放")

# # print(stu1.__dict__)

# stu1.f1()


#3:

# class A:

# def test(self):

# super().test()

# class B:

# def test(self):

# print('from B')

# class C(A,B):

# pass

# c=C()

# print(C.mro())

# c.test()

# obj=A()

# obj.test()

#4:
class A:
def test(self):
print('A---->test')
super().aaa()
class B:
def test(self):
print('B---->test')

def aaa(self):
print('B---->aaa')

class C(A,B):
def aaa(self):
print('C----->aaa')

c=C()
print(C.mro())
c.test()
"""
A---->test
B---->aaa
"""

物件組合

# 組合:把另外一個類的物件賦值給當前物件的屬性

# 組合表達的是一種有的關係

class Teacher:
def init(self, name, age, gender, level):
self.name = name
self.age = age
self.gender = gender
self.level = level

def tell(self):
print("%s:%s" % (self.name, self.age))

class Student:
def init(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender

class Course:
def init(self, name, price, period):
self.name = name
self.price = price
self.period = period

def tell(self):
print('<%s:%s:%s>' % (self.name, self.price, self.period))

tea1 = Teacher("egon", 18, "male", 10)
stu1 = Student("xxx", 19, "male")

python = Course("python開放", 30000, "3mons")
linux = Course("linux課程", 30000, "3mons")

tea1.courses = [python,linux]
stu1.course = python

# tea,stu  # 超級物件

# stu1.course.tell()

for course_obj in tea1.courses:
course_obj.tell()

內建方法

內建方法都是在滿足某種條件下自動觸發的

# 1 __str__

# class People:

# def __init__(self, name, age):

# self.name = name

# self.age = age

#

# def __str__(self):

# # print('===>')

# return "<%s:%s>" %(self.name,self.age)

#

# obj = People("egon", 18)

#

# print(obj) # print(obj.__str__())


# 2 __del__

class People:
def __init__(self, name, age,f):
self.name = name
self.age = age
self.f = f

def __del__(self):
print('===>')
# 回收資源
self.f.close()

obj = People("egon", 18,open("a.txt",'w',encoding='utf-8'))

del obj
print('執行完畢...')