![]() And now we can take off the ones here and we can copy this and paste it here and also take off this guy. That's going to be title, price, period, and publisher. And remember, in the case of Periodical, it takes all of these so that it's going to be, let's see. And then in the init function for each, we call the super class. So Magazine will inherit from Periodical and so will Newspaper. Then Periodical, which inherits from Publication, and then we're going to change Newspaper and Magazine to inherit from Periodical. All right, so now we have a class hierarchy with Publication at the top, which Book inherits from. And then we'll have the Periodical class to find the period and the publisher. And we'll call the super class for title and price. And that will take self, title, price, period, and publisher. And of course, we'll have to create the init for Periodical as well. And Periodical is going to inherit from Publication. So I'm going to create another class named Periodical. So both of these classes have period and publisher attributes, so that's a pretty good hint that we can collect those in a super class also. Now, we could do the same thing with Newspaper and Magazine classes, but there's some duplication there too. So I only need to keep the author and pages. And Book no longer has to define title and price in its class because they're now defined in Publication. And then I'm going to call the init with title and price. So I'm going to create super and that's a function. Okay, and then we initialize the super class using the super function. To do that, inside these parentheses, I'm going to write Publication. So now we can fix the Book class and have it inherit from the Publication class. And so that means that, on this object, we have to set title equal to title, oops, and self.price equal to price. ![]() So we'll put init here and we'll have the Publication class contain the title and price. And then have that class define those common attributes. So let's make a class called Publication. So what we could do is define a class named Publication. So let's start with the most obvious duplication, which is the title and price attributes. We can improve the organization of these classes and make it easier to introduce new classes by implementing some inheritance and class hierarchy. So for example, all three classes have attributes for title and price, and the Newspaper and Magazine classes also have the same attributes for period and publisher. ![]() You can see that each one is its own class, but there's a considerable amount of duplication among the data that each class holds. And so at the moment, each of these classes is a standalone implementation, so let's hide the terminal. And you can see that, when we do this, I'm printing out the author of Book one, the publisher of Newspaper one, and then the price of each, okay. So before we do anything else, let's just go ahead and open up the terminal. And you can also see further down in the file, we have some code to create a few sample objects and then access some data on them. Magazines are the same, title, price, and they also have a Publication period and a publisher. A Newspaper also has a title and a price, but it has a publisher instead of a single author and it has a Publication period because they're published on a Periodical basis. So Books have a title and a price along with the author's name and the number of pages. ![]() And in this example, you can see I have three classes, there's a Book, Magazine, and Newspaper, and each of one of these classes represents a type of Publication, and each of them has a set of attributes that are relevant to that Publication type. So let's open up the inheritance_start file. So this makes it easy to centralize common functionality and data in one place instead of having it spread out and duplicated across multiple classes. Inheritance defines a way for a given class to inherit attributes and methods from one or more super classes. And in this example, we're going to see how that works in Python. One of the core concepts of object-oriented programming is the notion of inheritance. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |