求知 文章 文库 Lib 视频 iPerson 课程 认证 咨询 工具 讲座 Modeler   模型库  
会员   
 


人工智能、机器学习& TensorFlow
6月30日-7月1日 直播



基于 UML 和EA进行分析设计
7月30-31日 北京+线上



图数据库与知识图谱
8月21日-22日 北京+线上
 
追随技术信仰

随时听讲座
每天看新闻
 
 
  Python 教程
1.Python 简介
2.Python 入门
3.Python 语法
4.Python 注释
5.Python 变量
6.Python 数据类型
7.Python 数字
8.Python Casting
9.Python 字符串
10.Python 布尔
11.Python 运算符
12.Python 列表
13.Python 元组
14.Python 集合
15.Python 字典
16.Python If ... Else
17.Python While 循环
18.Python For 循环
19.Python 函数
20.Python 数组
21.Python Lambda
22.Python Lambda
23.Python 继承
24.Python 迭代
25.Python 作用域
26.Python 模块
27.Python 日期
28.Python JSON
29.Python RegEx
30.Python PIP
31.Python Try Except
32.Python 命令行输入
33.Python 字符串格式化
Python 文件处理
1.Python 文件打开
2.Python 文件写入
3.Python 删除文件
Matplotlib教程
1.数据可视化是什么
2.Matplotlib是什么
3.Matplotlib下载和安装
4.Matplotlib.pyplot接口汇总
5.第一个Matplotlib绘图程序
6.PyLab绘制曲线图
7.Matplotlib figure图形对象
8.Matplotlib axes类使用详解
9.Matplotlib subplot()函数用法详解
10.Matplotlib subplots()函数详解
11.Matplotlib subplot2grid()函数详解
12.Matplotlib设置网格格式
13.Matplotlib设置坐标轴格式
14.Matplotlib设置坐标轴范围
15.Matplotlib设置刻度和标签
16.Matplotlib中文乱码解决方案
17.Matplotlib双轴图
18.Matplotlib设置刻度和标签
19.Matplotlib柱状图
20.Matplotlib直方图
21.Matplotlib饼状图
22.Matplotlib折线图
23.Python Matplotlib散点图
24.Matplotlib等高线图
25.Matplotlib振动图
26.Matplotlib箱型图
27.Matplotlib提琴图
28.Python Matplotlib 3D绘图详解
29.Matplotlib绘制文本
30.Matplotlib数学表达式
31.Matplotlib image图像处理
32.Matplotlib转换对象
NumPy 教程
1.NumPy是什么
2.NumPy下载与安装
3.NumPy ndarray对象
4.NumPy 简介
5.NumPy 入门
6.NumPy 数组索引
7.NumPy 数组裁切
8.NumPy 数据类型
9.NumPy 副本/视图
10.NumPy 数组形状
11.NumPy 数组重塑
12.NumPy 数组迭代
13.NumPy 数组连接
14.NumPy 数组拆分
15.NumPy 数组搜索
16.NumPy 数组排序
17.NumPy 数组过滤
18.NumPy 中的随机数
19.NumPy ufuncs
机器学习教程
1. 入门
2. 平均中位数模式
3. 标准差
4. 百分位数
5. 数据分布
6. 正态数据分布
7. 散点图
8. 线性回归
9. 多项式回归
10. 多元回归
11. 缩放
12. 训练/测试
13. 决策树
Python MySQL教程
1. Python_MySQL入门
2. Python 创建数据库
3. Python 创建表
4. Python 插入表
5. Python 从表中选取
6. Python 使用筛选器来选取
7. Python 结果排序
8. Python 删除记录
9. Python 删除表
10. Python 更新表
11. Python 限定结果
12. Python 加入
Python MongoDB
1. Python MongoDB入门
2. Python 创建数据库
3.Python MongoDB 创建集合
4.Python MongoDB 插入文档
5.Python MongoDB 查找
6.Python MongoDB 查询
7.Python MongoDB 排序
8.Python MongoDB 删除文档
9.Python 删除集合
10. Python MongoDB Update
11.Python MySQL Limit
 

 
目录
Python 继承
216 次浏览
8次  

Python 继承

继承允许我们定义继承另一个类的所有方法和属性的类。

父类 是继承的类,也称为基类。

子类 是从另一个类继承的类,也称为派生类。

创建父类

任何类都可以是父类,因此语法与创建任何其他类相同:

实例

创建一个名为Person 的类,其中包含 firstname 和 lastname 属性以及 printname 方法:

class Person:
  def __init__(self, fname, lname):
    self.firstname = fname
    self.lastname = lname

  def printname(self):
    print(self.firstname, self.lastname)

# 使用 Person 来创建对象,然后执行 printname 方法:

x = Person("Bill", "Gates")
x.printname()
                               

创建子类

要创建从其他类继承功能的类,请在创建子类时将父类作为参数发送:

实例

创建一个名为 Student 的类,它将从 Person 类继承属性和方法:

class Student(Person):
  pass

注释: 如果您不想向该类添加任何其他属性或方法,请使用 pass 关键字。

现在,Student 类拥有与 Person 类相同的属性和方法。

实例

使用 Student 类创建一个对象,然后执行 printname 方法:

x = Student("Elon", "Musk")
x.printname()

添加 __init__() 函数

到目前为止,我们已经创建了一个子类,它继承了父类的属性和方法。

我们想要把 __init__() 函数添加到子类(而不是 pass 关键字)。

注释: 每次使用类创建新对象时,都会自动调用 __init__() 函数。

实例

为 Student 类添加 __init__() 函数:

class Student(Person):
  def __init__(self, fname, lname):
    # 添加属性等

当您添加 __init__() 函数时,子类将不再继承父的 __init__() 函数。

注释: 子的 __init__() 函数会覆盖对父的 __init__() 函数的继承。

如需保持父的 __init__() 函数的继承,请添加对父的 __init__() 函数的调用:

实例

class Student(Person):
  def __init__(self, fname, lname):
    Person.__init__(self, fname, lname)

现在,我们已经成功添加了 __init__() 函数,并保留了父类的继承,我们准备好在 __init__() 函数中添加功能了。

使用 super() 函数

Python 还有一个 super() 函数,它会使子类从其父继承所有方法和属性:

实例

class Student(Person):
  def __init__(self, fname, lname):
    super().__init__(fname, lname)

通过使用 super() 函数,您不必使用父元素的名称,它将自动从其父元素继承方法和属性。

添加属性

实例

把名为 graduationyear 的属性添加到 Student 类:

class Student(Person):
  def __init__(self, fname, lname):
    super().__init__(fname, lname)
    self.graduationyear = 2019

在这例子中,2019 年应该是一个变量,并在创建 student 对象时传递到 Student 类。为此,请在 __init__() 函数中添加另一个参数:

实例

添加 year 参数,并在创建对象时传递正确的年份:

class Student(Person):
  def __init__(self, fname, lname, year):
    super().__init__(fname, lname)
    self.graduationyear = year

x = Student("Elon", "Musk", 2019)

添加方法

实例

把名为 welcome 的方法添加到 Student 类:

class Student(Person):
  def __init__(self, fname, lname, year):
    super().__init__(fname, lname)
    self.graduationyear = year

  def welcome(self):
    print("Welcome", self.firstname, self.lastname, "to the class of", self.graduationyear)

提示: 如果您在子类中添加一个与父类中的函数同名的方法,则将覆盖父方法的继承。


您可以捐助,支持我们的公益事业。

1元 10元 50元





认证码: 验证码,看不清楚?请点击刷新验证码 必填



216 次浏览
8次