The general form of the day14--class, the Python constructor, and the inheritance of the class

Source: Internet
Author: User

The general form of a class creates a class we typically use the class keyword to create a class, followed by a class name, can be customized, and finally end with a colon, as follows: Class ClassName: "Class of Description '" Class content can write global variables of class, method    Here's an example: Class Ren (object): ' This class is Abort Ren class ' #类的说明一定要三个引号引起来 name = ' Meizi ' sex = ' F ' def hello (selfself): print (' Hello world! ') a = Ren () print (type (a)) print (a.name) print (A.sex) A.hello () prints the result to

Explanation: 1.object default is the parent class of all classes, do not write the default inheritance object 2.a = Ren (), is to instantiate the class Ren 3. The type of print a above is a Ren Class 4. Calls the methods and variables of the class, directly after the class is directly instantiated with "." Call can be 5. If you want to add a variable or assignment to instance a, you can use "." Directly. Add variable assignment. The constructor of the __init__ class is called when the object is generated, because the class can act as a template, so you can force some of the attributes that we think must be bound to be filled in when the instance is created.        By defining a special __init__ method, when creating an instance, bind the attributes such as Name,score: Class Ren (): def __init__ (self,name,sex): Self.name = Name self.sex = Sex def hello (self): print (' Hello {0} '. Format (self.name)) test = ren (' yangling ', ' M ') Test.hello ( The results are printed as:

Explain:    1. When passing parameters, it must be passed two parameters, name and score, otherwise the error     2.self parameters will not be passed, Python Auto Club Student instantiation of S is passed to the first parameter, that is, the inheritance      inheritance of the self  class, as the name implies, it is known as its meaning, for example, you now have an existing class A, now you need to write a class B, But Class B is a special version of Class A, the Class A is called the parent class, the class B is called a subclass, and the subclass inherits all the properties and methods of the parent class, and can customize its own properties and methods, which greatly increases the reusability of the code.     In our first section of this chapter, we can inherit the object class, which is the parent class of all classes, all of which inherit the object class by default, which is a super class and, if not written, inherits object by default.     Inheritance class Format: Class A (parent):    ...  python class supports multiple inheritance, while Java does not have many inheritance, but can have multi-interface implementation, Python's multiple inheritance is very simple, Let's take a look at the multi-inheritance format: Class A (object):      # defining classes a    Passclass B (object):     #定义类B & nbsp   Pass class C (A, B):           #继承类A和B     pass      Multiple inheritance in fact, in the need of the parent class position, directly write more than one parent class can be, and then use "," separate can be, C class at the same time inherit the Class A and Class B. Inheritance considerations for     Python classes:    1. The construction of a class in inheritance (The __init () method) is not automatically called, and it needs to be called in person in the construction of the subclass.     2.Python always looks in the subclass first, and if it is not found, it will look in the parent class.     Example 1: class A (object): # define Class a    Passclass B (object): #定义类B &NBSp   Pass class C (A, B): #继承类A和B     Pass class parent ():    name = ' parent '     SE x = ' F ' def __init__ (self):    print ("My name was {0}". Format (self.name)) def get_name (self):    return s Elf.namedef get_sex (self):    return Self.sex class child (parent):    name = ' Child '   & nbsp Sex = "N"     def __init__ (self):        print (' My name is {0} '. Format (self.name))   &nbs P def hello (self):        print ("Hello world")  a = Child () A.hello () print (A.get_name ()) Print ( A.get_sex ())   printing results for: 

The general form of the day14--class, the Python constructor, and the inheritance of the class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.