174.md 5.2 KB
Newer Older
W
init  
wizardforcel 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
# 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中,使用新的语法和语义引入了类。

类定义语法:

```java
class ClassName:

<statement 1>

<statement 2>

.

.

.

<statement n>
```

**示例:**

下面给出的是一个简单的Python类的示例:

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

     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面向对象程序的基本构建块。

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

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

属性

属性是对象的特征。 _init_()方法用于初始化对象的属性。 要访问对象的属性,我们对对象使用点运算符。 喜欢

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

     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的另一个功能,它是从现有类构建新类的一种方式,它们被称为派生类。 派生类是从基类派生或继承的。 继承的主要优点是可以重用代码,并可以降低程序的复杂性。 派生类扩展了基类的功能。

**Example:**

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

**输出:**

```java
Calling car class

Mercedes,BMW

Calling vehicle class

Calling vehicle1 method
```

**多态性**

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

**Example:**

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

**Output:**

```java
Programming books:

In python world

In java world
```

**运算符重载**

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

**Example:**

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