سرفصل های مهم
14.4 - Object Inheritance
توضیح مختصر
The parent class might be animal and then you might have a dog, a cat, and a bird, right? And that's probably the right logic to have right now, because you haven't written programs that are complex enough to require object orientation in terms of the code that you write. Those things are actually complex data and code and we don't have to worry about that, and so the benefit to us right now is it simplifies our programs to make use of objects that others create.
- زمان مطالعه 0 دقیقه
- سطح خیلی سخت
دانلود اپلیکیشن «زوم»
فایل ویدیویی
برای دسترسی به این محتوا بایستی اپلیکیشن زبانشناس را نصب کنید.
ترجمهی درس
متن انگلیسی درس
So multiple instances are we have one template and we make multiple variables out of that template, and they all kind of have the same shape because we’re starting from the same template. Inheritance is a different concept. Inheritance is we’re going to make more than one template. And we’re going to have a second template that’s related to a first template. We call the first one a parent template and the second one a child. And idea is it’s like we’re going to make a class, a template, that’s everything that’s this class has plus. We can either add capabilities or extend the capabilities of the parent. So the idea is this is just a form of store and reuse. And again, don’t get too worried about when you’re going to use this. I really am trying to define for you the notion of inheritance rather than teach you how to write inheritance in object orientation. That will come in time, perhaps years from now, when you actually will actually use this skill. But it does tell you something about how things are related. And if you say this extends that, then you say, okay. That just means that all of the features of the thing that’s being extended come in. Another term for this is subclasses. A subclass is a more specialized version of a parent class. Sometimes you have a parent class and a couple of different subclasses. The parent class might be animal and then you might have a dog, a cat, and a bird, right? And so there are things that animals have or mammals, might have animal, mammal, dog, cat, right? And so that might be a hierarchy of classes. Now, don’t get too stuck on that. It’s just a form of storing and reusing a functionality so that you don’t have to repeat yourself. And it’s not like the more inheritance you use, the better your programs are. Sometimes that’s not true at all. But we programmers just hate repeating ourselves and so if we can do it once and then reuse it, we like that. And so that’s really what inheritance is there for. So here’s a bit of code. This first part is unchanged. So we have a class called PartyAnimal. It’s got two variables, name and x, that are attributes of that. We have a constructor, and the whole idea of the constructor is just to grab that name parameter. And then we have the party, which just adds 1 and prints out the current party count. And that’s fine, we’ve done that before. Now we have a new class. So now we say class FootballFan and in parentheses here, we have the name of a different class. So what we’re saying is PartyAnimal, FootballFan is everything that PartyAnimal is plus whatever we define down here. Okay? So this is like an extension. It’s as if all this was pulled in, and then this is addition, okay? So a football fan has an x instance variable, a name instance variable, and a points instance variable. And has a constructor, a party method, and a touchdown method. And so all of these tings, all this stuff is stuck right there. It’s like it came in by us saying, the way I read this in English is class FootballFan extends PartyAnimal. Which means it includes everything PartyAnimal includes plus this extension stuff that we’re going to add, right? All the capabilities of PartyAnimal and more. And so if we’re going to use these, I mean the fact that we’ve extended, it doesn’t mean that this one goes away. And so we can still construct a PartyAnimal with the name of Sally and call the party method in that, and that does what it does. And then we can make a new object using the FootballFan template. So, and give it a name, and that FootballFan actually calls, there is no constructor here and so it’s actually calling this constructor, the one from PartyAnimal. Because PartyAnimal is included in FootballFan. And it’s setting the name, and x to 0, and the name to nothing, and then points to 0 also happens. So all three variables are present in this j object. And then we call the party method, which is really here. Runs the code. But then we also have now a touchdown method that can run this code, okay? And so you can see that this FootballFan has every capability that the PartyAnimal has, plus one more capability that we’ve added. And so that’s the basic idea of extension. You know, s.x is 0, the name is Sally, it runs s.party so x becomes 1. Right? That’s just a little object with a name s and away we go. If on the other hand we look at this next bit of line of code, right here, we end up with a FootballFan and a FootballFan inside of it has an x. It has a name and it has points. And at moment of constructor, x is 0, name is Jim, and points is 0. Then we call the party method. And the party method changes the x to x plus 1, so that becomes a 1. The touchdown method, I add 7 to the points so that becomes 7. And then it calls the party method, and so it’s calling within itself the party method. And so that calls that code and that causes this to turn into 2. So even within this method, you can call other methods. And so this j object has everything that the s object had and then some. So again, I just want you to get these words, classes, we have attributes and methods. Attribute is data, method is a function that’s part of a class. An object or an instance is what we construct from the template. So, class is the cookie cutter and object is the cookie. And a constructor is a bit of code that runs at the moment of object creation, and inheritance is the ability to define a new class and add the existing capabilities of an existing class to make that class. And so that’s kind of the definitions. It’s a, you look at it and you say, why did they invent this? And that’s probably the right logic to have right now, because you haven’t written programs that are complex enough to require object orientation in terms of the code that you write. But we’ve actually been using object orientation all along because it’s a way to group functionality. Something like something as complex as beautiful soup or our SQLite connections. Those things are actually complex data and code and we don’t have to worry about that, and so the benefit to us right now is it simplifies our programs to make use of objects that others create. Some time from now, when you become a more sophisticated programmer, you’re like I’ve got to solve a problem and I’m going to start creating a bunch of objects to solve those problems.
مشارکت کنندگان در این صفحه
تا کنون فردی در بازسازی این صفحه مشارکت نداشته است.
🖊 شما نیز میتوانید برای مشارکت در ترجمهی این صفحه یا اصلاح متن انگلیسی، به این لینک مراجعه بفرمایید.