First, object-oriented introduction
Before you introduce object orientation, let's review the process-oriented programming thought that we learned before.
Process-oriented Programming:
The core is the process of the word, the process refers to the steps to solve the problem, that is, what to do first, what to do after doing, based on the idea of programming is like in the design of a pipeline, is a mechanized way of thinking
Advantages: Complex problems are streamlined and simplified
Cons: Poor scalability
Now it's your turn to object-oriented programming ideas!
Object-oriented programming, the core is the object of two words, the object is a combination of features and skills, based on the idea of writing programs (the brain always want to be the object of the word), like creating a world, in God's eyes any existence is the object, any non-existent things can be created, is a kind of God-like way of thinking
Advantages: Strong Scalability
Cons: Programming is more complex than process-oriented
Second, class
Objects are a combination of features and skills, while classes are a combination of features and skills of a series of objects.
Emphasize:
1. The object is a concrete existence, and the class is an abstract concept
2. The classes and objects that are summed up at different points of view are different
In the real world, there is a concrete existence of the object, and then with the development of human civilization to summarize the concept of class
In the program, the class is defined before the class is called to produce the object
The most common of the classes is the definition of variables and functions, but any Python code can exist in the class body.
To define a class:
Class statement to create a new definition of a class followed by a colon ":"
class Student: # the same characteristics ' Superschool ' # the same skills def Choose_course (self): Print ('Choosing course')
The class body code executes immediately at the class definition stage, resulting in a class namespace that is used to throw the class body code in.
To view the namespace of a class:
Print (Student. __dict__
Modify, add, delete
' SUPPER ' 'China'
Del Student.country
Summary class:
1. A class is essentially a namespace, or a container for storing variables and functions
2. One of the purposes of the class is to use the name space to remove the name from its interior.
3. The purpose of the class is to invoke the class to produce the object
Object:
The process of calling a class is called instantiation of the class, and the return value of the calling class is called an object/instance of the class
STU1 = Student ()
Customize your own unique features for your objects:
Stu1.name=' li tie egg 'stu1.age=18stu1.sex='male'
Stu2.name=' Zhao Gang bomb 'stu2.age=38stu2.sex='female '
As you can see, there's a redundant code here that we can optimize
def Init (obj,name,age,sex): = name = age = Sex
Does the resulting object make it easier to call the function every time, or is there a better way? There must be some answer.
classStudent:#the same characteristicsSchool ='Oldboy' #stu1, ' Li Tie egg ', ' male ' def __init__(obj, name, age, Sex): Obj.name= Name#stu1.name= ' li Tie Egg 'Obj.age = Age#stu1.age=18Obj.sex = Sex#stu1.sex= ' Male ' #the same skills defChoose_course (self):Print('Choosing Course')
The calling class first produces an empty object Stul, and then returns, triggering the execution of the __init__ in the class, passing the object along with the parameter specified in the calling class bracket.
Stu1=student (' li tie egg ', 'male'#__init__ (STU1, ' Li tie eggs ', ' male ')stu2=student (' Zhao gang ', 'female') #__init__ (stu2, ' Zhao bomb ', ' female ')
Summarize the function of __init__: It is the original property of the object when it is instantiated, note that there is no return value, the default return is None
Property Lookup:
1. Look in the object's own namespace first, but not the class you belong to.
2. Variables defined in a class are shared by all objects, objects can be used, and classes can be used, and once a class changes the value of its own data property, all objects can perceive it.
Binding method:
A function defined in a class is a function property of a class, a class can be used, a class call is a normal function, but a function defined in a class is used for an object, and it is used to bind to an object.
1. Function of class: pass several parameters
2. Binding method, a function that points to a class: What is special is the binding to whom it is called, and who will call it automatically as the first parameter is passed in.
Three, all objects
Unify the concepts of classes and types in Python3
classFoo:PassPrint(Foo) obj=Foo ()Print(Type (obj))Print(int) age=10#Age=int (Ten)Print(Type (age)) L1=[1,2,3]#l1=list ([i])#print (Type (L1))#L1.append (4)#print (L1)L1.append (4)#list.append (l1,4)Print(L1)
L1.append is equivalent to invoking the APPEND function in the list class
Object-oriented later continue to add, Tanabata just new objects!
python-Getting started with object orientation