Python Object-oriented
Python has been an object-oriented language since its inception, which is why it is easy to create a class and object in Python.
Introduction to Object-oriented technology
- class: used to describe a collection of objects that have the same properties and methods. It defines the properties and methods that are common to each object in the collection. An object is an instance of a class.
- Class variables: class variables are common throughout the instantiated object. Class variables are defined in the class and outside the body of the function. Class variables are not typically used as instance variables.
- data members: class variables or instance variables are used to manipulate the data related to the class and its instance objects.
- method Overrides: if the method inherited from the parent class does not meet the requirements of the subclass, it can be overridden, which is called the override of the method, also known as the override of the method.
- instance variable: A variable defined in a method that acts only on the class of the current instance.
- inheritance: A derived class (derived class) that inherits the fields and methods of the base class. Inheritance also allows the object of a derived class to be treated as a base class object. For example, there is a design where an object of type dog is derived from the animal class, which is the analog "is a (is-a)" Relationship (example, dog is a animal).
- instantiation: Creates an instance of a class, the concrete object of the class.
- method: a function defined in a class.
- object: An instance of a data structure defined by a class. The object consists of two data members (class variables and instance variables) and methods.
The classes in Python provide all the basic functions of object-oriented programming: The inheritance mechanism of classes allows for multiple base classes, and derived classes can overwrite any method in the base class, and the method can be called in the base class with the same name.
objects can contain any number and type of data
Class definition
Syntax format:
# !/usr/bin/env python # -*-coding:utf-8-*- # Author:jerry Shi class ClassName: <statement-1> ... <statement-N>#!/usr/bin/env python# -*-coding:utf-8-*-# author:jerry Shiclass ClassName (object): <statement-1> . . . <statement-N>
Class object
The class object supports two operations: Property Reference and instantiation.
The property reference uses the same standard syntax as all property references in Python:obj.name.
After a class object is created, all the names in the class namespace are valid property names. So if the class definition is this:
#!/usr/bin/env python#-*-coding:utf-8-*-#Author:jerry ShiclassMyClass:"""a simple instance of a class"""I= 12345deff (self):return 'Hello World'#Instantiating Classesx =MyClass ()#accessing the properties and methods of a classPrint("the attribute I of the MyClass class is:", X.I)Print("the method F output of the MyClass class is:", X.F ())#Instantiating Classesx =MyClass ()#accessing the properties and methods of a class
Many classes tend to create objects with an initial state. So the class might define a special method (constructor) named __init__ (), like this:
# !/usr/bin/env python # -*-coding:utf-8-*- # Author:jerry Shi def __init__ (self): = []
Of course, the __init__ () method can have parameters, and arguments are passed through __init__ () to the instantiation of the class. For example:
# !/usr/bin/env python # -*-coding:utf-8-*- # Author:jerry Shi class Complex: def __init__ (self, Realpart, Imagpart): = Realpart = imagpart = Complex (3.0,-4.5)
Methods of the class
Inside the class, using the DEF keyword, you can define a method for a class that differs from a generic function definition, and that the class method must contain the parameter self and be the first parameter:
#!/usr/bin/env python#-*-coding:utf-8-*-#Author:jerry Shi#class definitionclasspeople:#Defining basic PropertiesName ="' Age=0#define private properties, private properties cannot be accessed directly outside the class __weight=0#Defining construction Methods def __init__(self,n,a,w): Self.name=N self.age=a self.__weight=WdefSpeak (self):Print("%s said: I am%d years old. "%(self.name,self.age))#Instantiating Classesp = People ('Test', 10,30) P.speak ()
Python Basic Learning (v)