多语言展示
当前在线:1571今日阅读:91今日分享:37

如何用PYTHON简单理解类,对象和方法的关系

用PYTHON简单理解类,对象和方法的关系
工具/原料

PYTHON

方法/步骤
1

打开JUPYTER NOTEBOOK,新建一个PY文档。

2

大哥最喜欢吃了小哥最喜欢喝了假设我们要开发一个类针对上述的要求。

3

class Brother:首先我们创建一个类,这个类假设叫做哥哥类。

4

class Brother:    def eating():类创建完了以后就要方法,这个方法和定义函数很类似。

5

class Brother:    def eating(self):创建方法以后第一个参数一定要是self。

6

class Brother:    def eating(self):        print('love eating!')同函数一样,这个方法需要返回或者处理某件事情。

7

class Brother:    def eating(self):        print('love eating!')    def drinking(self):        print('love drinking!')这个时候再继续创建其它方法。

8

brother1 = Brother()brother2 = Brother()这个时候我们创建两个对象,两个对象需要运用到Brother这个类。

9

brother1.eating()brother2.drinking()两个对象运用方法的时候和平时用的方法是一样的操作。

10

type(brother1.eating())而这个返回值不属于任意类型。

注意事项

理解整个句子的每一个词的关系就可以理解了

推荐信息