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

时间:2016-03-16 18:30:48

标签: java static-methods static-variables

我有一些问题代码,我从一本不应该说非静态变量或方法必须是静态的书中提取。变量的错误来自于我没有用该代码改变任何东西。我添加代码后,方法中出现错误,但现在即使我把代码拿出来,它仍然给我错误。我在格式货币语句的getFormattedPrice方法中的Product.java中有变量错误。当我调用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);
    }
}

1 个答案:

答案 0 :(得分:2)

从此方法中删除static

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

您在实例变量中得到错误,因为price,而不是静态类变量。

并将static添加到private void writeProducts,以便从其他静态方法调用writeProducts