Object-oriented review ----

In this section:

1 Overview

2, create classes and objects

3, the three characteristics of object-oriented

  3.1 package

  3.2 Inheritance

  Over 3.3-state

 

 

1 Overview

Programming Paradigm

Programming is the code a programmer with a specific grammatical structure + algorithm + data consisting of process to tell the computer how to perform a task, a program is a set of instructions that programmers in order to get a written task results, the saying goes, all roads lead to Rome, a way to achieve a task many different ways, the characteristics of these different ways of programming are summarized get out of programming categories, namely programming paradigm. Represent different ideas to solve problems of all kinds of jobs to take on a different nature programming paradigm, most languages only support a programming paradigm, of course, some languages can simultaneously support multiple programming paradigms. The two most important programming paradigms are process-oriented programming and object-oriented programming, and python also supports functional programming.

 

  • Process for: write barrier based on business logic codes from top to bottom
  • Functional: The function code is encapsulated into a function, not need to repeat the preparation of the future, the function can be called only
  • Object-oriented: the classification and packaging of the function, allowing developers to "faster and better and stronger ..."

 

Object-oriented programming ( Object Oriented Programming, of OOP )

OOP programming is the use of "class" and "object" to create a variety of models to achieve a description of the real world, the use of object-oriented programming For one, because it can make the maintenance and expansion of the program easier, and greatly improve program development efficiency, in addition, based on object-oriented programming can make it easier for people to understand your code logic, enabling the team to develop to become more calm.

  Some object-oriented core characteristics are as follows:

  Class class : i.e., a class is an abstract class of an object have the same attribute, the blueprint, the prototype. Defines the properties (variables (data)), the common method of these objects are included in the class.

  Object object : An object that is an instance after instance of a class, a class must be called in the program only after instantiated, a class can instantiate multiple objects, each also can have different attributes, just like humans It means the owner, everyone refers to a specific target, before people have in common, there are also different.

  Encapsulation package : in a class assignment for data internal to external calls transparent to the user, which makes the class into a container or capsule, which contains the data and methods of a class.

  Inheritance Inheritance: A class can be derived subclasses, attributes defined in the parent class, method automatically inherited by subclasses.

  Polymorphism Polymorphism: polymorphism is an important characteristic of the object-oriented, simple point that: "an interface, a variety of implementation," refers to a group derived class different subclasses, and each subclass inherits the same method name at the same time for the parent class to do a different realization, this is the show of the same thing a variety of forms. The program is actually a concrete world abstracting process, polymorphism is a reflection of abstraction, the common number of specific things abstracted, then, dialogue with different specific things through this abstract thing.

 

 

2, create classes and objects

Object-oriented programming is a programming way, landing this programmatically requires the use of "class" and "object" to achieve, so the object-oriented programming in fact, the use of "class" and "object".

  •  Class is a template, the template can contain multiple functions, function in the realization of some of the features.
  •  Object is an example of a template created, you can execute the letter through the class instance object

 

 

1  # Create a class 
2  class Bar:
 . 3  
. 4      # Create a class function 
. 5      DEF foo (Self, Arg):
 . 6          Print (Self, Arg)
 . 7  
. 8  # create objects z1 The class Bar 
. 9 z1 = Bar ()
 10  Print ( Z1)        # outputs the target address 
. 11 (111) z1.foo      # performing the method Bar
  • class keyword, class representation.
  • Create an object, the class name in brackets can be.
  • Function defined in the class called "methods."

 

Applicability functional programming and object-oriented programming

In Python functional programming and object-oriented programming Both can be used, unlike Java and c. However, the results of all can use some of it will lead to tangle, when to use it more appropriate.

See the next plane, using programming functions and time to perform a "method" object-oriented programming object-oriented manner than the simple function.

  • Object-oriented: Object [create] [implementation] through the object
  • Functional programming: [Executive function]

The answer is to observe the above comparison is yes, then is not absolute, its different ways of programming for different scenarios.

 

Suitable functional application scenario is: independently without sharing data between the various functions.

Is applicable to a plurality of object-oriented function parameters have some of the same cases . as follows:

. 1  class DatabaseHelper:
 2      
. 3          DEF  the __init__ (Self, IP, Port, username, pwd):
 . 4              self.ip = IP
 . 5              self.port = Port
 . 6              self.username = username
 . 7              self.pwd = pwd
 . 8          
. 9          DEF the Add (Self , Content):
 10              # using self encapsulated user name, password and other data links 
. 11              Print ( ' Content ' )
 12 is              # close the data link 
13 is          
14          DEF Delete (self, Content):
15              # using self encapsulated user name and password link data 
16              Print ( ' Content ' )
 . 17              # close the data link 
18 is          
. 19          DEF Update (self, Content):
 20 is              # using self encapsulated user name and password link data 
21 is              Print ( ' Content ' )
 22 is              # close the data link 
23 is              
24          DEF GET (self, Content):
 25              # using self encapsulated user name, password and other data link 
26 is              Print ( ' Content ' )
 27              # close the data link 
28 
29 S1 = DatabaseHelper ( ' 1.1.1.1 ' , 3306, ' John Doe ' , ' SB ' )

 

 

 

3, the three characteristics of object-oriented

Object-oriented characteristics of the three means: encapsulation, inheritance, and polymorphism.

 

3.1 package

Package, by definition is the contents of the package somewhere, go after the call content is encapsulated in somewhere.

Therefore, when using the object-oriented characteristics of the package, it is necessary:

  • Encapsulation of the content to somewhere
  • Call the package contents from somewhere

 

Encapsulation of the content to somewhere

 

It is a form of self parameter when executed Bar = zhongjianren ( 'intermediary' , 33) when, self equal zhongjianren 

So, in fact, the contents are encapsulated into objects in zhongjianren, if a plurality of objects, each object has a name and age properties, similar to the figure in the memory to store.

 

 Call content is encapsulated from somewhere

When you call content is packaged, there are two cases:

  • Direct calls through the object
  • Indirect call through self

 

 

 In summary, for the object-oriented packages, in fact, it is to use a constructor encapsulation of the content object, and then packaged contents obtained indirectly or directly self through the object.

 

3.2 Inheritance

Inheritance, object-oriented inheritance and real life inherit the same, namely: the child can inherit the contents of the Father.

Inheritance refers to such a capability: it can use all the features of an existing class, and without having to rewrite these functions extend the case of the original class.

Create a new class by inheriting called "sub-class" or "derived class."

Inherited classes are called "base class", "parent" or "superclass."

In certain OOP languages, subclasses inherit a plurality of base classes. But under normal circumstances, a subclass can have only one base class to implement multiple inheritance can be achieved through multiple inheritance.

 

继承的语法

 所以,对于面向对象的继承来说,其实就是将多个类共有的方法提取到父类中,子类仅需继承父类而不必一一实现每个方法

 

多继承

  • Python的类可以继承多个类,Java和C#中则只能继承一个类。
  • Python的类如果继承了多个类,那么其寻找方法的方式有两种,分别是:深度优先 广度优先。

 

 

在Pyhon2中,经典类是按深度优先来继承的,新式类是按广度优先来继承的。

在Python3中,经典类和新式类都是按广度优先来继承的。

经典类和新式类,从字面上可以看出一个老一个新,新的必然包含了跟多的功能,也是之后推荐的写法,从写法上区分的话,如果当前类或者父类继承了object,那么该类便是新式类,否则便是经典类。

而在Python3中,继承的父类会默认继承obje类。

 

多继承的顺序

 1 class D(object):
 2 
 3     def bar(self):
 4         print 'D.bar'
 5 
 6 
 7 class C(D):
 8 
 9     def bar(self):
10         print 'C.bar'
11 
12 
13 class B(D):
14 
15     def bar(self):
16         print 'B.bar'
17 
18 
19 class A(B, C):
20 
21     def bar(self):
22         print 'A.bar'
23 
24 a = A()
25 # 执行bar方法时
26 # 首先去A类中查找,如果A类中没有,则继续去B类中找,如果B类中么有,则继续去C类中找,如果C类中么有,则继续去D类中找,如果还是未找到,则报错
27 # 所以,查找顺序:A --> B --> C --> D
28 # 在上述查找bar方法的过程中,一旦找到,则寻找过程立即中断,便不会再继续找了
29 a.bar()

 

继承中一些注意事项:

1、当继承父类后,不想执行父类的方法时,可以进行重写。同方法名,优先执行子类方法。

2、无论在哪里调用对象,self永远是执行该方法的调用者。

3、当子类与父类有共同方法,想调用父类方法时,可使用super方法或调用父类名如:

  super(子类,self).父类中的方法()

  父类名.父类中的方法(self,···)  ——>py2中常用,但这种方法不推荐使用,因为一旦父类发生变化,方法调用位置的 类名 同样需要修改

 

 

3.3多态

多态与多态性

多态:多态指的是一类事物有多种形态(一个抽象类有多个子类,因而多态的概念依赖于继承)

多态性:多态性是指具有不同功能的函数可以使用相同的函数名,这样就可以用一个函数名调用不同内容的函数。

多态性使用的前提:①类的继承关系 ②要有方法重写。

 

语法

 1 class Animal:  # 同一类事物:动物
 2     def run(self):
 3         pass
 4 
 5 class Cat(Animal):  # 动物的形态之一:猫
 6     def run(self):
 7         print('cat runs')
 8 
 9 class Dog(Animal):  # 动物的形态之二:狗
10     def run(self):
11         print('dog runs')
12 
13 class Pig(Animal):  # 动物的形态之三:猪
14     def run(self):
15         print('pig runs')
16 
17 
18 #多态性:定义统一的接口
19 def func(obj):      #obj这个参数没有类型限制,可以传入不同类型的值
20     obj.run()       #调用的逻辑都一样,执行的结果却不一样
21 
22 func(Cat)
23 func(Dog)
24 func(Pig)

 

在python中多态的使用不如Java与C#中那么明显,所以python中刻意谈到多态的意义不是特别大。

 

Guess you like

Origin www.cnblogs.com/Yan-night/p/11451788.html