第七节-面向对象的编程


"""
习题1
定义一个字典类:my_dict, 实现以下功能:
1. 删除某个key
del_key(key)
2. 判断某个键是否在字典里,如果在返回键对应的值,不存在则返回"not found"
is_key(key)
3. 返回键组成的列表:返回list类型
get_key()
4. 合并字典,并且返回合并后字典的values组成的列表。返回类型:(list)
update_dict(dict1, dict2)
"""
class My_dict(object):
    #数据
    def __init__(self,data):
        self.data = data
    #删除某个key
    def del_key(self,key):
        # for k,v in self.data:
        if key in self.data:
            del self.data[key]
    #判断某个键是否在字典里
    def is_key(self,key):
        if key in self.data:
            print("键'%s'在字典中"%key)
        else:
            print("not found!")
    #返回键组成的列表
    def get_key(self):
        key_list = []
        for i in self.data:
            key_list.append(i)
        return key_list
    #合并字典,并且返回合并后字典的values组成的列表
    def update_dict(self,dict1):
        # dict0 = dict(dict1.items()+dict2.items())
        for k,v in dict1.items():
            self.data[k] = v
        return list(self.data.values())
#调用
dict_data = dict(a=1, b=4, c=3)
d = My_dict(dict_data)
print(d.data)
d.is_key("a")
d.del_key("a")
print(d.data)
d.is_key("a")
print(d.get_key())
dict_data1 = dict(a1=1, b1=5, c=3)
print(d.update_dict(dict_data1))
>>
{'a': 1, 'b': 4, 'c': 3}
键'a'在字典中
{'b': 4, 'c': 3}
not found!
['b', 'c']
[4, 3, 1, 5]
习题二、
"""  类
===================================================
习题2、
定义一个交通工具类Vehicle
----------------------------
它有属性:
速度:velocity, 默认为 0
里程数:mileage, 默认为 0
是否在运动:is_on, 默认为 False
----------------------------
实现如下的方法:
1. speed_up() 提高速度
2. slow_down() 降低速度
3. turn_on() 启动交通工具, 速度为10
4. go(time=10s) 根据速度-时间公式计算里程数, 更新里程数
====================================================
"""
class Vehicle(object):
    #数据属性
    def __init__(self,velocity,mileage,is_on = False):
        self.vel= velocity
        self.mil = mileage
        self.is_on = is_on
    def speed_up(self,v1):
        self.vel += v1
    def slow_down(self,v2):
        self.vel -= v2
    def turn_on(self):
        self.is_on = True
        self.vel = 10
    def update_mil(self,time):
        self.mil += self.vel * time
#调用
bus = Vehicle(35,1000)
print("bus.is_on:",bus.is_on)
bus.speed_up(100)
print("bus.vel:",bus.vel)
bus.slow_down(14.5)
print("bus.vel:",bus.vel)
bus.turn_on()
print("bus.vel:",bus.vel)
print("bus.is_on:",bus.is_on)
print("bus.mil:",bus.mil)
bus.update_mil(10)
print("bus.mil:",bus.mil)
>>
bus.is_on: False
bus.vel: 135
bus.vel: 120.5
bus.vel: 10
bus.is_on: True
bus.mil: 1000
bus.mil: 1100
类:有中封装的概念,使用,而不用去关注实现细节
return、yield
for i in range(5):
yield i
>>



迭代器:用returne构造
用yield构造:

继承和多态:
  • 子类继承父类的所有方法
  • 子类重新定义方法,达到子类的同父类同一个函数名,多种状态/效果:获得该进代码的可能性,即多态,,,(实现站在巨人的肩膀上编码)
习题1 
使用 yield 定义一个函数, 计算斐波那契数列。
在函数外部调用这个函数, 不断获取下一个结果, 并存储到一个名为 result 的列表中。
def Fibonacci_list():
    a1 = 0
    a2 = 1
    while True:
        a = a2
        a2 += a1
        a1 = a
        yield a2
#调用
f = Fibonacci_list()
result = [1]
for i in range(199):
    result.append(next(f))
print(result)
>>
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, ...

习题2
使用 return 计算斐波那契数列的前 n 项, 返回一个存储结果的列表。对比他们的不同
def Fibonacci_list2(n):
    result = [1,1]
    if n > 2:
        for i in range(2,n):
            result.append(result[i-2] +result[i-1])
        return result
    else:
        print("n <= 2")
#调用
s = Fibonacci_list2(200)
print(s)
>>
[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, ...

习题3
继承上一节课作业题中的交通工具类 Vehicle, 得到你自己的交通工具。
修改它提高 / 降低速度的幅度,
增加颜色这一属性。
并进行对象创建和调用函数测试。
看看你是否正确改动了以上内容。
===================================================
习题2、
定义一个交通工具类Vehicle
----------------------------
它有属性:
速度:velocity, 默认为 0
里程数:mileage, 默认为 0
是否在运动:is_on, 默认为 False
----------------------------
实现如下的方法:
1. speed_up() 提高速度
2. slow_down() 降低速度
3. turn_on() 启动交通工具, 速度为10
4. go(time=10s) 根据速度-时间公式计算里程数, 更新里程数
====================================================
"""
class Vehicle(object):
    #数据属性
    def __init__(self,velocity,mileage,is_on = False):
        self.vel = velocity
        self.mil = mileage
        self.is_on = is_on
    def speed_up(self):
        self.vel += 10
    def slow_down(self):
        self.vel -= 10
    def turn_on(self):
        self.is_on = True
        self.vel = 10
    def update_mil(self,time):
        self.mil += self.vel * time
"""
继承交通工具类,作为子类
"""
class my_vehicle(Vehicle):
    def __int__(self,velocity,mileage,is_on,colour):
        super(my_vehicle,self).__init__(velocity,mileage,is_on)
        self.colour = colour
    #更改方法,相当于实现多态
    def speed_up(self):
        self.vel += 20
    def slow_down(self):
        self.vel -= 20
    def colour(self,yanse):
        self.colour = yanse
#调用
car = my_vehicle(100,120)
car.turn_on()
car.colour('yellow')
car.speed_up()
print("my vehicle info:",car.vel,car.mil,car.colour,car.is_on)
>>
my vehicle info: 30 120 yellow True

猜你喜欢

转载自blog.csdn.net/sdhotn/article/details/80323472