Return to Video

Class Parent - Programming Foundations with Python

  • 0:00 - 0:02
    Okay, so here we have our design of the
  • 0:02 - 0:04
    classes we are going to build up on the top
  • 0:04 - 0:07
    right hand corner, and what I've done thus far is
  • 0:07 - 0:11
    created a new Python file, and I called it inheritance.py.
  • 0:11 - 0:14
    Now, based on our design, I'm going to go ahead
  • 0:14 - 0:18
    and create a class called Parent. [SOUND] There. The next
  • 0:18 - 0:21
    thing to do is to initialize the variables of class
  • 0:21 - 0:25
    Parent, variables like last_name and eye_color. So, to do that,
  • 0:25 - 0:28
    I will define this class as init method
  • 0:28 - 0:30
    or constructor. The first argument for this function,
  • 0:30 - 0:33
    we know, is self. And the two instance
  • 0:33 - 0:38
    variables for class Parent, we know, are self.last_name and
  • 0:38 - 0:42
    self.eye_color. Now the init function we know will
  • 0:42 - 0:45
    receive a couple of values as arguments. So, let
  • 0:45 - 0:47
    me add them in here, and we will
  • 0:47 - 0:51
    use these two arguments to initialize our instance variables.
  • 0:51 - 0:56
    Let me do that next. Alright. You may notice that this piece of code is pretty
  • 0:56 - 0:59
    similar to the code for class Movie that
  • 0:59 - 1:01
    we have written previously. By the way, one new
  • 1:01 - 1:03
    thing I will do here, is add a
  • 1:03 - 1:06
    quick print statement inside the init method. It will
  • 1:06 - 1:12
    print out, Parent Constructor Called. So, this print
  • 1:12 - 1:16
    statement will explicitly tell us, each time the init
  • 1:16 - 1:19
    method or the constructor of class Parent is called.
  • 1:19 - 1:21
    Alright, to make sure that this code that we
  • 1:21 - 1:24
    have written thus far actually works, let's go ahead
  • 1:24 - 1:27
    and use this class. I will define an instance
  • 1:27 - 1:31
    of class Parent and call it billy_cyrus, and will
  • 1:31 - 1:35
    provide the two arguments that are necessary. The first
  • 1:35 - 1:37
    of which is last_name and that happens to be
  • 1:37 - 1:41
    Cyrus, and the next one is eye_color which is,
  • 1:41 - 1:44
    let's say, blue. Now, a quick word
  • 1:44 - 1:48
    of caution. Ordinarily, I would have kept these
  • 1:48 - 1:49
    two things, the definition of the class
  • 1:49 - 1:52
    Parent and creating its instances. I would have
  • 1:52 - 1:56
    kept them in separate Python files. But I have kept them both here in the
  • 1:56 - 2:02
    same file for ease of demonstration. Okay, so to demonstrate
  • 2:02 - 2:06
    that the instance actually works,
  • 2:06 - 2:15
    I'll print out its last name. Let me save and then run this file. And
  • 2:15 - 2:18
    there's my output. It says the parent constructor
  • 2:18 - 2:21
    was called. Which seems appropriate, because we created
  • 2:21 - 2:24
    an instance called billy_cyrus. And as soon as
  • 2:24 - 2:26
    we did that, the class Parent's _initl_ method
  • 2:26 - 2:29
    got called, which had a print statement inside
  • 2:29 - 2:32
    it. And then, we printed out Billy Cyrus's
  • 2:32 - 2:37
    last name, which it printed out correctly. Okay, so far so good.
  • 2:37 - 2:41
    Now, you'll notice that there really isn't anything new that we've done here
  • 2:41 - 2:45
    thus far. The new thing we're going to try is called inheritance, which
  • 2:45 - 2:49
    will happen when we create the class called Child. Let's do that next.
Cím:
Class Parent - Programming Foundations with Python
Leírás:

more » « less
Video Language:
English
Team:
Udacity
Projekt:
UD036 - OOP
Duration:
02:50
Udacity Robot edited Angol subtitles for 07-05 Class Parent
Udacity Robot edited Angol subtitles for 07-05 Class Parent
Udacity Robot edited Angol subtitles for 07-05 Class Parent
Cogi-Admin edited Angol subtitles for 07-05 Class Parent

English subtitles

Felülvizsgálatok Compare revisions