zoukankan      html  css  js  c++  java
  • python 单继承的实现

    第一类单继承的实现
    from person import Person
    from student import Student
    from worker import Worker

    per = Person("aa", 1, 2)
    stu = Student("tom", 18, 12345, 110)
    print(stu.name, stu.age)
    stu.run()
    print(stu.stuId)
    #print(stu.__money)私有属性
    print(stu.getMoney()) #通过继承过来的共有方法访问私有属性
    #stu.stuFunc()

    wor = Worker("lilei", 20, 111)
    print(wor.name, wor.age)
    wor.eat("apple")

    print(per.getMoney()

    第二类person
    class Person(object):
    def __init__(self, name, age, money):
    self.name = name
    self.age = age
    self.__money = money

    def setMoney(self, money):
    self.__money = money
    def getMoney(self):
    return self.__money

    def run(self):
    print("run")

    def eat(self, food):
    print("eat " + food)

    第三类student
    from person import Person

    class Student(Person):
    def __init__(self, name, age, money, stuId):
    #调用父类中的__init__
    super(Student, self).__init__(name, age, money)
    #子类可以有一些自己独有的属性
    self.stuId = stuId

    def stuFunc(self):
    print(self.__money)

    第四类worker
    from person import Person

    class Worker(Person):
    def __init__(self, name, age, money):
    #调用父类中的__init__
    super(Worker, self).__init__(name, age, money)


  • 相关阅读:
    linux学习之路第八天(linux文件权限详解)
    linux学习之路第八天(组管理和权限管理)
    python 多线程示例
    python scapy 网卡发包
    python scapy 网卡抓包
    python 返回数组的索引
    MPLS 网络中的 MTU
    mysql 导入导出sql文件
    linux 修改MTU值
    ovs 源mac, 目的src 互换
  • 原文地址:https://www.cnblogs.com/pygo/p/12303710.html
Copyright © 2011-2022 走看看