小编use*_*861的帖子

Python全局变量和类功能

我创建了一个简单的python程序,它提供了SMS_Inbox的基本功能.我创建了一个SMS_Inbox方法.

store = []
message_count = 0
class sms_store:
    def add_new_arrival(self,number,time,text):
        store.append(("From: "+number, "Recieved: "+time,"Msg: "+text))
        **message_count += 1**
    def delete(self,i):
        if i > len(store-1):
            print("Index does not exist")
        else:
            del store[i]
            message_count -= 1
Run Code Online (Sandbox Code Playgroud)

在粗体位我得到一个错误:

UnboundLocalError: local variable 'message_count' referenced before assignment.
Run Code Online (Sandbox Code Playgroud)

我创建了一个全局变量存储,它是一个空列表,这在我使用add_new_variable对象时有效.但是由于某种原因,它没有向我的全局message_count变量添加值.

请帮忙

python class

8
推荐指数
2
解决办法
5万
查看次数

使用"这个".在Java中的构造函数中

我已经设置了这样的构造函数:

public class VendingMachine {

    private double currentBalance;
    private double itemPrice;
    private double totalCollected;

    public VendingMachine(double itemCost) {
        currentBalance = 0;
        totalCollected = 0;
        itemPrice = itemCost;
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

我的问题是通过接受double的参数来设置我的构造函数有什么不同itemCost.

有什么不同而不是做到:

this.itemPrice = itemCost;
Run Code Online (Sandbox Code Playgroud)

java constructor

3
推荐指数
1
解决办法
156
查看次数

标签 统计

class ×1

constructor ×1

java ×1

python ×1