What is a Class?

Data structures like lists and strings are extremely useful, but sometimes they aren’t enough to represent something you’re trying to implement. For example, let’s say we needed to keep track of a bunch of pets. We could represent a pet using a list by specifying the first element of the list as the pet’s name and the second element of the list as the pet’s species. This is very arbitrary and nonintuitive, however – how do you know which element is supposed to be which?

Classes give us the ability to create more complicated data structures that contain arbitrary content. We can create a Pet class that keeps track of the name and species of the pet in usefully named attributes called name and species, respectively.

What is an Instance?

Before we get into creating a class itself, we need to understand an important distinction. A class is something that just contains structure – it defines how something should be laid out or structured, but doesn’t actually fill in the content. For example, a Pet class may say that a pet needs to have a name and a species, but it will not actually say what the pet’s name or species is.

This is where instances come in. An instance is a specific copy of the class that does contain all of the content. For example, if I create a pet polly, with name "Polly" and species "Parrot", then polly is an instance of Pet.

This can sometimes be a very difficult concept to master, so let’s look at it from another angle. Let’s say that the government has a particular tax form that it requires everybody to fill out. Everybody has to fill out the same type of form, but the content that people put into the form differs from person to person. A classis like the form: it specifies what content should exist. Your copy of the form with your specific information if like an instance of the class: it specifies what the content actually is.

This is the basic instant for creating a class. The first word, class, indicates that we are creating a class. The second word, Pet, indicates the name of the class. The word in parentheses, object, is the class thatPet is inheriting from. We’ll get more into inheritance below, so for now all you need to know is that object is a special variable in Python that you should include in the parentheses when you are creating a new class.

class表示我们要新建一个类

pet是新建的类名

object是在python里一个特殊的变量,需要包含在新建类的圆括号里(知道怎么用就行了)

http://www.jesshamrick.com/2011/05/18/an-introduction-to-classes-and-inheritance-in-python/

movie_storyline和poster_image等是初始化函数的自变量,而self.title,self.storyline等是实例变量。

self.title=movie_title就是在用函数的自变量初始化实例变量。

对比,在定义新类和定义初始化函数的括号后面少了冒号:

对比,在调用Media.movie时,实际上是在调用init函数,而init函数里的self就是类的一个实例,在此处就是指向toy_story.问题出在此处没有给init函数里的除self其他各个参数赋值,当你需要print toy_story时程序不知道此值是什么应该输出什么所以此处会报错。

一旦init函数被调用,并且media.movie里的四个自变量获得了正确的值,所有与toy_story相关的变量都会被适当地初始化。因此当我们需要程序打印出我们需要的值时我们就能正确得到。

当我们创建好实例后,后台真正的操作是为我们的每一个实例创建一定的空间,在空间内 每个实例有自己的变量副本

Like this

由于关于类movie的每个实例都是唯一的,所以这些变量为“实例变量”

此时如果把init函数中movie_storyline赋值语句中的self删掉,那么如果print(toy_story.movie_storyline)会导致调用init函数的时候报错toy_story没有movie_storyline这个属性。

在类中定义,并与实例相关的函数:实例方法。

例如init()就是一个实例方法。并且每一个实例方法的第一个自变量都是self。

show_trailer是另外一个实例方法。打开的链接就储存在self.trailer_youtube_url中,所以要访问这个实例变量。

然后再在avatar这个实例中调用这个实例方法即可。

练习:

此处应该是调用的webbrowser的open函数而不是简单的一个名字所以应该是点·而不是下划线_

术语总结:

一个新的知识点:

fresh_tomatoes这个文件中有一个open_movies_page的函数

这个函数输入一个电影列表,会创建并输出一个html文件或网页来显示这些电影。(好牛杯!厉害了我的小西红柿...)

但是有一个重要前提是这个西红柿文件必须和你其他所有电影程序储存在同一个文件夹内才可以起作用。

面向对象编程中的高级概念

1.类变量(所有同类的实例都相同的变量)定义在类中。(类变量可能是个常量,python指南中说当它为常量最好用全大写。)like this.

三个引号表示可以在多行创建文档。

就可以打印出打引号的内容。

注意用法print(模块或者文件.类名和预定义的类变量)

面向对象编程的一大优势:reuse the code

划线的语法说明child类可以继承或重用所有parent类公开可用的所有东西。

要初始化所有从parent类那里继承的变量,例如last_name和eye_color,实际上要重用类parent中init方法,like this:

而child类自己的实例变量在后面初始化。

高亮处为创建一个child类的实例,当运行这条代码的时候child.__init__初始化函数会被立刻调用。

整个程序后台运行的过程:

运行child类的实例——>child__init__函数被调用——>init函数第一行就是打印所以先打印出Child Constuctor called——>parent类的init函数被调用,所以程序从4到5class parent定义处,parent__init__被调用——>Parent Constructor called被打印——>parent类的实例变量last_name和eye_color被初始化,类parent.__init__函数成功运行后——>控制回到4处,然后到8child类的实例变量被初始化。child类的init函数被成功运行——>child实例miley_cyrus被成功创建。——>接下来到9按顺序打印出需要打印的内容。

like this:

新知识点:重写函数(方法)

虽然在child类中没有明确地定义方法show_info但是child从parent那里继承了所有公开可用的信息包括show_info方法,所以可以直接用。结果like this,直接调用了parent类中定义的show_info方法。

新知识:方法覆盖(method overriding)

此时在child类中再定义和parent类同名的方法show_info,当调用这个方法时,child类的方法会覆盖掉parent类的方法。结果like this

输出了child类中定义的show_info方法中的内容。

转载于:https://www.cnblogs.com/Yiren-33/p/6689491.html

编程基础python学习2完结相关推荐

  1. 编程小白学python路线图_零基础Python学习路线图,让你少走弯路

    近几年Python的受欢迎程度可谓是扶摇直上,当然了学习的人也是愈来愈多.一些学习Python的小白在学习初期,总希望能够得到一份Python学习路线图,小编经过多方汇总为大家汇总了一份Python学 ...

  2. python自学路线-零基础Python学习路线,小白的进阶之路!

    近几年Python的受欢迎程度可谓是扶摇直上,当然了学习的人也是愈来愈多.一些学习Python的小白在学习初期,总希望能够得到一份Python学习路线图,小编经过多方汇总为大家汇总了一份Python学 ...

  3. 零基础Python学习路线图,小白的进阶之路!

    近几年Python的受欢迎程度可谓是扶摇直上,当然了学习的人也是愈来愈多.一些学习Python的小白在学习初期,总希望能够得到一份Python学习路线图,小编经过多方汇总为大家汇总了一份Python学 ...

  4. python自学行_有编程基础Python自学行吗?

    有编程基础Python自学行吗?目前Python比较火爆,计算机基础越好对学习Python新的编程语言越有利.大学计算机专业C语言不是很好的入门语言,理解起来有些抽象.零基础学Python很容易上手, ...

  5. 零基础Python学习路线图,Python学习不容错过

    近几年Python的受欢迎程度可谓是扶摇直上,当然了学习的人也是愈来愈多.一些学习Python的小白在学习初期,总希望能够得到一份Python学习路线图,小编经过多方汇总为大家汇总了一份Python学 ...

  6. python课程思维导图_零基础Python学习思维导图,记得收藏

    原标题:零基础Python学习思维导图,记得收藏 本文主要涵盖了 Python 编程的核心知识(暂不包括标准库及第三方库). 1.按顺序依次展示了以下内容的一系列思维导图:基础知识,数据类型(数字,字 ...

  7. python自学行吗-有编程基础Python自学行吗?

    有编程基础Python自学行吗?目前Python比较火爆,计算机基础越好对学习Python新的编程语言越有利.大学计算机专业C语言不是很好的入门语言,理解起来有些抽象.零基础学Python很容易上手, ...

  8. spark编程基础python版 pdf_Spark编程基础Python版-第5章-Spark-SQL.pdf

    <Spark编程基础(Python版)> 教材官网:/post/spark-python/ 温馨提示:编辑幻灯片母版,可以修改每页PPT的厦大校徽和底部文字 第5章Spark SQL (P ...

  9. 《爬虫与网络编程基础》学习

    <爬虫与网络编程基础>学习 任务1:计算机网络基础 Step1-2 Step 3:关于XML格式的学习 XML文件格式 XML文件的常见操作 1.构建xml格式文件 2. 保存XML文件 ...

最新文章

  1. 谈谈 Docker 网络
  2. Python3中的字符串
  3. 企业级 SpringBoot 教程 (二十三)异步方法
  4. vue在js上处理后台返回的数组_vuejs 根据后台返回数组,渲染图片路径
  5. PAT ---- 1029. 旧键盘(20)
  6. Navicat工具导出mySQL数据库某个视图结构的.sql脚本
  7. 工商银行黄金开户问答题答案
  8. u-boot移植随笔:移植过程问题及解决(本文章不时更新)
  9. 使C#代码现代化——第三部分:值
  10. Flutter 成功在鸿蒙上运行;微信 8.0 发布;支付宝和微信支付达到反垄断标准 | 极客头条...
  11. 计算机开机界面图片怎么修改,电脑win7系统怎么修改开机画面的方法
  12. bios sgx需要开启吗_Win10改Win7,UEFI改Legacy 启动,修改BIOS大全
  13. Linux alsa-lib c语言 播放wav音频
  14. fpga电平约束有什么作用_Xilinx FPGA的约束设计和时序分析总结
  15. RSS从入门到精通 .
  16. 推荐五款装机必备的常用软件
  17. R语言基础知识(1)-数据类型及其常用方法
  18. 仓储+调度,YOGO智能配送站能否改变外卖配送格局?...
  19. 德勤2020技术趋势报告 | 洞察5大重点趋势和12种宏观科技力量
  20. CTF-CRYPTO-2020新基建初赛-ezCrypto

热门文章

  1. Shell(12)——awk(2)
  2. 服务器同时登入会被挤下来吗_「英雄联盟手游」苹果iOS版LOL日服公测,日本服务器卡爆!...
  3. 关于文件操作的\r\n问题。
  4. RPM 包相关命令详解
  5. mysql的学习笔记
  6. rust windows 交叉编译_交叉编译问题,求指教
  7. Teamtalk源码分析
  8. mysql用supervisor管理_使用Supervisor管理进程
  9. Linux下mysql5.1修改密码,Linux下MySQL忘记密码
  10. linux+proc+原理,Linux内核中的Proc文件系统(一)