当前位置: 代码迷 >> 综合 >> Object-Oriented Python
  详细解决方案

Object-Oriented Python

热度:59   发布时间:2023-11-04 21:32:20.0

1.define an empty class

use pass to avoid error, python doesn’t let us define classes or functions when they are empty
在这里插入图片描述

2. 类中函数的第一个参数总是对象本身,写作self

在这里插入图片描述

3. attributes

The power of objects is in their ability to store data,and the data is stored inside objects using attributes. You can think of attributes like special variables that belongs to a particular class. When we instantiate an object, most of the time we specify the data that we want to store inside that object.
在这里插入图片描述

4. example

1.在这里插入图片描述make sure you use self.data, otherwise you are not creating an attribute, you’re just assigning to a variable. To access our attribute from within the method, we need to use self.data
2.以下例子出现报错: object() takes no parameters找了很久的错误结果发现初始化函数必须是左右两条下划线**init**在这里插入图片描述在这里插入图片描述

  相关解决方案