Object-oriented basic parts and modules

Source: Internet
Author: User

First, the object-oriented basis

Object-oriented noun interpretation:

    • 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. Object-oriented features: encapsulation

The package is best understood. Encapsulation is one of the object-oriented features and is the main feature of object and class concepts.

Encapsulation, which is the encapsulation of objective things into abstract classes, and classes can put their own data and methods only trusted class or object operation, to the untrusted information hiding.

First step: encapsulate the content somewhere

Class1 SQLHELPer:    def __init__ (self,a1,a2,a3): #成为构造方法, automatically executes objects created from classes        self.hhost = A1        self.uusername = A2        self.pwd = A3    def fetch (self):        print (self.hhost)        print (self.uusername)        print (SELF.PWD)    DEF create (Self,sql):        pass    def remove (Self,nid):        pass    def modify (self,name):        pass# Create an object based on class SQLHELPer # automatic execution of SQLHELPer __init__ method obj = SQLHELPer () obj.hhost = "c1.salt.com" obj.uusername = "Jason" obj.pwd = " 123 "
Obj1 = SQLHELPer (' abc.com ', ' Jack ', ' 234 ')

Self is a formal parameter, and when you execute obj = SQLHELPer (), self equals obj

When executing obj1 = SQLHELPer (' abc.com ', ' Jack ', ' 234 '), self equals obj1

So, the content is actually encapsulated in the object obj and Obj1, each object has A1,A2 and A3 attributes, in memory similar to to save.

Step two: Call the encapsulated content from somewhere

When the encapsulated content is called, there are two scenarios:

    • Called directly through an object
    • Indirectly called through self

1. Direct invocation of encapsulated content by object

Shows how objects obj1 and Obj2 are saved in memory, so that the encapsulated content can be called according to the Save format: Object. Property name

Class Foo:     def __init__ (self, Name, age):        self.name = name        Self.age = Age obj1 = Foo (' Wupeiqi ', +) print obj1. Name    # Direct call to the Name property of the Obj1 object print obj1.age     # directly calls the Obj1 object's Age Property Obj2 = Foo (' Alex ',) print Obj2.name    # Call the Name property of the Obj2 object directly print Obj2.age     # Call the Age property of the Obj2 object directly

2. Indirectly invoking the encapsulated content via self

When executing a method in a class, the encapsulated content needs to be indirectly called through self

Class Foo:      def __init__ (self, Name, age):        self.name = name        Self.age = Age      def detail (self):        print Self.name        print self.age  obj1 = Foo (' Wupeiqi ', +) Obj1.detail ()  # Python will pass obj1 to the self parameter by default, which is: Obj1.detail ( OBJ1), so, at this point, the internal self = obj1, namely: Self.name is Wupeiqi; Self.age is obj2  = Foo (' Alex ',) Obj2.detail ()  # python defaults to Obj2 is passed to the self parameter, namely: Obj1.detail (OBJ2), so the self = Obj2 inside the method, that is: Self.name is Alex; Self.age is 78

In summary, for object-oriented encapsulation, it is actually using the construction method to encapsulate the content into the object, and then indirectly through the object directly or self to obtain the encapsulated content.

Inherited

One of the main functions of object-oriented programming (OOP) language is "inheritance". Inheritance refers to the ability to use all the functionality of an existing class and to extend these capabilities without rewriting the original class.

New classes created through inheritance are called "subclasses" or "derived classes."

The inherited class is called the base class, the parent class, or the superclass.

The process of inheritance is from the general to the special process.

To implement inheritance, it can be implemented through inheritance (inheritance) and combination (composition).

In some OOP languages, a subclass can inherit multiple base classes. However, in general, a subclass can have only one base class, and to implement multiple inheritance, it can be implemented by multilevel inheritance.

Inheritance concepts are implemented in three categories: implementation inheritance, interface inheritance, and visual inheritance.

Implementing inheritance is the ability to use the properties and methods of a base class without additional coding; O interface inheritance is the ability to use only the names of properties and methods, but the subclasses must provide the implementation; Visual inheritance is the ability of a subform (class) to use the appearance of a base form (class) and to implement code.

When considering using inheritance, it is important to note that the relationship between the two classes should be a "belongs" relationship. For example, the Employee is a person and the Manager is a person, so these two classes can inherit the People class. But the Leg class cannot inherit the person class, because the leg is not a human.

Abstract classes define only the generic properties and methods that will be created by the subclass.

OO development paradigm is roughly: dividing objects → abstract classes → organizing classes into hierarchical structures (inheritance and compositing) → Designing and implementing several stages with classes and instances.

Class Role (object):    def __init__ (self, name):        self.name = name    def get_name (self):        return Self.nameclass Teacher (Role):    def __init__ (self, Name, course):        "If the        parent class already has a method, and the subclass has a method of the same name, the method of the parent class will be overwritten , the jargon 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 name        Self.course = Course # This variable is the Def say (self) that the parent class does not have    : # defines the parent class's        print (' My name is%s, I am a Chinese teather '%self.na Me) if __name__ = = ' __main__ ':    lisi = Teacher (' Lisi ', ' 中文版 ') # define Teacher instance    print (lisi.name) # Name this variable is a subclass obtained by inheriting the way    print (Lisi.get_name ()) # inherits from the method of the parent class    Lisi.say () # Subclass-specific methods

therefore, for object-oriented inheritance, it is actually the method of extracting multiple classes common to the parent class, and the subclass inherits only the parent class without having to implement each method in one.

Note: In addition to the names of subclasses and parent classes, you may have seen derived and base classes that are only different from subclasses and parent classes.

So the question comes again, how to inherit?

    • Whether multiple classes can be inherited
    • If you have inherited multiple classes that have the same function in each class, then that one will be used?

1. Python classes can inherit multiple classes, and Java and C # can inherit only one class

2. If the Python class inherits more than one class, there are two ways to find the method: Depth first and breadth First

Class D:    def bar (self):        print (' D.bar ') class C ():    def Bar (self):        print (' C.bar ') class B (D):    def Bar2 (self):        print (' B.bar ') class A (B,c):    def bar1 (self):        print (' A.bar ') a = A ()A.bar () # #D. Bar
# when executing the Bar method # First go to the class A to find, if not in Class A, then continue to find in class B, if there is a class B, then continue to find in Class D, if there is a class D, then continue to find in Class C, if still not found, then error # So, look in order: A--and B > C such as # in the process of finding the bar method above, once found, the search process immediately interrupted, will not continue to find A.bar ()

Depth First

Breadth First

Class D (object):    def Bar (self):        print (' D.bar ') class C (d):    def Bar (self):        print (' C.bar ') class B (d):    def bar2 (self):        print (' B.bar ') class A (B,c):    def bar1 (self):        print (' A.bar ') a = A ()
# when executing the Bar method # First go to the class A to find, if not in Class A, then continue to find in class B, if there is a class B, then continue to find in Class C, if there is a Class C, then continue to find in Class D, if still not found, then error # So, look in order: A--and B > D such as # in the process of finding the bar method above, once found, the search process is immediately interrupted and no further search
A.bar () # #C. Bar

Classic class and new class, literally can see an old a new, new inevitably contain with many functions, is also recommended after the wording, from the wording of the words, if the current class or the parent class inherits the object class , then the class is a new class, otherwise it is the classic class.

Classic class: First go to a class to find, if not in Class A, then continue to the class B to find, if there is a class B, then continue to find in class D , if there is a class D, then continue to find in class C , If it is still not found, the error

New class: First go to class a to find, if not in Class A, then continue to the class B to find, if there is a class B, then continue to the class C , if there is a Class C, then continue to find in class D , if still not found, The error

Note: In the above search process, once found, the search process immediately interrupted, and will not continue to find

* * The default is breadth first in Python3

Polymorphism Polymorphism (POLYMORPHISN) is a technique that allows you to set a 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 that is 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. So what is the role of polymorphism? We know that encapsulation can hide implementation details and make code modular; Inheritance can extend existing code modules (classes); they are all designed to-code reuse. And polymorphism is for another purpose--interface reuse! The role of polymorphism is to ensure that classes are called correctly when inheriting and deriving a property of an instance of any class in the family tree.  Definition of three categories (detailed description)
Class Foo (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, a class can inherit from the description document of more than one class ' class, and Python automatically assigns the contents of this polygon to the variable of the class __doc__ The CLASS_VAR1 = ' var1 ' # class variable, which belongs to the class itself and not to the object of the class, can be called by the name of the class name. The variable name can also be called, provided that there is no instance variable with the same name Def __init__ (self, ARG,        ARG2): "Initialization method, a bit similar to the Java language construction method, in the creation of the object of the class automatically call:p Aram var2: Parameters, initialization method can also: return:" '     SELF.VAR2 = arg # The variable of the object, which belongs to the object that is not part of the class, can only be called by the object. The variable name means that the SELF.__VAR3 = arg # object can only be called internally and cannot be called by the object. Variable name and cannot be inherited def func1 (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) 2, self represents the class object itself, when we pass the object name. Method () to invoke, the interpreter automatically passes the object as the first argument to the method, the method name. Method (object name): Return: ' # method body, method body can invoke the object's variables and methods through self. keyword.        __var3 = arg # method can call private variables and methods Def __FUNC2 (self): ' Private method, as with private variables, cannot be inherited and externally called: return: ' '        Pass Class Foo2 (Foo): Def __init__ (self, ARG, arg2): ' 1, this invokes the initialization method of the parent class, paying particular attention to the private method represented by the ' __ variable name '. 2, although the construction method of the parent class is called, __vaThe R3 variable is a private method of the parent class, and the subclass does not have the variable until it is redefined, paying particular attention to:p Aram Arg::p Aram Arg2:: Return: "Super (F Oo2, self). __init__ (ARG, arg2) Self.__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

# #参考文档

1, King Kok Wang Blog: http://www.cnblogs.com/alex3714/articles/5188179.html

2, Silver corner King Blog: http://www.cnblogs.com/wupeiqi/articles/5017742.html

3, 65 elder brother: http://www.cnblogs.com/zhangxiaxuan/p/5292691.html

4. eva_l:http://www.cnblogs.com/eva-j/p/5009377.html

Object-oriented basic parts and modules

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.