qzmjef 发表于 2024-8-18 05:34:49

Python语法示例——面向对象


    <h1 style="color: black; text-align: left; margin-bottom: 10px;">1.自己写一个Student类,此类的对象有属性name, age, score, 用来<span style="color: black;">保留</span>学生的姓名,年龄,成绩</h1><span style="color: black;"># 1)写一个函数input_student读入n个学生的信息,用对象来存储这些信息(<span style="color: black;">不消</span>字典),并返回对象的列表</span>
    <span style="color: black;"># 2)写一个函数output_student 打印这些学生信息(格式不限)</span>
    <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">Student</span>():</span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">__init__</span><span style="color: black;">(<span style="color: black;">self</span>, name, age, score)</span></span>:
    <span style="color: black;">self</span>.name = name
    <span style="color: black;">self</span>.age = age
    <span style="color: black;">self</span>.score = score


    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">input_student</span><span style="color: black;">()</span></span>:
    L = []
    <span style="color: black;">while</span> <span style="color: black;">True:</span>name = input(<span style="color: black;">"姓名:"</span>)
    <span style="color: black;">if</span> <span style="color: black;">not</span> <span style="color: black;">name:</span>
    <span style="color: black;">break</span>
    age = input(<span style="color: black;">"年龄:"</span>)
    score = input(<span style="color: black;">"成绩:"</span>)
    s = Student(name, age, score)
    L.append(s)
    <span style="color: black;">return</span> L


    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">output_student</span><span style="color: black;">(lst)</span></span>:
    <span style="color: black;">for</span> i <span style="color: black;">in</span> <span style="color: black;">lst:</span>
    print(<span style="color: black;">"姓名:%s 年龄:%s 成绩:%s"</span> % (i.name, i.age, i.score))


    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">main</span><span style="color: black;">()</span></span>:
    L = input_student()
    output_student(L)


    main()<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/dfic-imagehandler/d27e3066-4109-4a82-99ec-988dc83cd88f~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723899766&amp;x-signature=oy4C%2B4U8CP5kAv3ZPP0XxOzZGTE%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">2.定义一个类Huamn(人类),定义函数input_human录入信息,main调用<span style="color: black;">表示</span>信息</h1><span style="color: black;"># 有三个属性: 姓名name,年龄age,家庭住址address (<span style="color: black;">能够</span>省略<span style="color: black;">无</span>)</span>
    <span style="color: black;"># 有<span style="color: black;">办法</span>: show_info 用来<span style="color: black;">表示</span>人的信息,update_age用来让这个人的年龄<span style="color: black;">增多</span>一岁</span>
    <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">Huamn</span>():</span>
    total_count = <span style="color: black;">0</span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">__init__</span><span style="color: black;">(<span style="color: black;">self</span>, name, age, address=None)</span></span>:
    <span style="color: black;">self</span>.name = name
    <span style="color: black;">self</span>.age = age
    <span style="color: black;">self</span>.address = address
    <span style="color: black;">self</span>.__class_<span style="color: black;">_</span>.total_count += <span style="color: black;">1</span>

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">show_info</span><span style="color: black;">(<span style="color: black;">self</span>)</span></span>:
    print(<span style="color: black;">"姓名:%s 年龄:%s <span style="color: black;">位置</span>:%s"</span> % (<span style="color: black;">self</span>.name, <span style="color: black;">self</span>.age, <span style="color: black;">self</span>.address))

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">updata_age</span><span style="color: black;">(<span style="color: black;">self</span>)</span></span>:
    <span style="color: black;">self</span>.age += <span style="color: black;">1</span>

    @classmethod
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">get_huamn_count</span><span style="color: black;">(cls)</span></span>:
    <span style="color: black;">return</span> cls.total_count

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">__del__</span><span style="color: black;">(<span style="color: black;">self</span>)</span></span>:
    <span style="color: black;">self</span>.__class_<span style="color: black;">_</span>.total_count -= <span style="color: black;">1</span>


    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">input_human</span><span style="color: black;">()</span></span>:
    L = []
    <span style="color: black;">while</span> <span style="color: black;">True:</span>
    name = input(<span style="color: black;">"姓名:"</span>)
    <span style="color: black;">if</span> <span style="color: black;">not</span> <span style="color: black;">name:</span>
    <span style="color: black;">break</span>
    age = int(input(<span style="color: black;">"年龄:"</span>) <span style="color: black;">or</span> <span style="color: black;">0</span>)
    address = input(<span style="color: black;">"<span style="color: black;">位置</span>:"</span>)
    s = Huamn(name, age, address)
    L.append(s)
    <span style="color: black;">return</span> L


    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">main</span><span style="color: black;">()</span></span>:
    L = input_human()
    <span style="color: black;">for</span> obj <span style="color: black;">in</span> <span style="color: black;">L:</span>
    obj.show_info() <span style="color: black;"># 列出所有人的信息</span>
    <span style="color: black;">for</span> obj <span style="color: black;">in</span> <span style="color: black;">L:</span>obj.updata_age()<span style="color: black;"># 让所有人都长一岁</span>
    <span style="color: black;">for</span> obj <span style="color: black;">in</span> <span style="color: black;">L:</span>
    obj.show_info() <span style="color: black;"># 再次列表所有人的信息</span>
    print(<span style="color: black;">"当前总人数是%s"</span> % Huamn.get_huamn_count())


    main()<h1 style="color: black; text-align: left; margin-bottom: 10px;">3.写一个<span style="color: black;">自动</span>车类有骑行<span style="color: black;">办法</span>,调用时<span style="color: black;">表示</span>骑行里程km,再写一个电动<span style="color: black;">自动</span>车类继承<span style="color: black;">第1</span>个类,添加电池电量属性,<span style="color: black;">同期</span>有两个<span style="color: black;">办法</span></h1><span style="color: black;"># fill_charge(vol) 用来充电, vol 为电量(度), run(km) <span style="color: black;">办法</span>用于骑行,每骑行10km消耗电量1度</span>
    <span style="color: black;"># 当电量消耗尽时调用Bicycle的run<span style="color: black;">办法</span>骑行并<span style="color: black;">表示</span>骑行结果</span>
    <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">Bycycle</span>():</span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">run</span><span style="color: black;">(<span style="color: black;">self</span>, km)</span></span>:
    print(<span style="color: black;">"<span style="color: black;">自动</span>车骑行了%s<span style="color: black;">千米</span>"</span> % km)


    <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">Ebicycle</span>(<span style="color: black;">Bycycle</span>):</span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">__init__</span><span style="color: black;">(<span style="color: black;">self</span>, valume)</span></span>:
    <span style="color: black;">self</span>.valume = valume

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">fill_charge</span><span style="color: black;">(<span style="color: black;">self</span>, vol)</span></span>:
    <span style="color: black;">self</span>.valume += vol

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">run</span><span style="color: black;">(<span style="color: black;">self</span>, km)</span></span>:
    e_km = min(km, <span style="color: black;">self</span>.valume * <span style="color: black;">10</span>)
    <span style="color: black;">self</span>.valume -= e_km / <span style="color: black;">10</span> <span style="color: black;"># 电量消耗</span>
    <span style="color: black;">if</span> e_km &gt; <span style="color: black;">0</span>:
    print(<span style="color: black;">"电动<span style="color: black;">自动</span>车骑行了"</span>, e_km, <span style="color: black;">"<span style="color: black;">千米</span>"</span>)
    <span style="color: black;">if</span> km &gt; <span style="color: black;">e_km:</span> <span style="color: black;"># 判断没电后行驶的过程</span>
    <span style="color: black;">super</span>().run(km - e_km)


    b = Ebicycle(<span style="color: black;">5</span>)
    b.run(<span style="color: black;">10</span>)
    b.run(<span style="color: black;">100</span>)
    b.fill_charge(<span style="color: black;">6</span>)
    b.run(<span style="color: black;">70</span>)<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/dfic-imagehandler/c68de0cf-c562-4afd-9516-b7131e0a93fc~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723899766&amp;x-signature=sn2fwd2JnuApiMf3lB6GBUN3XfI%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">4.写一个实现迭代器协议的类 Primes 让此类<span style="color: black;">能够</span>生成从b<span style="color: black;">起始</span>的n个素数</h1><span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">Primes</span>:</span>
    @staticmethod
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">__isprime</span><span style="color: black;">(x)</span></span>:
    <span style="color: black;">for</span> i <span style="color: black;">in</span> range(<span style="color: black;">2</span>, x):
    <span style="color: black;">if</span> x % i == <span style="color: black;">0</span>:
    <span style="color: black;">return</span> False
    <span style="color: black;">return</span> True

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">__init__</span><span style="color: black;">(<span style="color: black;">self</span>, b, n)</span></span>:
    <span style="color: black;">self</span>.<span style="color: black;">begin</span> = b
    <span style="color: black;">self</span>.count = n

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">__iter__</span><span style="color: black;">(<span style="color: black;">self</span>)</span></span>:
    <span style="color: black;">self</span>.cur_pos = <span style="color: black;">self</span>.<span style="color: black;">begin</span> <span style="color: black;"># 设置迭代的<span style="color: black;">初始</span>值</span>
    <span style="color: black;">self</span>.cur_count = <span style="color: black;">0</span> <span style="color: black;"># 用于记录已生成几个</span>
    <span style="color: black;">return</span> <span style="color: black;">self</span>

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">__next__</span><span style="color: black;">(<span style="color: black;">self</span>)</span></span>:
    <span style="color: black;"># 已完成生成, 不<span style="color: black;">必须</span>再生成, 我停止迭代</span>
    <span style="color: black;">if</span> <span style="color: black;">self</span>.cur_count &gt;= <span style="color: black;">self</span>.<span style="color: black;">count:</span>raise StopIteration<span style="color: black;">self</span>.cur_count += <span style="color: black;">1</span> <span style="color: black;"># 计数加1</span>
    <span style="color: black;">while</span> <span style="color: black;">True:</span>
    <span style="color: black;">if</span> <span style="color: black;">self</span>.__isprime(<span style="color: black;">self</span>.cur_pos):
    v = <span style="color: black;">self</span>.cur_pos
    <span style="color: black;">self</span>.cur_pos += <span style="color: black;">1</span>
    <span style="color: black;">return</span> v
    <span style="color: black;">self</span>.cur_pos += <span style="color: black;">1</span> <span style="color: black;"># 为下一次循环做准备</span>


    <span style="color: black;">for</span> x <span style="color: black;">in</span> Primes(<span style="color: black;">10</span>, <span style="color: black;">4</span>):
    print(x) <span style="color: black;"># 11 13 17 19</span>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">5.实现文件的复制(<span style="color: black;">意见</span><span style="color: black;">运用</span>二进制方式进行操作)</h1><span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">mycp</span><span style="color: black;">(src_file, dst_file)</span>:</span>
    <span style="color: black;">
      src_file 源文件名
      dst_file <span style="color: black;">目的</span>文件名
    </span>
    <span style="color: black;">try</span>:
    <span style="color: black;">with</span>open(src_file,<span style="color: black;">rb</span>) <span style="color: black;">as</span> fr, open(dst_file, <span style="color: black;">wb</span>) <span style="color: black;">as</span> fw:
    <span style="color: black;"># <span style="color: black;">倘若</span>文件太大则分次进行搬移</span>
    <span style="color: black;">while</span> <span style="color: black;">True</span>:
    b = fr.read(<span style="color: black;">4096</span>)
    <span style="color: black;">if</span> <span style="color: black;">not</span> b: <span style="color: black;"># <span style="color: black;">倘若</span>字节串为空则停止复制</span>
    <span style="color: black;">break</span>
    fw.write(b)
    <span style="color: black;">except</span>:
    <span style="color: black;">return</span> <span style="color: black;">False</span>
    <span style="color: black;">return</span> <span style="color: black;">True</span>


    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">main</span><span style="color: black;">()</span>:</span>
    src = input(<span style="color: black;">"请输入源文件名: "</span>)
    dst = input(<span style="color: black;">"请输入<span style="color: black;">目的</span>文件名: "</span>)
    <span style="color: black;">if</span> mycp(src, dst):
    print(<span style="color: black;">"复制文件成功"</span>)
    <span style="color: black;">else</span>:
    print(<span style="color: black;">"复制文件失败"</span>)


    main()<h1 style="color: black; text-align: left; margin-bottom: 10px;">6.实现两个自定义列表的相加</h1><span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">MyList</span>:</span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">__init__</span><span style="color: black;">(<span style="color: black;">self</span>, iterable)</span></span>:
    <span style="color: black;">self</span>.data =

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">__repr__</span><span style="color: black;">(<span style="color: black;">self</span>)</span></span>:
    <span style="color: black;">return</span> <span style="color: black;">MyList(%r)</span> % <span style="color: black;">self</span>.data

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">__add__</span><span style="color: black;">(<span style="color: black;">self</span>, rhs)</span></span>:
    <span style="color: black;">return</span> MyList(<span style="color: black;">self</span>.data + rhs.data)

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">__mul__</span><span style="color: black;">(<span style="color: black;">self</span>, rhs)</span></span>:
    <span style="color: black;">return</span> MyList(<span style="color: black;">self</span>.data * rhs)


    L1 = MyList([<span style="color: black;">1</span>, <span style="color: black;">2</span>, <span style="color: black;">3</span>])
    L2 = MyList(range(<span style="color: black;">4</span>, <span style="color: black;">7</span>))
    L3 = L1 + L2
    print(<span style="color: black;">"L3 ="</span>, L3) <span style="color: black;"># MyList()</span>
    L4 = L1 * <span style="color: black;">2</span> <span style="color: black;"># 实现乘法运算</span>
    print(<span style="color: black;">L4 =</span>, L4) <span style="color: black;"># MyList()</span>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">7.实现有序集合类 OrderSet(), 能实现两个集合的交集 &amp;, 并集 | 补集 -, 对<span style="color: black;">叫作</span>补集 ^, ==, != 等操作(写集合相同)</h1><span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">OrderSet</span>:</span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">__init__</span><span style="color: black;">(<span style="color: black;">self</span>, iterable)</span></span>:
    <span style="color: black;">self</span>.data =

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">__repr__</span><span style="color: black;">(<span style="color: black;">self</span>)</span></span>:
    <span style="color: black;">return</span> <span style="color: black;">OrderSet(%r)</span> % <span style="color: black;">self</span>.data

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">__and__</span><span style="color: black;">(<span style="color: black;">self</span>, rhs)</span></span>:
    <span style="color: black;">return</span> OrderSet(set(<span style="color: black;">self</span>.data) &amp; set(rhs.data))

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">__or__</span><span style="color: black;">(<span style="color: black;">self</span>, rhs)</span></span>:
    <span style="color: black;">return</span> OrderSet(set(<span style="color: black;">self</span>.data) <span style="color: black;">| set(rhs.data))

      def __xor__(<span style="color: black;">self</span>, rhs):
      <span style="color: black;">return</span> OrderSet(set(<span style="color: black;">self</span>.data) ^ set(rhs.data))


      s1 = OrderSet()
      s2 = OrderSet()
      print(s1 &amp; s2) # OrderSet()
      print(s1 |</span> s2) <span style="color: black;"># OrderSet()</span>
    print(s1 ^ s2) <span style="color: black;"># OrderSet()</span>
    <span style="color: black;">if</span> OrderSet([<span style="color: black;">1</span>,<span style="color: black;">2</span>,<span style="color: black;">3</span>]) != OrderSet([<span style="color: black;">1</span>,<span style="color: black;">2</span>,<span style="color: black;">3</span>,<span style="color: black;">4</span>]):
    print(<span style="color: black;">"不相同"</span>) <span style="color: black;"># 不相同</span>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/dfic-imagehandler/69662a5b-988a-412a-ae79-a38efcb354f2~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1723899766&amp;x-signature=JnxjR6PpFkDnPaL2yq%2BDW7DZOlw%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">8.模拟英雄联盟写一个游戏<span style="color: black;">名人</span>的类</h1><span style="color: black;">"""
      (1).创建一个 Game_role的类
      (2) 构造<span style="color: black;">办法</span>中给对象封装 name, ad(攻击力), hp(血量) 三个属性
      (3) 创建一个attack<span style="color: black;">办法</span>,此<span style="color: black;">办法</span>是实例化两个对象,互相攻击的功能
      例:
      实例化一个对象 盖伦,ad为10, hp为100
      实例化另个一个对象 剑豪 ad为20, hp为80
      盖伦<span style="color: black;">经过</span>attack<span style="color: black;">办法</span>攻击剑豪,此<span style="color: black;">办法</span>要完成 谁攻击谁,谁掉了多少血, 还剩多少血的提示功能
      """</span>
    <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">Game_role</span>:</span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">__init__</span><span style="color: black;">(self, name, ad, hp)</span>:</span>
    self.name = name
    self.ad = ad
    self.hp = hp

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">attack</span><span style="color: black;">(self, p)</span>:</span>
    p.hp = p.hp - self.ad
    print(<span style="color: black;">"%s攻击了%s, %s掉了%s血, 还剩%s血"</span> % (self.name, p.name, p.name, self.ad, p.hp))


    p1 = Game_role(<span style="color: black;">盖伦</span>, <span style="color: black;">10</span>, <span style="color: black;">100</span>)
    p2 = Game_role(<span style="color: black;">剑豪</span>, <span style="color: black;">20</span>, <span style="color: black;">80</span>)

    p1.attack(p2) <span style="color: black;"># 盖伦攻击了剑豪, 剑豪掉了10血, 还剩70血</span>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">9.暴力摩托程序(完成下列需求)</h1><span style="color: black;">"""
      1.1 创建三个游戏<span style="color: black;">名人</span>,分别是:
      苍井井,女,18,攻击力ad为20,血量200
      东尼木木,男,20,攻击力ad为30,血量150
      波多多,女,19,攻击力ad为50,血量80
      1.2 创建三个游戏武器,分别是:
      平底锅,ad为20
      斧子,ad为50
      双节棍,ad为65
      1.3 创建三个游戏摩托车,分别是:
      小踏板,速度60迈
      雅马哈,速度80迈
      宝马,速度120迈
      1.4 完成下列需求(利用武器打人掉的血量为武器的ad + 人的ad):
      (1)苍井井骑着小踏板开着60迈的车行驶在赛道上
      (2)东尼木木骑着宝马开着120迈的车行驶在赛道上
      (3)波多多骑着雅马哈开着80迈的车行驶在赛道上
      (4)苍井井赤手空拳打了波多多20滴血,波多多还剩xx血
      (5)东尼木木赤手空拳打了波多多30滴血,波多多还剩xx血
      (6)波多多利用平底锅打了苍井井一平底锅,苍井井还剩xx血
      (7)波多多利用斧子打了东尼木木一斧子,东尼木木还剩xx血
      (8)苍井井骑着宝马打了骑着小踏板的东尼木木一双节棍,东尼木木哭了,还剩xx血
      (9)波多多骑着小踏板打了骑着雅马哈的东尼木木一斧子,东尼木木哭了,还剩xx血
      """</span>
    <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">GameRole</span>:</span>
    <span style="color: black;">"""游戏角色类"""</span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">__init__</span><span style="color: black;">(self, name, sex, age, ad, hp)</span>:</span>
    self.name = name
    self.sex = sex
    self.age = age
    self.ad = ad
    self.hp = hp

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">add_moto</span><span style="color: black;">(self, mo)</span>:</span>self.mo = mo<span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">attack</span><span style="color: black;">(self,p)</span>:</span>
    p.hp = p.hp - self.ad
    print(<span style="color: black;">%s赤手空拳打了%s%s滴血,%s还剩%s血</span> % (self.name, p.name, self.ad, p.name, p.hp))

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">add_weapon</span><span style="color: black;">(self, wea)</span>:</span>self.wea = wea<span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">road_rush</span><span style="color: black;">(self, p)</span>:</span>
    p.hp = p.hp - self.ad - self.wea.ad
    print(<span style="color: black;">%s骑着%s打了骑着%s的%s一%s,%s哭了,还剩%s血</span>\
    %(self.name, self.mo.name, p.mo.name, p.name, self.wea.name, p.name, p.hp))<span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">Weapon</span>:</span>
    <span style="color: black;">"""武器类"""</span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">__init__</span><span style="color: black;">(self, name, ad)</span>:</span>
    self.name = name
    self.ad = ad

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">fight</span><span style="color: black;">(self,p1,p2)</span>:</span>p2.hp = p2.hp - p1.ad - self.ad<span style="color: black;">#print(%s利用%s打了%s一%s,%s还剩%s血 % </span>
    <span style="color: black;"># (p1.name,self.name,p2.name,self.name,p2.name,p2.hp))</span>
    print(<span style="color: black;">{0}利用{1}打了{2}一{1},{2}还剩{3}血</span>.format(p1.name, self.name, p2.name, p2.hp))<span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">Moto</span>:</span>
    <span style="color: black;">"""摩托车类"""</span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">__init__</span><span style="color: black;">(self, name, speed)</span>:</span>
    self.name = name
    self.speed = speed

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">drive</span><span style="color: black;">(self, p)</span>:</span>
    print(<span style="color: black;">%s骑着%s开着%d迈的车行驶在赛道上</span> % (p.name, self.name, self.speed))


    p1 = GameRole(<span style="color: black;">苍井井</span>, <span style="color: black;">女</span>, <span style="color: black;">18</span>, <span style="color: black;">20</span>, <span style="color: black;">200</span>)
    p2 = GameRole(<span style="color: black;">东尼木木</span>, <span style="color: black;">男</span>, <span style="color: black;">20</span>, <span style="color: black;">30</span>, <span style="color: black;">150</span>)
    p3 = GameRole(<span style="color: black;">波多多</span>, <span style="color: black;">女</span>, <span style="color: black;">19</span>, <span style="color: black;">50</span>, <span style="color: black;">80</span>)

    w1 = Weapon(<span style="color: black;">平底锅</span>, <span style="color: black;">20</span>)
    w2 = Weapon(<span style="color: black;">斧子</span>, <span style="color: black;">50</span>)
    w3 = Weapon(<span style="color: black;">双节棍</span>, <span style="color: black;">65</span>)

    m1 = Moto(<span style="color: black;">小踏板</span>, <span style="color: black;">60</span>)
    m2 = Moto(<span style="color: black;">雅马哈</span>, <span style="color: black;">80</span>)
    m3 = Moto(<span style="color: black;">宝马</span>, <span style="color: black;">120</span>)

    <span style="color: black;"># 组合: 给p1 对象封装了一个属性,属性值 m1这个对象</span>
    p1.add_moto(m1)
    p1.mo.drive(p1) <span style="color: black;"># 苍井井骑着小踏板开着60迈的车行驶在赛道上</span>

    p2.add_moto(m2)
    p2.mo.drive(p2) <span style="color: black;"># 东尼木木骑着雅马哈开着80迈的车行驶在赛道上</span>p3.add_moto(m3)
    p3.mo.drive(p3)<span style="color: black;"># 波多多骑着宝马开着120迈的车行驶在赛道上</span>

    p1.attack(p3) <span style="color: black;"># 苍井井赤手空拳打了波多多20滴血,波多多还剩60血</span>
    p2.attack(p3) <span style="color: black;"># 东尼木木赤手空拳打了波多多30滴血,波多多还剩30血</span>p3.add_weapon(w1)
    p3.wea.fight(p3, p1)<span style="color: black;"># 波多多利用平底锅打了苍井井一平底锅,苍井井还剩130血</span>
    p3.add_weapon(w2)
    p3.wea.fight(p3, p2) <span style="color: black;"># 波多多利用斧子打了东尼木木一斧子,东尼木木还剩50血</span>p1.add_moto(m3)
    p1.add_weapon(w3)
    p2.add_moto(m1)
    p1.road_rush(p2)<span style="color: black;"># 苍井井骑着宝马打了骑着小踏板的东尼木木一双节棍,东尼木木哭了,还剩-35血</span>

    p3.add_moto(m1)
    p2.add_moto(m2)
    p3.road_rush(p2) <span style="color: black;"># 波多多骑着小踏板打了骑着雅马哈的东尼木木一斧子,东尼木木哭了,还剩-135血</span>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">10.有1000个员工<span style="color: black;">倘若</span>几个员工对象的姓名和性别相同,这是一个人,请对这1000个员工做去重</h1><span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">Employee</span>:</span>
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">__init__</span><span style="color: black;">(<span style="color: black;">self</span>, name, age, sex, partment)</span></span>:
    <span style="color: black;">self</span>.name = name<span style="color: black;">self</span>.age = age
    <span style="color: black;">self</span>.sex = sex
    <span style="color: black;">self</span>.partment = partment
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">__hash__</span><span style="color: black;">(<span style="color: black;">self</span>)</span></span>:
    <span style="color: black;">return</span> hash(<span style="color: black;">%s%s</span><span style="color: black;">%(self.name, self.sex)</span>)
    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">__eq__</span><span style="color: black;">(<span style="color: black;">self</span>, other)</span></span>:
    <span style="color: black;">if</span> <span style="color: black;">self</span>.name == other.name<span style="color: black;">and</span> <span style="color: black;">self</span>.sex == other.<span style="color: black;">sex:</span>
    <span style="color: black;">return</span> True


    employ_lst = []
    <span style="color: black;">for</span> i <span style="color: black;">in</span> range(<span style="color: black;">200</span>):
    employ_lst.append(Employee(<span style="color: black;">echo</span>, i, <span style="color: black;">male</span>, <span style="color: black;">python</span>))
    <span style="color: black;">for</span> i <span style="color: black;">in</span> range(<span style="color: black;">200</span>):
    employ_lst.append(Employee(<span style="color: black;">Rubicon</span>, i, <span style="color: black;">male</span>, <span style="color: black;">python</span>))
    <span style="color: black;">for</span> i <span style="color: black;">in</span> range(<span style="color: black;">200</span>):
    employ_lst.append(Employee(<span style="color: black;">master</span>, i, <span style="color: black;">male</span>, <span style="color: black;">python</span>))

    <span style="color: black;"># print(employ_lst)</span>
    <span style="color: black;"># set集合的去重机制: 先调用hash,再调用eq,eq不是每次都触发,<span style="color: black;">仅有</span>hash值相等的时候才会触发</span>
    employ_set = set(employ_lst)
    <span style="color: black;">for</span> person <span style="color: black;">in</span> <span style="color: black;">employ_set:</span>
    print(person.__dict_<span style="color: black;">_</span>)<h1 style="color: black; text-align: left; margin-bottom: 10px;">11.反射实现选课系统</h1><span style="color: black;">"""userinfo文件内容
      echo|123456|Manager
      jojo|666|Student
      master|2222|Teacher
      """</span>
    <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">Manager</span>:</span>
    OPERATE_DIC = [
    (<span style="color: black;">创造学生账号</span>, <span style="color: black;">create_student</span>),
    (<span style="color: black;">创建课程</span>, <span style="color: black;">create_course</span>),
    (<span style="color: black;">查看学生信息</span>, <span style="color: black;">check_student_info</span>),
    ]

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">__init__</span><span style="color: black;">(self, name)</span>:</span>
    self.name = name

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">create_student</span><span style="color: black;">(self)</span>:</span>
    print(<span style="color: black;">创建学生账号</span>)

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">create_course</span><span style="color: black;">(self)</span>:</span>
    print(<span style="color: black;">创建课程</span>)

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">check_student_info</span><span style="color: black;">(self)</span>:</span>
    print(<span style="color: black;">查看学生信息</span>)


    <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">Student</span>:</span>
    OPERATE_DIC = [
    (<span style="color: black;">查看所有课程</span>, <span style="color: black;">check_course</span>),
    (<span style="color: black;"><span style="color: black;">选取</span>课程</span>, <span style="color: black;">choose_course</span>),
    (<span style="color: black;">查看已<span style="color: black;">选取</span>的课程</span>, <span style="color: black;">choosed_course</span>)
    ]

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">__init__</span><span style="color: black;">(self, name)</span>:</span>
    self.name = name

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">check_course</span><span style="color: black;">(self)</span>:</span>
    print(<span style="color: black;">check_course</span>)

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">choose_course</span><span style="color: black;">(self)</span>:</span>
    print(<span style="color: black;">choose_course</span>)

    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">choosed_course</span><span style="color: black;">(self)</span>:</span>
    print(<span style="color: black;">查看已<span style="color: black;">选取</span>的课程</span>)


    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">login</span><span style="color: black;">()</span>:</span>
    username = input(<span style="color: black;">user: </span>)
    password = input(<span style="color: black;">pwd: </span>)
    <span style="color: black;">with</span> open(<span style="color: black;">userinfo</span>) <span style="color: black;">as</span> f:
    <span style="color: black;">for</span> line <span style="color: black;">in</span>f:
    user, pwd, ident = line.strip().split(<span style="color: black;">|</span>) <span style="color: black;"># ident = Manager</span>
    <span style="color: black;">if</span> user == username <span style="color: black;">and</span> pwd == password:
    print(<span style="color: black;">登录成功</span>)
    <span style="color: black;">return</span> username, ident


    <span style="color: black;"><span style="color: black;">def</span> <span style="color: black;">main</span><span style="color: black;">()</span>:</span>usr, id = login()
    print(<span style="color: black;">user,id :</span>, usr, id)
    file = sys.modules[<span style="color: black;">__main__</span>]
    cls = getattr(file, id) <span style="color: black;"># Manager = getattr(当前文件,Manager)</span>obj = cls(usr)
    operate_dic = cls.OPERATE_DIC<span style="color: black;">while</span> <span style="color: black;">True</span>:
    <span style="color: black;">for</span> num, i <span style="color: black;">in</span> enumerate(operate_dic, <span style="color: black;">1</span>):
    print(num, i[<span style="color: black;">0</span>])
    choice = int(input(<span style="color: black;">num &gt;&gt;&gt;</span>))
    choice_item = operate_dic
    getattr(obj, choice_item[<span style="color: black;">1</span>])()


    <span style="color: black;">if</span> __name__ == <span style="color: black;">__main__</span>:
    main()<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">最后,<span style="color: black;">博主</span>想说:我是一名python<span style="color: black;">研发</span>工程师,整理了一套最新的python系统学习教程,想要这些资料的<span style="color: black;">能够</span>关注私信<span style="color: black;">博主</span>“01”<span style="color: black;">就可</span>(免费分享哦)<span style="color: black;">期盼</span>能对你有所<span style="color: black;">帮忙</span>。</p>




4lqedz 发表于 2024-10-1 13:42:16

外贸论坛是我们的,责任是我们的,荣誉是我们的,成就是我们的,辉煌是我们的。

4lqedz 发表于 前天 21:06

外贸论坛是我们的,责任是我们的,荣誉是我们的,成就是我们的,辉煌是我们的。
页: [1]
查看完整版本: Python语法示例——面向对象