讀書筆記之python面向對象編程的四大支柱


#封裝,繼承,多態,抽象
#封裝
#1.封裝1:在面向對象編程中,對象將變量和方法集中在一個地方,即對象自己
class Rectangle():
def __init__(self,w,l):
self.width=w
self.len=l

def area(self):
return self.width*self.len

#封裝2:隱藏類的內部數據,避免客戶端代碼直接訪問
class Data:
def __init__(self):
self.nums=[1,2,3,4,5]
def change_data(self,index,n):
self.nums[index]=n

#data 類有一個叫num的實例變量,包含一個整型數列表。
# 建立data對象後,有2中方法能夠變nums元素:
# 1)使用change_data方法,2)直接使用Data對象訪問其nums實例變量
class Data:
def __init__(self):
self.nums=[1,2,3,4,5]

def change_data(self,index,n):
self.nums[index]=n
#方法1
data_one=Data()
data_one.nums[0]=100
print(data_one.nums)
#方法2
data_two=Data()
data_two.change_data(0,100)
print(data_two.nums)

#python中沒有私有變量,全部變量都是能夠公開訪問,
#python經過另外一種方法解決了私有變量應對的問題。
#使用命名約定,在變量或方法前如下劃線開頭
class PublicPrivateExample:
def __init__(self):
self.public="safe"
self._unsafe="unsafe"

def public_method(self):
#客戶端能夠使用
pass
def _unsafe_method(self):#客戶端不該使用
pass

#抽象:剝離事物的諸多特徵,使其只保留最基本的特質的過程
#多態:爲不一樣的基礎形態(數據類型)提供相關接口(函數或方法)的能力
#以下多態的例子,print函數爲字符串,整數,浮點數這3種不一樣的數據類型提供了相同的接口
print("hello,world")
print(200)
print(200.1)

#未使用多態的代碼畫圖,分別畫出三角形,正方形,圓形
shape=[trl,sql,crl]
for a_shape in shapes:
if type(a_shape)=="Triangle":
a_shape.draw_triangle()
if type(a_shape)=="Square":
a_shape.draw_triangle()
if type(a_shape)=="Circle":
a_shape.draw_circle()

#使用多態
shape=[trl,
swl,
crl]
for a_shape in shapes:
a_shape.draw()
#13.4繼承
class Shape():
def __init__(self,w,l):
self.width=w
self.len=l
def print_size(self):
print("""{}by{}""").format(self.width,self.len)
# my_shape=Shape(20,25)
# my_shape.print_size()
class Square(Shape):
def area(self):
return self.width*self.len

a_square=Square(20,25)
a_square.print_size()
print(a_square.area())

#方法覆蓋,子類繼承父類,定義一個與繼承的方法名稱相同的新方法,覆蓋父類的方法
class Shape():
def __init__(self,w,l):
self.width=w
self.len=l
def print_size(self):
print("""{} by{}""".format(self.width,self.len))
class Square(Shape):
def area(self):
return self.width*self.len
def print_size(self):
print("""I am{}by{}""".format(self.width.self.len))


a_square=Square(20,20)
a_square.print_size()

#組合:表示狗和主人的關係
class Dog():
def __init__(self,name,breed,owner):
self.name=name
self.breed=breed
self.owner=owner

class Person():
def __int__(self,name):
self.name=name

mick=Person("Mick Jagger")
stan=Dog("Stanley","Bulldog",mick)
print(stan.owner.name)

練習

 

 答案python

1sql

class Rectangle():
def __init__(self, width, length):
self.width = width
self.length = length

def calculate_perimeter(self):
return self.width * 2 + self.length * 2


class Square():
def __init__(self, s1):
self.s1 = s1

def calculate_perimeter(self):
return self.s1 * 4

a_rectangle = Rectangle(25, 50)
a_square = Square(20)

print(a_rectangle.calculate_perimeter())
print(a_square.calculate_perimeter())

2.
class Square():
def __init__(self, s1):
self.s1 = s1

def calculate_perimeter(self):
return self.s1 * 4

def change_size(self, new_size):
self.s1 += new_size

a_square = Square(100)
print(a_square.calculate_perimeter())

a_square.change_size(200)
print(a_square.change_size())

3.
class Shape():
def what_am_i(self):
print("I am a shape.")


class Rectangle(Shape):
def __init__(self, width, length):
self.width = width
self.length = length

def calculate_perimeter(self):
return self.width * 2 + self.length * 2


class Square(Shape):
def __init__(self, s1):
self.s1 = s1

def calculate_perimeter(self):
return self.s1 * 4

a_rectangle = Rectangle(20, 50)
a_square = Square(29)

a_rectangle.what_am_i()
a_square.what_am_i()

4.
class Horse():    def __init__(self, name):        self.name = nameclass Rider():    def __init__(self, name, horse):        self.name = name        self.horse = horseharry_the_horse = Horse("Harry")the_rider = Rider("Sally", harry_the_horse)print(the_rider.horse.name)
相關文章
相關標籤/搜索