简体   繁体   English

将对象值传递给方法

[英]passing object values to methods

I am trying to make a void method that calculates average, but I would like to pass values to the method from the main method. 我试图制作一个计算平均值的void方法,但我想将值从main方法传递给方法。 Here is an example of what I am trying to do: 这是我想要做的一个例子:

 public class Plant {

    int leaves;
    int age;
    int sumLeaves;
    double average;

    void averageLeaves() {

        sumLeaves = leaves + leaves; //here is where I need help
        average = (double) sumLeaves / 2;
        System.out.println("The average number of leaves is: " + average);

    }

    public static void main(String[] args) {

        Plant plantA = new Plant();
        plantA.leaves = 5;

        Plant plantB = new Plant();
        plantB.leaves = 3;

        averageLeaves();

    }

The problem I'm having is getting the values of multiple plants into the method. 我遇到的问题是将多种植物的价值纳入方法中。 I've tried using .get , loops, static , and many other things and haven't figured it out yet. 我已经尝试过使用.get ,循环, static和许多其他东西,但还没有想出来。 What I want is to the value of plantA.leaves and plantB.leaves to the method in order to find their sum. 我想要的是plantA.leavesplantB.leaves的值,以便找到它们的总和。

Since you have the averageLeaves() defined in the Plant class, You have to actually invoke the methode from one of the plant instance. 由于您具有在Plant类中定义的averageLeaves() ,因此您必须实际从其中一个工厂实例调用方法。 Here is how you should do it. 这是你应该怎么做的。

plantA.averageLeaves();

One big mistake you are doing here is, 你在这里犯的一个大错误是,

sumLeaves = leaves + leaves;         

This actually adds the leaves of a specific (one) instance of plants, It is wrong. 这实际上增加了特定(一个)植物实例的叶子,这是错误的。 you have to actually pass the number of leaves from difference instances. 你必须实际传递差异实例中的叶子数。

Here is a nicer way to do it using getters & setters. 使用getter和setter这是一个更好的方法。 Also it would make much sense to make the averageLeaves() method static. averageLeaves()方法averageLeaves()静态也是averageLeaves() That way you do not need an instance to invoke the method. 这样您就不需要实例来调用该方法。

    public class Plant {

    int leaves;
    int age;
    //int sumLeaves;  you do not need them now, as the averageLeaves method is static
    //double average;

    static void averageLeaves (int leaves1, int leaves2) {

        int sumLeaves = leaves2 + leaves1;               //here is where I need help
        double average = (double) sumLeaves / 2;
        System.out.println("The average number of leaves is: " + average);
    }

    void setLeaves(int leaves){
        this.leaves = leaves;
    }

    int getLeaves(){
        return this.leaves;
    }
}

    public class Main {

    public static void main(String[] args) {

        Plant plantA = new Plant();
        plantA.setLeaves(5);

        Plant plantB = new Plant();
        plantB.setLeaves(3);

        Plant.averageLeaves(plantA.getLeaves(), plantB.getLeaves());
    }
}

You will have to do it like this: 你必须这样做:

public class Plant 
{
    int age;
    int sumLeaves;
    double average;

    void averageLeaves (int leaves) 
    {
        sumLeaves = leaves + leaves;               //here is where I need help
        average = (double) sumLeaves / 2;
        System.out.println("The average number of leaves is: " + average);
    }
}

public class Example
{ 
        public static void main(String[] args) 
        {
            Plant plantA = new Plant();
            plantA.leaves = 5;

            Plant plantB = new Plant();
            plantB.leaves = 3;

            plantA.averageLeaves(plantA.leaves);
            plantB.averageLeaves(plantB.leaves);
        }
}

Look that you only have to declare the variables that you want to input into the method (inside the parenthesis) and when you use your method you have to pass the values that the method will use ( in order of declaration in the method ). 看看你只需要声明要输入到方法中的变量(在括号内),当你使用你的方法时,你必须传递方法将使用的值(按照方法中的声明顺序 )。

I expect it will be helpful for you! 我希望它对你有所帮助!

It's a little unclear what you mean. 有点不清楚你的意思。 Is the average of the plant itself? 植物本身的平均值是多少? The average of the two plants. 这两种植物的平均值。 I'm assuming the latter. 我假设后者。 Then one way of doing it is to invoke the method as a static member and pass in the leaves as arguments. 然后,一种方法是将方法作为静态成员调用,并将叶子作为参数传入。

public class Plant {

    int leaves;

    static void averageLeaves (int leaves1, int leaves2) {
        int sumLeaves = leaves1 + leaves2;    //here is where I need help
        double average = (double) sumLeaves / 2;
        System.out.println("The average number of leaves is: " + average);
    }

    public static void main(String[] args) {

        Plant plantA = new Plant();
        plantA.leaves = 5;

        Plant plantB = new Plant();
        plantB.leaves = 3;

        Plant.averageLeaves(plantA.leaves, plantB.leaves);

}

Here is a simple version that allows you to pass in a variable number of plants and calculate their average. 这是一个简单的版本,允许您传入可变数量的植物并计算它们的平均值。

public class Plant {
    private int leaves;

    public Plant(int leaves) {
        this.leaves = leaves;
    }

    public int getLeaves() {
        return this.leaves;
    }

    public static void main(String[] args) {
        PrintLeavesAverage(
            new Plant(5),
            new Plant(10),
            new Plant(15),
            new Plant(12)
        );
    }

    private static void PrintLeavesAverage(Plant... plants) {
        int totalLeaves = 0;
        for (Plant p : plants) {
            totalLeaves += p.getLeaves();
        }
        double average = (double)totalLeaves/plants.length;
        System.out.println("The average number of leaves is: " + average);
    }
}

... or if you want to get a bit fancier, and want to try out some of the newer Java 8 features, you can use streams to calculate the average like this: ...或者如果你想获得一点点发烧友,并想尝试一些较新的Java 8功能,你可以使用流来计算平均值,如下所示:

private static void PrintLeavesAverage(Plant... plants) {
    OptionalDouble average = Arrays.stream(plants).mapToInt(p -> p.leaves).average();

    if (average.isPresent()) {
        System.out.println("The average number of leaves is: " + average.getAsDouble());
    } else {
        System.out.println("No plants, no average");
    }
}

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

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