面向对象练习题二

时间:2023-02-23 13:55:51

练习题二

'''
模拟cs游戏
人物角色分为警察和匪徒两种,定义成两个类
折有的警察的角色都是 police
每个警察都有自己独有名字,生命值,武器,性别
每个都可以开枪攻击敌人,且攻击目标不能是 police
所有的匪徒的角色都是 terrorist
每个匪徒都有自己独有名字,生命值,武器,性别
每个都可以开枪攻击敌人,切攻击目标不能是 terrorist
a.实例化一个警察,一个匪徒,警察攻击匪徒,匪徒掉血

'''


class Terrorist(object):
    def __init__(self, name, life_value, sex, weapon):
        self.name = name
        self.life_value = life_value
        self.sex = sex
        self.weapon = weapon

    def attack(self, police_obj):
        if not isinstance(police_obj, Police):
            print('攻击目标错误!!')
        else:
            police_obj.life_value -= self.weapon


class Police(object):

    def __init__(self, name, life_value, sex, weapon):
        self.name = name
        self.life_value = life_value
        self.sex = sex
        self.weapon = weapon

    def attack(self, terrorist_obj):
        if not isinstance(terrorist_obj, Terrorist):
            print('攻击目标错误!!')

        else:
            terrorist_obj.life_value -= self.weapon


p = Police('zhang', 200, 'female', 60)
t = Terrorist('li', 200, 'male', 40)

print('Terrorist ', t.__dict__)
print('Police attack Terrorist start !!!!')
while t.life_value > 0:
    p.attack(t)
    if t.life_value < 0:
        t.life_value = 0
    print('Terrorist life_value', t.life_value)

"""
16.自定义类
个圆形类,属性是半径,提供两个方法,计算圆周长和面积
实现如图的继承关系,然后验证经典类与新式类在查找一个属性时的搜索顺序
基于多态的概念来实现Liunx中一切皆文件的概念:文本文件,进程,磁盘都是文件,然后
验证多态性
ⅳv.定义老师类,把老师的属性:薪资,隐藏起来,然后针对该属性开放访问接口
V.定义如下类,并最大程度地重用代码(继承,派生:子类重用父类方法,组合,)老师
类学生类分数类课程类日期类
基于授权定制自己的列表类型,要求定制的自己的init方法。
ⅶi.定制自己的 append:只能向列表加入字符串类型的值
vi.定制显示列表中间那个值的属性(提示: property)
ⅸX.其余方法都使用is默认的(提示:_ getattr加反射)
"""


class Circle(object):
    PI = 3.14

    def __init__(self, radius):
        self.radius = radius

    def area(self):
        """
         圆形的面积
        :return:
        """
        return Circle.PI * self.radius * self.radius

    def grith(self):
        """
        圆形的周长
        :return:
        """
        return 2 * Circle.PI * self.radius


class A:
    def test(self):
        print('from A')


class B(A):
    def test(self):
        print('from B')
        super(B, self).test()


class C(A):
    def test(self):
        print('from C')
        super(C, self).test()


class D(B):
    def test(self):
        print('from D')
        super(D, self).test()


class E(C):
    def test(self):
        print('from E')
        super(E, self).test()


class F(D, E):
    print("from F")
    pass


f = F()
f.test()
# 经典类python2,不继承object 顺序深度优先F-D-B-A-E-C
# 新式类python3,顺序广度优先F-D-B-E-C-A

import abc


class LFile(metaclass=abc.ABCMeta):

    @abc.abstractmethod
    def open(self):
        pass

    @abc.abstractmethod
    def read(self):
        pass

    @abc.abstractmethod
    def write(self):
        pass


class TxtFile(LFile):
    def __init__(self, name):
        self.name = name

    def read(self):
        print('read txtFile', self.name)

    def open(self):
        print('open txtFile', self.name)

    def write(self):
        print('write txtFile', self.name)


class ProcFile(LFile):
    def __init__(self, name):
        self.name = name

    def read(self):
        print('read ProcFile', self.name)

    def open(self):
        print('open ProcFile', self.name)

    def write(self):
        print('write ProcFile', self.name)


class DiskFile(LFile):
    def __init__(self, name):
        self.name = name

    def read(self):
        print('read DiskFile', self.name)

    def open(self):
        print('open DiskFile', self.name)

    def write(self):
        print('write DiskFile', self.name)


def read(obj):
    return obj.read()


def write(obj):
    return obj.write()


def open(obj):
    return obj.open()


d = DiskFile('d')
p = ProcFile('p')
t = TxtFile('t')

read(t)
write(p)
open(d)




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

class Student(People):
    def __init__(self,name,age):
        super().__init__(name,age)
        self.course = []


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

class Teacher(People):
    def __init__(self,name,age, sal):
        super().__init__(name,age)
        self.__sal = sal
        self.courses = []
        self.students = []

    def tell_info(self):
        for c in self.students:
            print('%s 教学生 %s' % (self.name, c.name))
        for s in self.courses:
            print('%s 教课程 %s' % (self.name, s.name))

python = Course('python','6 month',5000)
go = Course('go','4 month', 3000)

egon=Teacher('egon',18,5000)
s1=Student('s1',18)

egon.courses.append(python)
egon.courses.append(go)
egon.students.append(s1)

s1.course.append(python)
s1.course.append(go)

egon.tell_info()


class List:
    def __init__(self, seq, tag=False):
        self.tag = tag
        self.seq = seq

    def append(self, obj):
        """
         定制append只接受str类型
        :param obj:
        :return:
        """
        if not isinstance(obj, str):
            raise TypeError
        self.seq.append(obj)


    def clear(self):
        if not self.tag:
            raise PermissionError
        self.seq.clear()

    def __getattr__(self, item):
        print('getattr')
        if hasattr(self.seq,item):
            func = getattr(self.seq, item)
            func()
            print(self.seq)


    @property
    def mid(self):
        index = len(self.seq) // 2
        return self.seq[index]

l=List(['e','a','f'])
print(l.seq)
l.append('4')
print(l.seq)
print(l.tag)
print(l.__dict__)
print(l.mid)

# l.clear() #异常
l.sort

l.tag=True
l.clear()
print(l.seq)