python类的继承与类方法调用

版权声明:QQ群:796245415 个人技术交流,禁止用作商业活动 https://blog.csdn.net/chen498858336/article/details/83904575
# coding=utf-8
__author__ = "Chen"


class Person(object):

    def __init__(self, name, age):
        self.name = name
        self.age = age
        self.weight = '900'

    def talk(self):
        print("person is talking....")
        print(self)
        print(self.__class__)
        print(self.__dict__)


class Chinese(Person):

    def __init__(self, name, age, language):  # 先继承,在重构
        Person.__init__(self, name, age)  # 继承父类的构造方法,也可以写成:super(Chinese,self).__init__(name,age)
        self.language = language    # 定义类的本身属性

    def said_age(self):
        print(self.age)

    def said_name(self):
        print(self.name)

    def said_weight(self):
        print(self.weight)  # 直接调用继承属性

    def other_method(self):

        self.talk()
        self.said_name()  # 类的内部方法互相调用

    def talk(self):
        print("Person's talk is too low, i will  rewrite it! ")  # 父类方法重写


c = Chinese('Bill', 22, 'Chinese')
c.other_method()
c.said_age()
c.said_weight()
c.talk()
print c.language

"""function 之间互调"""


def tom():
    print("i am tom cat")


def jerry():
    tom()
    print("我把楼上的tom方法调来用了")
jerry()

"""模块导入"""


"""
from module import function as fn
from module import class1, class2   # 先实例化,在调用类方法,或者直接继承,直接调用用self.fn()
from module import *
import module
from package.module import function1, function2

 """

猜你喜欢

转载自blog.csdn.net/chen498858336/article/details/83904575