# Python 中的面向对象编程 > 原文: [https://javabeginnerstutorial.com/python-tutorial/object-oriented-programming-in-python/](https://javabeginnerstutorial.com/python-tutorial/object-oriented-programming-in-python/) 与在 Python 中一样,在开发程序时不必将代码创建为类,因为我们可以使用也称为过程编程的函数。 但是,过程序编程用于编写小型,简短和简单的程序,而面向对象的编程(OOP)程序随着程序的大小和复杂性的增长而变得越来越重要。 自从开发以来,Python 一直是面向对象的语言。 因此,让我们简要介绍一下所使用的面向对象的编程概念- **类** 与其他编程语言相比,Python 中的类概念被添加了最少的新语法和语义。 Python 中类的概念是 C++ 和 Modula-3 中类的混合。 Python 类提供 OOP 的所有基本功能,例如允许多个基类的类继承,可以覆盖其基类的任何方法的派生类以及可以使用相同名称调用基类的方法的方法。 首先看课 在 Python 中,使用新的语法和语义引入了类。 类定义语法: ```py class ClassName: . . . ``` **示例**: 下面给出的是一个简单的 Python 类的示例: ```py class Student: //common base class for all students stuCount=0 def_init_(self, name, rollno): self.name = name self.rollno = rollno Student.stuCount += 1 def displayCount( self ): print “The number of students are: %d ” % Student.stuCount def displayStudent( self ): print “Name : ” , self.name , “, Roll No : ” , self.rollno ``` 在上面的代码中,变量`stuCount`是一个类变量,其值在学生类的所有实例之间共享。 可以从类内部或类外部以`Student.stuCount`访问该变量。 第一个方法`init()`是一种特殊的方法,称为类构造器,或者是在创建类的新实例时调用的初始化方法。 调用方法时,Python 本身会添加`self`参数。 **对象** 对象是 Python 面向对象程序的基本构建块。 ```py stu1 = Student(“Raj” , 34) // first object of student class stu2 = Student(“Reema” , 12) //second object of student class ``` 属性 属性是对象的特征。 `__init__()`方法用于初始化对象的属性。 要访问对象的属性,我们对对象使用点运算符。 喜欢 ```py stu1.displayStudent( ) stu2.displayStudent( ) print “The number of students are: %d ” % Student.stuCount ``` 因此,完整的程序是: ```py class Student: //common base class for all students stuCount=0 def_init_(self, name, rollno): self.name = name self.rollno = rollno Student.stuCount += 1 def displayCount( self ): print “The number of students are: %d ” % Student.stuCount def displayStudent( self ): print “Name : ” , self.name , “, Roll No : ” , self.rollno stu1 = Student(“Raj” , 34) // first object of student class stu2 = Student(“Reema” , 12) //second object of student class stu1.displayStudent( ) stu2.displayStudent( ) print “The number of students are: %d ” % Student.stuCount ``` **继承** 继承 Python 中 OOP 的另一个功能,它是从现有类构建新类的一种方式,它们被称为派生类。 派生类是从基类派生或继承的。 继承的主要优点是可以重用代码,并可以降低程序的复杂性。 派生类扩展了基类的功能。 **示例:** ```py class Vehicle: // base class def _init_( self ): print “Calling vehicle class” def vehicle1( self ): print “Calling vehicle1 method” class Car(Vehicle): //derived class def _init_(self): print “Calling car class” def car1(self): print “Mercedes,BMW,” c= Car( ) //instance of car class c.car1( ) //calling derived class method c.vehicle1( ) //calling base class method ``` **输出**: ```py Calling car class Mercedes,BMW Calling vehicle class Calling vehicle1 method ``` **多态** 多态是一个过程,其中函数以不同的方式用于不同的输入。 基本上,多态是如果类 B 从类 A 继承而来,则它不能继承类 A 的所有内容,因此可以继承类 A 的某些函数。 **示例:** ```py class Books: def _init_(self, name=‘ ’): self.name = name def programming(self): print “Programming books:” class Python(Books): def programming(self): print “In python world” class Java(Books): def programming(self): print “In java world” b = Books( ) b.programming( ) p = Python( ) p.programming( ) j = Java( ) j.programming( ) ``` **输出:** ```py Programming books: In python world In java world ``` **运算符重载** 在 Python 中,类可以使用特殊的方法名称进行操作,但是不能直接调用这些方法,只能使用特定的语法。 **示例:** ```py class Addition: def _init_(self,a,b): self.a = a self.b =b def _str_(self): return ‘Addition (%d, %d) ’ % (self.a, self.b) def _add_(self,other): return Addition(self.a + other.a, self.b + other.b) a1 = Addition(5,10) a2 = Addition(2,3) print a1 + a2 ```