在Java中将静态值分配给对象值

时间:2020-06-30 15:26:35

标签: java object static

我是学习Java的初学者。我有以下问题;

我想将静态变量Dog.averageWeight的值分配给对象变量dog1.weight。

我想要dog.weight = 3;

我尝试了不同的选择,但是没有用。有人可以告诉我这是否有效吗?

    public static int numberOfAllDogs;
    public static int totalWeight;
    public static int averageWeight;

    public String name;
    public int weight;

    public void initialize(String name) {
        this.name = name;
        this.weight = weight;

        numberOfAllDogs++;

    }

    public void initialize(String name, int weight) {
        this.name = name;
        this.weight = weight;

        numberOfAllDogs++;
        totalWeight = totalWeight + this.weight;
    }

    public static void main(String[] args) {

        Dog dog1 = new Dog();
        Dog dog2 = new Dog();

        dog1.initialize("daisy");
        dog2.initialize("chuck", 5);
        dog2.initialize("marco", 5);
        dog2.initialize("emy", 5);

        averageWeight = totalWeight / numberOfAllDogs;

        System.out.println("Total weight: " + totalWeight); //15
        System.out.println("Avvarage weight: " + averageWeight); // 3
        System.out.println(dog2.weight);
        System.out.println(dog1.weight);
    }
}

1 个答案:

答案 0 :(得分:1)

首先改变 public static int averageWeight;

public static double averageWeight = 0;

解决方案

public void initialize(String name, int weight) {
        this.name = name;
        this.weight = weight;

        numberOfAllDogs++;
        totalWeight = totalWeight + this.weight;
        averageWeight = ((double)totalWeight / numberOfAllDogs);
    }

因此,当您新建一条狗时,averageWeight随之变化

但是我实际上将重构此代码:

class Dog
{
    /* You cannot change it by yourself so make it private */
    private static int numberOfAllDogs = 0;
    private static int totalWeight = 0;
    private static double averageWeight = 0;

    private String name;
    private int weight;
    /* constructor for dog without weight :D (idk why you created this one) */
    public Dog (String name){ 
        this.name = name;
        weight = 0;
        numberOfAllDogs++;
        averageWeight = ((double)totalWeight / numberOfAllDogs);
    }
     /* constructor for dog*/
    public Dog (String name,int weight){
        this.name = name;
        this.weight = weight;
        numberOfAllDogs++;
        totalWeight += this.weight;
        averageWeight = ((double)totalWeight / numberOfAllDogs);
    }
    /* getters and setters */

    /* you can only get static content, not to change, cuz of your task*/
    public static int getNumberOfAllDogs() {
        return numberOfAllDogs;
    }

    public static int getTotalWeight() {
        return totalWeight;
    }

    public static double getAverageWeight() {
        return averageWeight;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setWeight(int weight) {
        totalWeight -= this.weight;
        this.weight = weight;
        totalWeight += weight;
        averageWeight = ((double)totalWeight / numberOfAllDogs);
    }

     public int getWeight() {
        return weight;
    }
}
// our public class 
public class Main {
    public static void main(String[] args){
        Dog dog1 = new Dog("Doggy1"); // dog1 creation
        Dog dog2 = new Dog("Doggy2", 5); // dog2 creation
        System.out.println("Total weight: " + Dog.getTotalWeight()); // total weight
        System.out.println("Avvarage weight: " + Dog.getAverageWeight()); // avg weight
        System.out.println(dog1.getWeight()); // dog1 weight
        System.out.println(dog2.getWeight()); // dog2 weight
    }
}
相关问题