我需要在我拥有的产品类文件中添加一个受保护的变量(我已附加该文件的当前代码)。我需要将 count 变量的访问修饰符从 public 更改为 protected。我怎么做?!我需要在下面的代码中进行哪些更改以添加受保护的变量:
import java.text.NumberFormat;
public class Product
{
private String code;
private String description;
private double price;
public static int count = 0;
public Product()
{
code = "";
description = "";
price = 0;
}
public void setCode(String code)
{
this.code = code;
}
public String getCode(){
return code;
}
public void setDescription(String description)
{
this.description = description;
}
public String getDescription()
{
return description;
}
public void setPrice(double price)
{
this.price = price;
}
public double getPrice()
{
return price;
}
public String getFormattedPrice()
{
NumberFormat currency = NumberFormat.getCurrencyInstance();
return currency.format(price);
}
@Override
public String toString()
{
return "Code: " + code + "\n" +
"Description: " + description + "\n" +
"Price: " + this.getFormattedPrice() + "\n";
}
public static int getCount()
{
return count;
}
}