First, object-oriented learning characteristics
A class is an abstraction, a blueprint for a class of objects that have the same properties. Prototype. The properties and common methods of these objects are defined in the class.
Process-oriented programming and object-oriented programming:
Process-oriented Programming: Use a series of instructions to tell the computer how to execute the basic design idea is that the program starts with solving a big problem and then breaks down a big problem into many small problems or sub-processes object-oriented programming: OOP programming is using "classes" and "objects" To create a variety of models to achieve the real world description of the world is the object of everything. As long as it is an object, it must belong to a certain species.
Object-Oriented Programming:
OOP programming is the use of "classes" and "objects" to create a variety of models to achieve the real world of the description of the world is the object of everything. As long as it is an object, it must belong to a certain species.
Objiect object:
An object is an instantiated instance of a class, a class can instantiate multiple objects, and each object can have different properties
Encapsulation Package :
Inheritance Inheritance :
A class can derive a subclass, a property defined within the parent class, a method that automatically inherits the quilt class
An object is an instantiated instance of a class, a class must be instantiated before it can be called in a program, a class can instantiate multiple objects, and each object can have different properties
Ii. Examples
1. Create a class
Class Role (object): #定义一个类 def __init__ (self): #初始化 Pass
The above __init__ () is called the initialization method (or constructor method), when the class is called, this method (although it is a function form, but in the class is not called the function, called the method) will automatically execute, do some initialization action.
2. Example
__author__ = ' LW ' class Role (object): Def __init__ (self,name,role,weapon,life_value=100,money=15000): ##定义一个类, class是定义类的语法,Role是类名,(object)是新式类的写法Self.name = name ##初始化函数,在生成一个角色时要初始化的一些属性就填写在这里Self.role = Role## # #self. Name = R1.name Self.weapon = Weapon Self.life_value = Life_value Self.money = Money def shot (self): print ("Shoot ing ...) def got_shot (self): print ("Ah...,i got shot ...") def buy_gun (self,gun_name): Print ("Just Bo Ught%s "%gun_name) r1 = role (' Alex ', ' police ', ' AK47 ') #生成一个角色, that is, generate an instance (object) r2 = role (' Jack ', ' terrorist ', ' B22 ') #生成一个角色, Generate an instance (object)
We see that when we created the role above, we did not pass the value to __init__, and the program did not give an error because the class was calling its own __init__ (...). When you assign a value to the self parameter,
r1
=
Role(
‘Alex‘
,
‘police‘
,
‘AK47’) #此时self 相当于 r1 , Role(r1,‘
Alex
‘,‘
police
‘,‘
AK47’)
r2
=
Role(
‘Jack‘
,
‘terrorist‘
,
‘B22’)#此时self 相当于 r2, Role(r2,‘
Jack
‘,‘
terrorist
‘,‘
B22’)
Python Learning path-day6-object oriented