21.md 3.3 KB
Newer Older
W
init  
wizardforcel 已提交
1 2 3 4 5 6
# Python 类:对象和类

> 原文: [https://pythonspot.com/objects-and-classes](https://pythonspot.com/objects-and-classes)

## 介绍

W
wizardforcel 已提交
7
技术总是在发展。什么是类,它们从何而来?
W
init  
wizardforcel 已提交
8

W
wizardforcel 已提交
9
**1\. 语句**:在计算的早期,程序员仅编写命令。
W
init  
wizardforcel 已提交
10

W
wizardforcel 已提交
11
**2\. 函数**:可重用的语句组,有助于结构化代码并提高了可读性。
W
init  
wizardforcel 已提交
12

W
wizardforcel 已提交
13
**3\. 类**:这些类用于创建具有功能和变量的对象。 字符串是对象的示例:字符串书具有功能`book.replace()``book.lowercase()`。这种样式通常称为面向对象编程。
W
init  
wizardforcel 已提交
14

W
wizardforcel 已提交
15
让我们一起深入吧!
W
init  
wizardforcel 已提交
16 17 18

## Python 类

W
wizardforcel 已提交
19
我们可以在 Python 中创建虚拟对象。 虚拟对象可以包含变量和方法。 程序可能具有许多不同的类型,并且是从类创建的。 例:
W
init  
wizardforcel 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

```py
class User:
    name = ""

    def __init__(self, name):
        self.name = name

    def sayHello(self):
        print("Hello, my name is " + self.name)

# create virtual objects
james = User("James")
david = User("David")
eric = User("Eric")

# call methods owned by virtual objects
james.sayHello()
david.sayHello()

```

W
wizardforcel 已提交
42
运行该程序。 在此代码中,我们有 3 个虚拟对象:`james``david``eric`。 每个对象都是`User`类的实例。
W
init  
wizardforcel 已提交
43 44 45

![python class: creation of objects](img/4915adfc6d1a53dddb74cfc74d45644b.jpg)

W
wizardforcel 已提交
46
Python 类:创建对象
W
init  
wizardforcel 已提交
47

W
wizardforcel 已提交
48
在此类中,我们定义了`sayHello()`方法,这就是为什么我们可以为每个对象调用它的原因。`__init__()`方法被称为**构造函数**,并且在创建对象时始终被调用。该类拥有的变量在这种情况下为`name`。这些变量有时称为类属性。
W
init  
wizardforcel 已提交
49

W
wizardforcel 已提交
50
我们可以在类中创建方法来更新对象的内部变量。这听起来可能有些含糊,但我将举一个例子进行说明。
W
init  
wizardforcel 已提交
51 52 53

## 类变量

W
wizardforcel 已提交
54
我们定义了一个`CoffeeMachine`类,其中的虚拟对象包含大量的咖啡豆和大量的水。 两者都定义为数字(整数)。然后我们可以定义添加或删除豆子的方法。
W
init  
wizardforcel 已提交
55 56 57 58 59 60 61 62 63 64

```py
def addBean(self):
    self.bean = self.bean + 1

def removeBean(self):
    self.bean = self.bean - 1

```

W
wizardforcel 已提交
65
对于水,我们也是如此。如下所示:
W
init  
wizardforcel 已提交
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

```py
class CoffeeMachine:
    name = ""
    beans = 0
    water = 0

    def __init__(self, name, beans, water):
        self.name = name
        self.beans = beans
        self.water = water

    def addBean(self):
        self.beans = self.beans + 1

    def removeBean(self):
        self.beans = self.beans - 1

    def addWater(self):
        self.water = self.water + 1

    def removeWater(self):
        self.water = self.water - 1

    def printState(self):
        print "Name  = " + self.name
        print "Beans = " + str(self.beans)
        print "Water = " + str(self.water)

pythonBean = CoffeeMachine("Python Bean", 83, 20)
pythonBean.printState()
print ""
pythonBean.addBean()
pythonBean.printState()

```

W
wizardforcel 已提交
103
运行该程序。 代码的顶部定义了我们所描述的类。 下面的代码是我们创建虚拟对象的地方。 在此示例中,我们只有一个对象称为`pythonBean`。 然后我们调用更改内部变量的方法,这是可能的,因为我们在类中定义了这些方法。 输出:
W
init  
wizardforcel 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117

```py

Name  = Python Bean
Beans = 83
Water = 20

Name  = Python Bean
Beans = 84
Water = 20

```

[下载练习](https://pythonspot.com/download-oop-exercises/)