您现在的位置是:首页 > Python 当前栏目 Python入门-面向对象 Python 对象 继承 入门 2023-03-02 11:13:59 时间 本文介绍Python的面向对象: (1)类的定义 (2)类的成员 (3)继承 (4)重写父类方法 1、类的定义 (1)定义 class 类名: 类体 (2)类的构造函数: def __init___(self,形参列表): #成员方法第一个参数通常叫self,代表本类的实例,而非类(self是对象,不是类) 方法体 2、类的成员 (1)成员变量 class Test: age=9 #共有 __b="name" #以两个下划线开头的,为私有属性; 在方法名前面加两个下划线,也表示私有 t=Test() #实例化对象,不需要用new关键字 (2)成员方法 class Test: age=9 def say(self,age): self.age=age #成员方法调用成员属性,可以用self调用,类型java中的this t=Test() #实例化对象,不需要用new关键字 t.say 3、继承 (1)定义 class 子类(父类): #不需要使用extends关键字;且python是支持多继承的 类体 (2)当父类中有多个相同的方法是,默认是调用括号中排最前的父类方法 class A: def say(self): print("a") class B: def say(self): print("b") class C(A,B): def __init__(self): self.say() c=C() #输出a 4、重写父类方法 class A: def say(self): print("a") class C(A): def say(self): print("c") c=C() c.say() #输出c 本文地址: Python入门-面向对象 相关文章 python安装模块小结 Python之函数小结 回环矩阵 python Python-unittest测试框架 Python 基础知识教程 python系列教程21 Python基础回顾总结 Java switch语句 python 多线程 Python-接口开发入门 Python 多线程 python抓取svn log python操作excel小试牛刀 python线程池 Python3 字符串方法 Python基本语法元素 python 属性插槽 python函数的return Python模拟手机操作 Python:进度条tqdm