多个实例不能使用相同的变量,变量不能更改

时间:2013-08-30 10:03:34

标签: java

我不太确定这有什么问题,我确信它与范围有关。昨天我遇到了一个问题,即由于我不止一次地调用该方法,一个字段会将自身初始化为零,使得一个类字段固定,因为它保留了它的值,无论调用任何方法的时间长短。

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

以下是代码:

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?什么是最好的方法来解决这个问题?

3 个答案:

答案 0 :(得分: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");
}

答案 1 :(得分: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

答案 2 :(得分: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();
}

}