Write it in front.
Finally, it's Friday. When the little friends are no longer working overtime and rejoicing over the weekend, I just finished this week's game work, from the face of late to the tight frown to the present calm calm, as if only a few weeks of time. Sudden discovery: Change-it turns out to be so simple. Most of the time we want to fight for their youth once to lose to the occasional conflict and relaxed and happy life. We're only two words from the person who went to the end-hold on! So despite entering the class and object this part, everyone will have fear psychology, but hold on for a moment, you will be the person who came to the end!
Review
In the previous article, we initially learned about classes and objects, and ended up with some of their syntax, which is equivalent to getting the skeleton of an object. Today to add some of their relevant content, give our small skeleton to add muscle and skin, it is a living object, from now on you are a person with the object! Ha ~
First of all, we look at the object-oriented characteristics of the previous article, we have already known that the "Car" is a class, the car's "brand" and "price" is its property, can run on the road is his behavior; so we define a class car, The price and type two properties are defined in this car class, and there is a drivedistance method. We say that this car class is like a storage bag, and the "car" related to the scattered properties, methods are loaded into the package, which is object-oriented encapsulation .
Three main features of object-oriented
The main content we talked about in the previous article was in line with its encapsulation features. So that's the problem? is the class only encapsulated? Of course not, as a thing so difficult to understand, if only encapsulation is sorry we died so many brain cells! So, the bolt from the blue, object-oriented has three major features, namely: encapsulation, inheritance and polymorphism.
Good news and good news, good news one: encapsulation we have finished, so we have removed one of the three mountains, good news two: Because of the particularity of Python, the application of polymorphism is not extensive, so we actually have a half victory. Gossip Less, today we talk about inheritance , first remove one!
The subject--inheritance
College Reunion, table dinner, we are people, have to eat, drink these behaviors, but after graduation we all did a different job, some when the accountant, some did a programmer, now we get a description of these students this demand, we look very happy, we can achieve it, and then wrote down the following code:
We look at the code on the left side of the above, so the write really achieved our needs, but, wrote so many lines, but the real difference is only the yellow box content, fortunately, college students of the occupation are similar, if high school party can be lively. This time, we think, is it possible that we do not repeat the previous code, but also to achieve the same function? Of course! →_→ the right piece of code. It's a lot simpler to look at, and that's the inheritance of classes. Now you look like a little bit of rice paste, it's okay, here you just need to know there is a simple way to achieve, this is called inheritance. We'll talk about it in detail later.
Let's take a closer look at this picture above, explain what is called inheritance, first in the top yellow box, we define a class called classmate, which put the eat, drink two methods, the following we define two classes, Pythoner and accounting class, Inside each wrote a occupation method, print out the character's occupation. We see that the classmate class is no different from the class we saw before, but when Pythoner and accounting classes are defined, we see that the red box writes the class name of the classmate class, and we say that this implements the inheritance. The Pythoner and accounting classes integrate all the properties and methods of classmate.
After saying the definition of the inheriting class, let's take a look at the instantiation and invocation, we look at the right side of the small figure, we instantiate two objects, Eva and sweet, note that the red box we instantiate is Pythoner and accounting these two derived classes, But we can call classmate's eat and drink methods, and we can use the Name property of the parent class in occupation. magic! The code is posted and you're going to play it yourself.
1 classclassmate ():2 def __init__(self):3Self.name =name4 5 defEat (self):6 Print '%s is eating.'%Self.name7 8 defDrink (self):9 Print '%sis drinking'%Self.nameTen One A classPythoner (classmate): - - defoccupation (self): the Print '%s is a pythoner.'%Self.name - - classAccounting (classmate): - + defoccupation (self): - Print '%s is a accounting.'%Self.name + AEva = Pythoner ('Eva') at eva.occupation () - eva.eat () - Eva.drink () - -Sweet = Accounting ('Sweet') - sweet.occupation () in sweet.eat () -Sweet.drink ()
class Inheritance
Classes and objects of Python--advanced article