Day20 object-oriented foundation

1. Object-oriented concepts

What is object-oriented

  Object-oriented referred to OOP is a programming idea is summed up the experience of predecessors, how to guide programmers to write better programs

  Its core is the object, the object as a basic operation unit program, the program is a set of objects, the programmer is responsible for scheduling these objects interact to accomplish the task

In object-oriented programmers in the angle changed from the operator into a specific commander

Object-oriented advantages and disadvantages of the use of scenarios

  Advantages: improved scalability. When an object is changed, other objects is of no effect

     Increased flexibility. For example the game, each player is free operation, rather than machine-like fixed! You can buy weapons, you can also buy Armor

     Improved reusability. A defined object can be repeatedly used

  Disadvantages: increased complexity of the program

     We can not accurately predict the results

  Be used: high scalability requirements program, usually the user-oriented, such as qq, micro-channel, and some of the frequently updated game

Process-oriented advantages and disadvantages and usage scenarios

  Advantages: logical, complex problems can be streamlined, and thus of short answer

  Disadvantages: poor scalability, maintainability, once the need to modify a function, it will affect the overall function

  Be used: low scalability requirements of the procedure, such as the kernel, Git, calculators, etc. Once there is no need to modify the late

Note: The object-oriented and process-oriented and not good or bad, depending on the particular use to distinguish which case

 

2. The concept of classes and objects

And line type of object-oriented programming (OOP) in the core of the two concepts

class

  Type, category, it is a set of features and acts with the same object

Objects

  Is a thing of concrete existence, with words and behavior characteristics, the object is a combination of characteristics and skills

Class contains a series of objects, the object belongs to a class

In life there is a first, and then the object class, while in the program is the first class and then have the object

Conclusion: In object-oriented programming will need to consider the characteristics and behavior of objects we need to create, and then create a class of objects that belong to these characteristics and behaviors

 

3. How to create classes and objects

The syntax for defining classes and objects

1  # syntax definition of classes and objects 
2  class class name:   # Create a class 
3      internal code can define his features and skills (behavior)
 4      wherein variable name represents a
 5      skills (acts) represented by the function
 . 6  
. 7 concrete objects name = class name ()   # create a specific object

Simply create classes and objects

1  # simply create an object and a class 
2  class the Person:   # This is a class, although this class nothing 
. 3      Print (123 )
 . 4      Role = ' Person '   # of people who are characteristic properties 
. 5      DEF EAT (Self) :   # people have to eat, eat have a skill 
6          return  ' eAT ' 
7  
8 p1 = the person ()   # this is a subject that he is created out according to the class, the class created when the code will execute 
9  Print ( p1.eat ())   # print type return value 'eat' function 
10  Print (p1.role)   # View class attributes

 

4. Find the sequence design attributes and properties

Property design

1  # Design Attribute 
2  class the Person:   # define a class 
. 3      Age = 18 is   # attribute class 
. 4      Pass 
. 5  
. 6 P1 = the Person ()   # generates an object 
. 7 p1.name = ' sxc '   # increase the properties of the object 
. 8  Print (P1 .age)
 . 9  Print (p1.name)
 10  
. 11 p1.name = ' ZZJ '   # modify attribute 
12 is  Print (p1.name)
 13 is  
14  del p1.name   # delete attribute
15  
16  Print (p1. __Dict__ )   # View all the properties of an object, name of the attribute is removed 
17  
18  Print (p1. __Class__ )   # class information access objects

 

Property search order

Start looking object's own property, you can not find a class to go to find. Objects ------> ------ class> parent

 1 # 属性的查找顺序
 2 class Person:  # 定义一个类
 3     age = 18  # 类的属性
 4     pass
 5 
 6 p1 = Person()  # 生成一个对象
 7 p1.age = 20  # 对象中的属性
 8 print(p1.age)  # 此时打印的是对象的属性
 9 
10 del p1.age  #删除对象中的属性
11 print(p1.age)  # 此时打印的就是类的属性了

 

5.初始化方法

什么是初始化方法

  用于为对象的属性设置初始值的函数,__init__方法

为什么需要初始化方法

  在类的实例(对象)中,一些属性是必须存在的,就可以用初始化函数来完成,如人的姓名

特点:当实例化对象时,会自动执行__init__方法,会自动将对象作为第一个参数传入。

初始化对象的属性

 1 # 初始化方法
 2 class Student:
 3     def __init__(self,name,age,sex):  # 初始化方法
 4         self.name = name
 5         self.age = age
 6         self.sex = sex
 7 
 8 s1 = Student('sxc', 18, 'male')
 9 s2 = Student('zzj', 19, 'male')
10 print(s1.name)  # 查看属性
11 print(s2.age)

注意:初始化函数不能有返回值,他只能返回None,不写return和只写return和return None都可以,但是一般默认不写

 

6.绑定方法之对象的绑定方法

在累中定义的函数方法默认都对象绑定方法,并且没有被任何装饰器装饰

特点:当使用对象调用该函数时会自动传入对象本身作为第一个参数

   当使用类名来调用时他就是一个普通的函数,有几个参数就要传入几个参数

 1 # 对象绑定方法
 2 class Student:
 3     def __init__(self,name,age,sex):  # 初始化方法
 4         self.name = name
 5         self.age = age
 6         self.sex = sex
 7 
 8     def say(self):
 9         print('我是%s,我的年龄是%s,我的性别是%s'%(self.name,self.age,self.sex))
10 
11 s1 = Student('sxc',18,'')
12 s1.say()  # 对象直接调用会将本身作为参数
13 
14 Student.say(s1)  # 类名来调用需要传入参数

 

7.绑定方法之类的绑定方法

用classmethod装饰器装饰的方法就是类绑定方法

特点:不管用类还是对象调用,参数的第一个必须是cls表示当前类本身,使用类名来调用,调用时会自动传入类

 1 # 类绑定方法
 2 class Student:
 3     def __init__(self,name,age,sex):  # 初始化方法
 4         self.name = name
 5         self.age = age
 6         self.sex = sex
 7 
 8     @classmethod
 9     def say(cls,self):
10         print('我的类是%s,我是%s,我的年龄是%s,我的性别是%s'%(cls,self.name,self.age,self.sex))
11 
12 s1 = Student('sxc',18,'')
13 s1.say(s1)  # 自动传入类
14 
15 Student.say(s1)  # 自动传入类

 

8.非绑定方法(不常用)

用staticmethod装饰器装饰的方法就叫做非绑定方法,不需访问类的数据,也不需要访问对象的数据 

 1 # 非绑定方法
 2 class Student:
 3     def __init__(self,name):  # 初始化方法
 4         self.name = name
 5 
 6     @staticmethod  # 非绑定方法
 7     def msg():
 8         print('i am a student')
 9 
10 s1 = Student('sxc')  # 定义一个对象
11 
12 Student.msg()  # 类可以直接调用,不需要传参数
13 
14 s1.msg()  # 对象可以直接调用,不需要传参数

 

Guess you like

Origin www.cnblogs.com/sxchen/p/11241339.html