zoukankan      html  css  js  c++  java
  • 编程基础python学习2完结

    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方法中的内容。

  • 相关阅读:
    常用集体名词的用法
    囊中羞涩的表达
    《当幸福来敲门》观后感
    <肖申克的救赎>观后感
    心语4
    补充:回答网友的问题,如何不用路径,而直接将CImage画到DC中,之后DC一起显示.
    线程中对变量的用法
    添加按键变量数组,就是很多同种类型按键关联变量,这些变量是一个数组;
    不容按钮、下拉框 执行同一个函数或者同一种函数的用法
    CImage显示位图与CDC双缓冲冲突,使用路径层解决.
  • 原文地址:https://www.cnblogs.com/Yiren-33/p/6689491.html
Copyright © 2011-2022 走看看