Python's Path "seventh": Python Basics (23)-Object-oriented beginner

Source: Internet
Author: User

First, the object-oriented basis
    • class: used to describe a collection of objects that have the same properties and methods. It defines the properties and methods that are common to each object in the collection. An object is an instance of a class.

    • Class variables: class variables are common throughout the instantiated object. Class variables are defined in the class and outside the body of the function. Class variables are not typically used as instance variables.

    • data members: class variables or instance variables are used to manipulate the data related to the class and its instance objects.

    • method Overrides: if the method inherited from the parent class does not meet the requirements of the subclass, it can be overridden, which is called the override of the method, also known as the override of the method.

    • Instance variable: A variable defined in a method that acts only on the class of the current instance.

    • inheritance: A derived class (derived class) that inherits the fields and methods of the base class. Inheritance also allows the object of a derived class to be treated as a base class object. For example, there is a design where an object of type dog is derived from the animal class, which is the analog "is a (is-a)" Relationship (example, dog is a animal).

    • instantiation: Creates an instance of a class, the concrete object of the class.

    • method: a function defined in a class. (Method belongs to class, not instance)

    • object: An instance of a data structure defined by a class. The object consists of two data members (class variables and instance variables) and methods.

Ii. definition of a class
1 classFoo (object):#Foo is the class name, and the representation in parentheses is which class the class inherits from, where object is the base class for all classes, and a class can inherit from more than one class2     " "3 class, Python automatically assigns this content to the class's variable __doc__4     " "5CLASS_VAR1 ='var1' #class, which belongs to the class itself, not to the object of the class, and can be called by the name of the class name. Variable name, as long as there is no instance variable of the same name6     def __init__(self, ARG, arg2):7         " "8 The initialization method, which is somewhat similar to the Java language construction method, is called automatically when the object of the class is created9 :p Aram var2: Parameters, initialization methods can also beTen : return: One         " " ASELF.VAR2 = arg#Object that belongs to an object that is not part of a class and can only be called by the object. Variable name -Self.__var3= arg#object can only be called internally and cannot be called through the object. Variable name and cannot be inherited -   the     deffunc1 (Self, arg): -         " " - Method - 1, the method belongs to the class (that is, at the time of instantiation does not like the object's variables to open up the memory space alone) + 2, self represents the object of the class itself, when we call through the object name. Method (), the interpreter automatically passes the object as the first argument to the method, the method name. Method (object name) - : return: +         " " A         #method Body, the method body can call the object's variables and methods through the self. Keyword atSelf.__var3= arg#method can call private variables and methods -   -     def __FUNC2(self): -         " " - private methods, like private variables, cannot be inherited and externally invoked - : return: in         " " -         Pass to   + classFoo2 (Foo): -     def __init__(self, ARG, arg2): the         " " * 1. The initialization method of the parent class is called, paying special attention to the private method represented by the "__ variable name" . $ 2, in spite of calling the construction method of the parent class, the __VAR3 variable is a private method of the parent class, and the subclass does not have the variable until it is redefined, paying special attention toPanax Notoginseng :p Aram Arg: - :p Aram Arg2: the : return: +         " " ASuper (Foo2, self).__init__(ARG, arg2) theSelf.__var3= Arg2#because __var3 is a private method of the parent class, although the initialization method of the parent class is called, the subclass still does not have it, so it still needs to be redefined
Three main features of object-oriented 1, package

encapsulation, which is to save the content to a place, easy to invoke and modify, such as a variable encapsulated into a class, so that the invocation of the class instantiation, through the object name. Variable name to invoke and modify the variable.

1 classRole (object):2     def __init__(self, name):3Self.name = Name#defining an instance variable4     defget_name (self):5         returnSelf.name6 if __name__=='__main__':7Zhangsan = Role ('Zhangsan')#Create an instance8     Print(Zhangsan.name)#the variable that invokes the instance name

The content is encapsulated in the Zhangsan variable, called directly by the Zhangsan instance of the call can be

2. Inheritance

Inheritance is the way a class can inherit another class, the methods and variables of another class, and so on. Is the same as the inheritance in reality.

classRole (object):def __init__(self, name): Self.name=namedefget_name (self):returnSelf.nameclassTeacher (Role):def __init__(self, Name, course):" "if the parent class already has a method, and the subclass has a method of the same name, it overrides the method of the parent class, which is called rewriting" "Super (Teacher, self).__init__(name)#through super This syntax can call the parent class's methods and variables, here call the constructor of the parent class, initialize the nameSelf.course = Course#This variable is not in the parent class.    defSay (self):#that defines the parent class.        Print('My name is%s, I am a 中文版 teather'%self.name)if __name__=='__main__': Lisi= Teacher ('Lisi','中文版')#defining an instance of teacher    Print(Lisi.name)#Name This variable is a subclass obtained by inheriting the method    Print(Lisi.get_name ())#methods that inherit from the parent classLisi.say ()#sub-class-specific methods
3. polymorphic

Polymorphism is for interface reuse, a technique that allows you to set the parent object to be equal to one or more of his child objects, and after assignment, the parent can operate differently depending on the attributes of the child object currently assigned to it. To put it simply, it is a sentence: A pointer to the parent class type is allowed to be assigned a pointer to the child class type.

Python does not support polymorphism, and it does not use polymorphic

Python's Path "seventh": Python Basics (23)-Object-oriented beginner

Related Article

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.