7.25 Object-Oriented

Object-Oriented

1. What is the object orientation is?

  It is a programming idea, in core subject

  The program seen as a bunch collection of objects, program function is to control scheduling objects interact complete

2. Why use an object-oriented

  Advantages: high scalability and good maintainability, flexibility, high reusability

  Cons: program structure more complex, we can not predict the results

3. Classes and Objects

  Class: a series with the same characteristics and behavior of objects aggregate, is an abstract concept

  Object: a collection have certain characteristics and behavior, it is a thing of the presence of specific

  Class contains a plurality of objects, objects of a certain type

4. Define

  Written: class class name:

        Content class

  The class name big hump

  Create an object: class name ()

The proper use of attributes

  Public class objects into portions

  Each object in the object's own unique namespace

6. initialization function

  Assign initial values ​​for objects, and some other initialization logic

7. binding method

  (1) objects and functions, object binding method

    When an object is automatically invoked incoming object itself as the first argument

    If a class name called, it is a normal function needs its own traditional values

  (2) class and function, class binding method: @classmethod

    With a class or object call incoming class itself

8. The unbound methods: @staticmethod

  Is an ordinary function, does not automatically pass the value

9. Scene

  When the logical functions required to access the object, it is bound to the object, the class data is only necessary to bind to the class

  Find the order of attributes, first find the object's own namespace, class name can not be found in looking for space, looking like he could not find parent

inherit

1. What is inherited

  Inheritance is a relationship between two objects is described, what is what relationship, such as McDull, Paige, just pigs are pigs mane

  In the procedure, described in inheritance relationship between classes and, for example, a succession of b, a b can be used directly in existing methods and properties, called a subclass, b is called the parent class, and called a base class 

2. Why use inheritance

  Inherited party may direct one party has inherited some things

  Its purpose is to reuse existing code has been improved reusability

3. How to use inheritance

  The basic syntax:

class Base: 
    desc = " This is a base class " 
    DEF show_info (Self):
         Print (self.desc) 

    DEF make_money (Self):
         Print ( " one day earn a one hundred million ... ' ) 

# specify the parent class-bit Base 
class SubClass (Base):
     Pass 

obj = SubClass ()
 # even if nothing class can use existing content in the parent class 
obj.make_money ()
 Print (obj.desc)
View Code

  In python a plurality of subclasses inherit the parent class at the same time

4. Abstract

  Features: not specific, unclear, vague, not read

  The same portion of the plurality of process subclasses, extraction, to form a new class, a process also known as abstract

5. How to correctly use inheritance

  1. The first abstract inheritance

  2. inherited an already existing class, extend or modify the original function

Property search order

  The object itself -> where the class -> find parent -> parent class parent -> Object

Derivation

  When a subclass occurred with different content parent class, subclass it is called a derived class

  Usually sub-class will write some new code, and the parent can not be exactly the same, both are usually derived class

  So derived class refers to a subclass

cover

  Also known as Rewrite: overrides

  When a subclass appeared entirely consistent with the parent class property or method name, it overrides the parent class, making it impossible to find a property or method of the parent class

class Person:
    def say_hi(self):
        print("hello")

class Student(Person):
    def say_hi(self):
        print("hello world!")

stu = Student()
stu.say_hi()  # hello world!

How to achieve a type of element can limit the list of classes

class of MyList (List):
     DEF  the __init__ (Self, ELEMENT_TYPE): 
        . Super () the __init__ () # initialization method is called the parent class to complete the basic initialization 
        self.element_type = ELEMENT_TYPE 

    DEF the append (Self, Object):
         "" " 
        : param object: to store the elements 
        : return: no 
        "" " 
        IF of the type (Object) == self.element_type:
             # we need here append function to access the parent class to accomplish the actual storage operation 
            . super () append (object )
         the else :
             Print ( " Sorry SIR, IS not you Element of the type% S " %self.element_type) 

# specify the element type to be stored when creating 
m = MyList (int)
 # when you have demand, is the need to create the object what to do with the thing would expect initialization method 
m.append (1 )
 Print ( m [0]) 
m.append ( " string " )

Subclass the parent class content access

1. Three Methods

  Mode 1: property or method super (current class name, self) you want to adjust the parent class.

class Parent:
    text = "abc"
    def say_something(self):
        print("anything")

class Sub(Parent):
    def show_info(self):
        print(super(Sub,self).text)
        super(Sub,self).say_something()

sub = Sub()
sub.show_info()

  Mode 2:. Super () method of the parent class or attribute you want to transfer, the new syntax py3, the most common way

class Parent:
    text = "abc"
    def say_something(self):
        print("anything")

class Sub(Parent):
    def show_info(self):
        print(super().text)
        super().say_something()

sub = Sub()
sub.show_info()

  Mode 3: class name of the parent class property or method you want to tune (self), Mode 3 has nothing to do with inheritance.

class Parent:
    text = "abc"
    def say_something(self):
        print("anything")

class Sub(Parent):
    def show_info(self):
        print(Parent.text)
        Parent.say_something(self)

sub = Sub()
sub.show_info()

2. Emphasizes:

  When you inherit an existing class, and you override the parent class init method, you must call the parent class initialization method of the first line of the initialization method, passing in the required parameters parent class

combination

  It is also a relationship, describing what is what is the relationship between two objects, for example, students have cell phones, game characters have certain equipment

  An object as a property of another object (both what what)

1. The purpose of the combined

  Also to reuse existing code

  When to use inheritance: analysis of the relationship between two classes, in the end is not what is the relationship between what

  When to use a combination of: if between the two classes, there is no significant relationship, do not belong to the same

  Further, compared hierarchical composition, the lower the degree of coupling

class Phone:
    def __init__(self,price,kind,color):
        self.price = price
        self.kind = kind
        self.color = color

    def call(self):
        print("正在呼叫XXXX;")

    def send_message(self):
        print("正在发送短信....")

class Student:
    def __init__(self,name,gender,phone):
        self.name = name
        self.gender = gender
        self.phone = phone

    def show_info(self):
        print("name:%s gender:%s" % (self.name,self.gender))

phone = Phone(1000,"apple","red")

stu1 = Student("rose","male",phone)
stu1.phone.call()
stu1.phone.send_message()
View Code

The new class of Classic

  Python3 in any class are inherited directly or indirectly Object

  The new class: any explicit or implicit object inherits from class to the new class is called, so that all the new class python3

  Classic: That is not the object of a subclass, only appear in the python2

Diamond inheritance

Because python support multiple inheritance, when there is a diamond inheritance when:

  If the new class, the first depth, when faced with a common parent class on breadth

  If Classic is a depth-first

Guess you like

Origin www.cnblogs.com/francis1/p/11247503.html