繁体   English   中英

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

[英]Method and variable have non-static needs to be static error

我从书中摘录了一些问题代码,这些代码不应说非静态变量或方法必须是静态的。 变量上的错误来自于我无需更改该代码的任何内容。 我向其中添加代码后出现了方法中的错误,但是现在即使我取出代码,它仍然会给我错误。 在格式货币语句的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);
    }
}

从此方法中删除static

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

之所以会出现错误,是因为price在实例变量而不是静态类变量中。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM