1

我不太确定这有什么问题,我确信它与范围有关。昨天我遇到了一个问题,由于我不止一次地调用该方法,一个字段将自己初始化回零,使一个类字段修复了这个问题,因为它保留了它的值,而不管调用了多少次任何方法。

现在我遇到了相反的问题,我需要重置该字段,因为另一个对象需要使用它(这是可能的/不好的做法吗?)

这是代码:

public class TestDigitalCamera {

    static String brand;
    static double megaPixels;
    static double price;

    //create 2 camera instances with the values of the variables tied to the arguments.
    static DigitalCamera camera = new DigitalCamera(brand, megaPixels);
    static DigitalCamera camera2 = new DigitalCamera(brand, megaPixels);


    public static void main(String[] args) {

        //no idea what this technique is called, need to look back but I know what it does
        //I could use a for loop and reuse the same object over and over(would that even work anyway?) but the task says
        //that i require 4 instances, ofc I am just working with 2 atm for simplicity
        camera = getInformation(camera);
        displayInfo();
        camera2 = getInformation(camera2);
        displayInfo();


    }

    //it basically runs this for the camera instance...right? lol
    public static DigitalCamera getInformation(DigitalCamera dc){
        Scanner userInput = new Scanner(System.in);

        //self explanatory
        System.out.println("Enter brand: ");
        brand = userInput.next();
        System.out.println("Enter Mega Pixels: ");
        megaPixels = userInput.nextDouble();

        //I have another class setup with getters/setters for this, which get used in the next method
        dc.setBrand(brand);
        dc.setMegaPixels(megaPixels);
        return dc;      

    }

    public static void displayInfo(){

        //users the getters to pull the values
        //the price is calculated using an if statement
        System.out.println("Brand: " + camera.getBrand() + "\n"
                + "Megapixels : " + camera.getMegaPixels() + "\n"
                        + "Price : $" + camera.getPrice() + "\n");
    }

}

这是因为范围吗?该变量可用于任何和所有对象,但它只能用于 1?解决这个问题的最佳方法是什么?

4

3 回答 3

2

你有这个代码:

camera = getInformation(camera);
displayInfo();
camera2 = getInformation(camera2);
displayInfo();

在这里,您的方法在调用它的时候displayInfo()并没有真正从camera对象中获取任何参数和打印信息。即使您camera2在第二次调用中获得了对象的引用,getInformation您并没有真正打印它。

你可以displayInfo这样声明:

public static void displayInfo(DigitalCamera camera) {
    //users the getters to pull the values
    //the price is calculated using an if statement
    System.out.println("Brand: " + camera.getBrand() + "\n"
         + "Megapixels : " + camera.getMegaPixels() + "\n"
         + "Price : $" + camera.getPrice() + "\n");
}
于 2013-08-30T10:07:04.823 回答
0

您应该像这样重构您的代码:

public static void displayInfo(DigitalCamera camera){

    //users the getters to pull the values
    //the price is calculated using an if statement
    System.out.println("Brand: " + camera.getBrand() + "\n"
            + "Megapixels : " + camera.getMegaPixels() + "\n"
                    + "Price : $" + camera.getPrice() + "\n");
}

然后在main方法中:

public static void main(String[] args) {

    camera = getInformation(camera);
    displayInfo(camera);
    camera2 = getInformation(camera2);
    displayInfo(camera2);


}

如果你在这样的方法中声明你的两个DigitalCamera实例main

DigitalCamera camera = getInformation(camera);

那么就不需要单独的静态变量了。

我强烈建议您阅读 Java 教程中关于类和实例成员的部分:http: //docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

于 2013-08-30T10:15:20.477 回答
0

我会这样写你的课

公共类 TestDigitalCamera {

public static test_display(){
    DigitalCamera camera = getCamera();
    System.out.println("Brand: " + camera.getBrand() + "\n"
            + "Megapixels : " + camera.getMegaPixels() + "\n"
            + "Price : $" + camera.getPrice() + "\n");

}

public static getCamera(){
    Scanner userInput = new Scanner(System.in);

    //self explanatory
    System.out.println("Enter brand: ");
    brand = userInput.next();
    System.out.println("Enter Mega Pixels: ");
    megaPixels = userInput.nextDouble();

    //I have another class setup with getters/setters for this, which get used in the next method
    DigitalCamera dc = new DigitalCamera();
    dc.setBrand(brand);
    dc.setMegaPixels(megaPixels);
    return dc;
}

public static void main(String[] args) {
    test_displainfo();
}

}

于 2013-08-30T10:18:22.217 回答