How to do object-oriented programming in python
Object-oriented programming is simply based on the use of classes and objects. All codes are implemented through classes and objects. Programming is object-oriented programming!
The three major characteristics of object-oriented: encapsulation, inheritance, polymorphism
First create a class
#使用class创建一个School类,类中有个student方法 class School: def student(self): pass a1=School()
1. Encapsulation
1. Encapsulation: Encapsulate certain content into a place first, and then call it when needed
class School: def __init__(self,name,age): #构造方法,创建对象是执行 self.name=name self.age=age#创建对象a1,a2a1=School("zhangsan",18) a2=School("lisi",18)
The above code implements the function of encapsulation, and encapsulates the respective name and age into the name and age attributes of self respectively. , it is equivalent to being encapsulated into objects a1 and a2. The functions defined in the
class are called methods, and the functions with __init__ are called constructors, which will be automatically executed when creating a1 and a2 objects.
2. Calling: There are two ways to call, direct calling through the object and indirect calling through self
Direct calling through the object
class School: def __init__(self,name,age): self.name=name self.age=age def student(self): print("name:%s,age:%s"%(self.name,self.age))#创建对象a1,a2a1=School("zhangsan",18) a2=School("lisi",18) print a1.name,a1.age print a2.name,a2.age#执行结果: zhangsan 18 lisi 18
Indirect calling through self
class School: def __init__(self,name,age): self.name=name self.age=age def student(self): print("name:%s,age:%s"%(self.name,self.age)) #创建对象a1,a2 a1=School("zhangsan",18) a2=School("lisi",18) #执行类中的方法时,通过self间接调用被封装的内容 a1.student() a2.student() #执行结果: #name:zhangsan,age:18 #name:lisi,age:18
2. Inheritance
1. Inheritance: both the derived class (subclass) can inherit the methods of the base class (parent class), and we can extract the methods common to multiple classes into the parent class, so that The subclass only needs to inherit the parent class without having to implement each method one by one.
Write another class in brackets after the class name to indicate that it has inherited that class
class School: def __init__(self,name,age): self.name=name self.age=age def student(self): print("name:%s,age:%s"%(self.name,self.age)) def classroom(self): print("%s去教室"%self.name) class SchoolA(School): #SchoolA继承School def __init__(self,name): self.name=name class SchoolB(SchoolA): #SchoolB继承SchoolA def __init__(self,name): self.name=name #创建对象a1 a1=SchoolA("zhangsan") a1.classroom() #创建对象a2 a2=SchoolB("lisi") a2.classroom() #执行结果: # zhangsan去教室 # lisi去教室
In the above code we As you can see, there is no classroom method in SchoolA and SchoolB, but because SchoolB inherits SchoolA, and SchoolA inherits School, they can
call the classroom method in School after creating the object.
2. Multiple inheritance
In python, a class can also inherit multiple classes. When inheriting multiple classes, there are two ways to search for functions in the class
Depth priority: When the class is a classic class, in the case of multiple inheritance, it will be searched in a depth-first manner
Breadth priority: When the class is a new-style class, in the case of multiple inheritance, it will be searched in a breadth-first manner
(in python3.
The above is the detailed content of How to do object-oriented programming in python. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Introduction In today's rapidly evolving digital world, it is crucial to build robust, flexible and maintainable WEB applications. The PHPmvc architecture provides an ideal solution to achieve this goal. MVC (Model-View-Controller) is a widely used design pattern that separates various aspects of an application into independent components. The foundation of MVC architecture The core principle of MVC architecture is separation of concerns: Model: encapsulates the data and business logic of the application. View: Responsible for presenting data and handling user interaction. Controller: Coordinates the interaction between models and views, manages user requests and business logic. PHPMVC Architecture The phpMVC architecture follows the traditional MVC pattern, but also introduces language-specific features. The following is PHPMVC

SOLID principles are a set of guiding principles in object-oriented programming design patterns that aim to improve the quality and maintainability of software design. Proposed by Robert C. Martin, SOLID principles include: Single Responsibility Principle (SRP): A class should be responsible for only one task, and this task should be encapsulated in the class. This can improve the maintainability and reusability of the class. classUser{private$id;private$name;private$email;publicfunction__construct($id,$nam

PHP extensions can support object-oriented programming by designing custom functions to create objects, access properties, and call methods. First create a custom function to instantiate the object, and then define functions that get properties and call methods. In actual combat, we can customize the function to create a MyClass object, obtain its my_property attribute, and call its my_method method.

In high-concurrency scenarios of object-oriented programming, functions are widely used in the Go language: Functions as methods: Functions can be attached to structures to implement object-oriented programming, conveniently operating structure data and providing specific functions. Functions as concurrent execution bodies: Functions can be used as goroutine execution bodies to implement concurrent task execution and improve program efficiency. Function as callback: Functions can be passed as parameters to other functions and be called when specific events or operations occur, providing a flexible callback mechanism.

PHP's object-oriented programming paradigm provides advantages for project management and organization With the rapid development of the Internet, websites and applications of all sizes have sprung up. In order to meet the growing needs and improve development efficiency and maintainability, the use of object-oriented programming (Object-Oriented Programming, OOP for short) has become the mainstream of modern software development. In dynamic scripting languages like PHP, OOP brings many advantages to project management and organization. This article will introduce

What is object-oriented programming? Object-oriented programming (OOP) is a programming paradigm that abstracts real-world entities into classes and uses objects to represent these entities. Classes define the properties and behavior of objects, and objects instantiate classes. The main advantage of OOP is that it makes code easier to understand, maintain and reuse. Basic Concepts of OOP The main concepts of OOP include classes, objects, properties and methods. A class is the blueprint of an object, which defines its properties and behavior. An object is an instance of a class and has all the properties and behaviors of the class. Properties are characteristics of an object that can store data. Methods are functions of an object that can operate on the object's data. Advantages of OOP The main advantages of OOP include: Reusability: OOP can make the code more

1. Introduction to Python Python is a general-purpose programming language that is easy to learn and powerful. It was created by Guido van Rossum in 1991. Python's design philosophy emphasizes code readability and provides developers with rich libraries and tools to help them build various applications quickly and efficiently. 2. Python basic syntax The basic syntax of Python is similar to other programming languages, including variables, data types, operators, control flow statements, etc. Variables are used to store data. Data types define the data types that variables can store. Operators are used to perform various operations on data. Control flow statements are used to control the execution flow of the program. 3.Python data types in Python

Functional and Object-Oriented Programming (OOP) provide different programming mechanisms in C++: Function: independent block of code, focused on performing a specific task, containing no data. OOP: Based on objects, classes and inheritance, data and behavior are encapsulated in objects. In actual cases, the function method for calculating the area of a square is simple and direct, while the OOP method encapsulates data and behavior and is more suitable for managing object interaction. Choosing the appropriate approach depends on the scenario: functions are good for independent tasks, OOP is good for managing complex object interactions.
