174.md 5.2 KB
Newer Older
W
wizardforcel 已提交
1
# Python 中的面向对象编程
W
init  
wizardforcel 已提交
2 3 4

> 原文: [https://javabeginnerstutorial.com/python-tutorial/object-oriented-programming-in-python/](https://javabeginnerstutorial.com/python-tutorial/object-oriented-programming-in-python/)

W
wizardforcel 已提交
5
与在 Python 中一样,在开发程序时不必将代码创建为类,因为我们可以使用也称为过程编程的函数。 但是,过程序编程用于编写小型,简短和简单的程序,而面向对象的编程(OOP)程序随着程序的大小和复杂性的增长而变得越来越重要。 自从开发以来,Python 一直是面向对象的语言。
W
init  
wizardforcel 已提交
6 7 8 9 10

因此,让我们简要介绍一下所使用的面向对象的编程概念-

**类**

W
wizardforcel 已提交
11
与其他编程语言相比,Python 中的类概念被添加了最少的新语法和语义。 Python 中类的概念是 C++ 和 Modula-3 中类的混合。 Python 类提供 OOP 的所有基本功能,例如允许多个基类的类继承,可以覆盖其基类的任何方法的派生类以及可以使用相同名称调用基类的方法的方法。
W
init  
wizardforcel 已提交
12 13 14

首先看课

W
wizardforcel 已提交
15
在 Python 中,使用新的语法和语义引入了类。
W
init  
wizardforcel 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

类定义语法:

```java
class ClassName:

<statement 1>

<statement 2>

.

.

.

<statement n>
```

W
wizardforcel 已提交
35
**示例**
W
init  
wizardforcel 已提交
36

W
wizardforcel 已提交
37
下面给出的是一个简单的 Python 类的示例:
W
init  
wizardforcel 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

```java
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 ):

W
wizardforcel 已提交
54
     print The number of students are: %d  % Student.stuCount
W
init  
wizardforcel 已提交
55 56 57 58 59 60

def displayStudent( self ):

print Name :  , self.name , , Roll No :  , self.rollno
```

W
wizardforcel 已提交
61
在上面的代码中,变量`stuCount`是一个类变量,其值在学生类的所有实例之间共享。 可以从类内部或类外部以`Student.stuCount`访问该变量。 第一个方法`init()`是一种特殊的方法,称为类构造器,或者是在创建类的新实例时调用的初始化方法。 调用方法时,Python 本身会添加`self`参数。
W
init  
wizardforcel 已提交
62 63 64

**对象**

W
wizardforcel 已提交
65
对象是 Python 面向对象程序的基本构建块。
W
init  
wizardforcel 已提交
66 67 68 69 70 71 72 73 74

```java
stu1 = Student(Raj , 34) // first object of student class

stu2 = Student(Reema , 12) //second object of student class
```

属性

W
wizardforcel 已提交
75
属性是对象的特征。 `__init__()`方法用于初始化对象的属性。 要访问对象的属性,我们对对象使用点运算符。 喜欢
W
init  
wizardforcel 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

```java
stu1.displayStudent( )

stu2.displayStudent( )

print The number of students are: %d  % Student.stuCount
```

因此,完整的程序是:

```java
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 ):

W
wizardforcel 已提交
102
     print The number of students are: %d  % Student.stuCount
W
init  
wizardforcel 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

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
```

**继承**

W
wizardforcel 已提交
121
继承 Python 中 OOP 的另一个功能,它是从现有类构建新类的一种方式,它们被称为派生类。 派生类是从基类派生或继承的。 继承的主要优点是可以重用代码,并可以降低程序的复杂性。 派生类扩展了基类的功能。
W
init  
wizardforcel 已提交
122

W
wizardforcel 已提交
123
**示例:**
W
init  
wizardforcel 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152

```java
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
```

W
wizardforcel 已提交
153
**输出**
W
init  
wizardforcel 已提交
154 155 156 157 158 159 160 161 162 163 164

```java
Calling car class

Mercedes,BMW

Calling vehicle class

Calling vehicle1 method
```

W
wizardforcel 已提交
165
**多态**
W
init  
wizardforcel 已提交
166

W
wizardforcel 已提交
167
多态是一个过程,其中函数以不同的方式用于不同的输入。 基本上,多态是如果类 B 从类 A 继承而来,则它不能继承类 A 的所有内容,因此可以继承类 A 的某些功能。
W
init  
wizardforcel 已提交
168

W
wizardforcel 已提交
169
**示例:**
W
init  
wizardforcel 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

```java
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( )
```

W
wizardforcel 已提交
207
**输出:**
W
init  
wizardforcel 已提交
208 209 210 211 212 213 214 215 216 217 218

```java
Programming books:

In python world

In java world
```

**运算符重载**

W
wizardforcel 已提交
219
在 Python 中,类可以使用特殊的方法名称进行操作,但是不能直接调用这些方法,只能使用特定的语法。
W
init  
wizardforcel 已提交
220

W
wizardforcel 已提交
221
**示例:**
W
init  
wizardforcel 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245

```java
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
```