Object-Oriented Programming: classes, Objects
Object-oriented programming is a way of programming, which requires "classes" and "objects" to be implemented, so object-oriented programming is actually the use of "classes" and "objects".
Introduction to Object-oriented technology
- Class: A collection of objects that describe the same properties and methods, which defines the properties and methods that are common to each object in the collection.
- Object: An instance of a class
creating classes and Objects
1 classPerson :2 3 def __init__(self,name,age):4Self.name =name5Self.age = Age6 7 defTalk (self,msg):8 Print("%s-talk:%s"%(self.name,self.msg))9obj = Person ("Alex", "23")
- Class is the keyword representation, followed by the class name "person"
- Create Object obj = Person (), class name parentheses (arguments, parameters to pass the constructor method into)
- What's a self?
Self:python automatically passes the parameters that represent the object of your instance. The corresponding properties and methods of the object can be called through self.
- Construction method: "__init__ (Self,name,age)"; As long as the object is created, the method is automatically executed and then I can access it directly with the object: Obj.name &obj.age
1P_obj1 = Person ("Alex"," at")2P_obj2 = Person ("LCY"," -")3 Print(P_obj1.name,p_obj1.age)#Alex4 Print(P_obj2.name,p_obj2.age)#LCYObject-oriented three major features: encapsulation, inheritance, polymorphic encapsulation
The path to Python-object oriented (Basic)