#封装,继承,多态,抽象
#封装
#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)
练习
答案
1
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 = name
class Rider():
def __init__(self, name, horse):
self.name = name
self.horse = horse
harry_the_horse = Horse("Harry")
the_rider = Rider("Sally", harry_the_horse)
print(the_rider.horse.name)