Java成员变量初始化和构造函数
public class VendingMachine {
int price = 80;
int balance;
int total;
VedingMachine()
{
total = 1;
}
VedingMachine(int price)
{
this.price = price
}
void setPrice(int price)
{
this.price = price;
}
void showPrompt()
{
System.out.println("Welcome");
}
void insertMoney(int amount)
{
balance = this.balance + amount;
showBalance();
}
void showBalance()
{
System.out.println(balance);
}
void getFood()
{
if(balance >= price)
{
System.out.println("Here you are.");
balance = balance - price;
total = total + price;
}
}
public static void main(String[] args) {
VendingMachine vm = new VendingMachine();
vm.showPrompt();
vm.insertMoney(100);
vm.getFood();
vm.showBalance();
VendingMachine vm1 = new VendingMachine(100);
vm1.insertMoney(200);
vm1.showBalance();
}
}