gpt4 book ai didi

java - 方法和变量有非静态需要静态错误

转载 作者:行者123 更新时间:2023-12-02 03:49:06 24 4
gpt4 key购买 nike

我从书中提取了一些问题代码,这些代码不应该说非静态变量或方法需要是静态的。变量上的错误出现时,我没有对该代码进行任何更改。该方法中的错误是在我向其中添加代码后出现的,但现在即使我取出代码,它仍然会给我错误。我在 Product.java 中格式货币语句的 getFormattedPrice 方法中遇到变量错误。当我调用 writeProducts 时,方法错误出现在 main 方法中。

public class Product
{
private String code;
private String description;
private double price;

public Product(String code, String description, double price)
{
this.code = code;
this.description = description;
this.price = price ;
}

public Product()
{
this("", "", 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 static String getFormattedPrice()
{
NumberFormat currency = NumberFormat.getCurrencyInstance();
return currency.format(price);
}

public String getFormattedPrice(double price)
{
NumberFormat currency = NumberFormat.getCurrencyInstance();
return currency.format(price);
}

public boolean equals(Object object)
{
if (object instanceof Product)
{
Product product2 = (Product) object;
if
(
code.equals(product2.getCode()) &&
description.equals(product2.getDescription()) &&
price == product2.getPrice()
)
return true;
}
return false;
}

public String toString()
{
return "Code: " + code + "\n" +
"Description: " + description + "\n" +
"Price: " + this.getFormattedPrice() + "\n";
}
}

主要方法

public class XMLTesterApp
{
private static String productsFilename = "products.xml";

public static void main(String[] args)
{
System.out.println("Products list:");
ArrayList<Product> products = readProducts();
printProducts(products);

for(Product p2 : products){
System.out.println(p2.getPrice());
}
Product p1 = new Product("test", "XML Tester", 77.77);
products.add(p1);
writeProducts(products);
System.out.println("XML Tester has been added to the XML document.\n");


System.out.println("Products list:");
products = readProducts();
printProducts(products);


products.remove(2);
writeProducts(products);
System.out.println("XML Tester has been deleted from the XML document.\n");


System.out.println("Products list:");
products = readProducts();
printProducts(products);

}

private static ArrayList<Product> readProducts()
{
ArrayList<Product> products = new ArrayList<>();
Product p = null;
XMLInputFactory inputFactory = XMLInputFactory.newFactory();
try{
FileReader fileReader = new
FileReader("C:\\Users\\AndrewSpiteri\\Documents\\Classes\\Baker\\CS 242\\java\\netbeans\\ex_starts\\ch19_ex1_XMLTester\\products.xml");
XMLStreamReader reader = inputFactory.createXMLStreamReader(fileReader);

while(reader.hasNext()){
int eventType = reader.getEventType();
switch(eventType){
case XMLStreamConstants.START_ELEMENT:
String elementName = reader.getLocalName();
if(elementName.equals("Product")){
p = new Product();
String code = reader.getAttributeValue(0);
p.setCode(code);
}
if(elementName.equals("Description")){
String description = reader.getElementText();
p.setDescription(description);
}
if(elementName.equals("Price")){
String priceString = reader.getElementText();
double price = Double.parseDouble(priceString);
p.setPrice(price);
}
break;
case XMLStreamConstants.END_ELEMENT:
elementName = reader.getLocalName();
if(elementName.equals("Product"))
products.add(p);
break;
default:
break;
}
reader.next();
}
}
catch(IOException | XMLStreamException e){
System.out.println(e);
}
// add code that reads the XML document from the products.xml file

return products;
}

private void writeProducts(ArrayList<Product> products)
{
XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();
try{
FileWriter fileWriter = new
FileWriter("C:\\Users\\AndrewSpiteri\\Documents\\Classes\\Baker\\CS 242\\java\\netbeans\\ex_starts\\ch19_ex1_XMLTester\\products.xml");
XMLStreamWriter writer = outputFactory.createXMLStreamWriter(fileWriter);
writer.writeStartDocument("1.0");
writer.writeStartElement("Products");
for(Product product : products){
writer.writeStartElement("Product");
writer.writeAttribute("Code", product.getCode());
writer.writeStartElement("Description");
writer.writeCharacters(product.getDescription());
writer.writeEndElement();
writer.writeStartElement("Price");
double price = product.getPrice();
writer.writeCharacters(Double.toString(price));
writer.writeEndElement();
writer.writeEndElement();
}
writer.writeEndElement();
writer.flush();
writer.close();
}
catch(IOException | XMLStreamException e){
System.out.println(e);
}
}

private static void printProducts(ArrayList<Product> products)
{
for (Product p : products)
{
printProduct(p);
}
System.out.println();
}

private static void printProduct(Product p)
{
String productString =
StringUtils.padWithSpaces(p.getCode(), 8) +
StringUtils.padWithSpaces(p.getDescription(), 44) +
p.getFormattedPrice();

System.out.println(productString);
}
}

最佳答案

从此方法中删除static

public static String getFormattedPrice()
{
NumberFormat currency = NumberFormat.getCurrencyInstance();
return currency.format(price);
}

您会收到错误,因为 price 位于实例变量中,而不是静态类变量中。

并将static添加到private void writeProducts,以便从另一个静态方法调用writeProducts

关于java - 方法和变量有非静态需要静态错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36044096/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com