python类的继承与类方法调用

本文详细介绍了Python中面向对象编程的实现方式,包括类的定义、继承与重构、方法的重写以及对象属性的调用。通过具体示例,展示了如何创建类、初始化对象、调用方法,并解释了Python中特殊方法如`__init__`和`__dict__`的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# 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

 """
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值