使用变量创建类与将变量作为参数传递到方法中

时间:2022-03-31 21:26:08

Which is a better way to do this, in terms of performance or other factors?

在性能或其他因素方面,哪种方法更好?

Say I am creating a class with jut one method in it that takes 2 values and adds them together.

假设我正在创建一个带有jut one方法的类,它接受2个值并将它们加在一起。

Is it better to have 2 instance variables to store the 2 values then have a method which uses these, or is it better to just pass the variables in as parameters when calling the method?

有两个实例变量存储2个值然后有一个使用这些的方法更好,还是在调用方法时将变量作为参数传递更好?

Note: this class is only ever created just to use this method.

注意:此类只是为了使用此方法而创建的。

public class MyClass {
    int value1;
    int value2;

public MyClass(int value1, int value2){
    this.value1 = value1;
    this.value2 = value2;
}

public int MyMethod(){
    return value1 + value2;
}

or this way:

或者这样:

public class MyClass {

public MyClass(){

}

public int MyMethod(int value1, int value2){
    return value1 + value2;
}

Or would it be better to just use a static method, so I don't need to create an object every time I use the method?

或者使用静态方法会更好,所以每次使用方法时都不需要创建对象?

3 个解决方案

#1


1  

Which is a better way to do this, in terms of performance or other factors?

在性能或其他因素方面,哪种方法更好?

These ways have no relation with performance. These are way of designing.

这些方式与性能无关。这些是设计方式。

The first form (storing the arguments as fields):

第一种形式(将参数存储为字段):

public MyClass(int value1, int value2){
    this.value1 = value1;
    this.value2 = value2;
}

public int myMethod(){
    return value1 + value2;
}

makes sense if you want to save the passed information in the class in order to be able to invoke myMethod() when you want (the creation of the object and the computation may be differed in the time) but also to use value1 and value2 later in another methods of the instance.

如果你想在类中保存传递的信息以便能够在需要时调用myMethod()(对象的创建和计算可能在时间上有所不同),以及稍后使用value1和value2,这是有意义的在实例的另一种方法中。

The second form (using only the arguments) makes sense if you want only compute the result :

如果您只想计算结果,则第二种形式(仅使用参数)是有意义的:

public int myMethod(int value1, int value2){
    return value1 + value2;
}

Use one or the other one according to your need.

根据您的需要使用一个或另一个。

#2


1  

It depends very match on your intention.

这取决于你的意图。

  1. If you name you value1 and value2 as engineWeight and wheelsWeight then you can name your method:

    如果将name1和value2命名为engineWeight和wheelsWeight,则可以为方法命名:

    public int calculateWeightSum(){ return value1 + value2; }

    public int calculateWeightSum(){return value1 + value2; }

In such case it will have sense to have such kind of approach.

在这种情况下,采用这种方法是有意义的。

  1. The second approach(with method params) may be used as usual business logic of the object. You give variable to operate and it returns result. The result may depend on which object you make an invocation. That's the reason you don't mark this method as static. If you mark it as static, the object state won't affect the result.

    第二种方法(使用方法参数)可以用作对象的通常业务逻辑。您给变量操作并返回结果。结果可能取决于您进行调用的对象。这就是你不将此方法标记为静态的原因。如果将其标记为静态,则对象状态不会影响结果。

  2. Java doesn't have "global" variables. It has static public variables that can be treated very similarly. You can use them with both approaches, but the intention of using it here is very unclear. Probably in this case is better to make utility with static methods to do the job.

    Java没有“全局”变量。它具有可以非常相似地处理的静态公共变量。您可以将它们与两种方法一起使用,但在此处使用它的目的非常不明确。在这种情况下,可能最好使用静态方法来完成工作。

As for performance, it's pretty much the same in all the cases. But, in the approach with constructor you use additional memory to store field for each object. But, again. It depends very much on your intention, here it's difficult to judge until you provide more information about real task. For now I just described my ideas on how it's convenient to use different approaches.

至于性能,在所有情况下几乎都是一样的。但是,在构造函数的方法中,您使用额外的内存来存储每个对象的字段。但是,再一次。这在很大程度上取决于你的意图,在你提供有关真实任务的更多信息之前,很难判断。现在我刚才描述了我对如何使用不同方法的方便的想法。

#3


1  

In Java, variables that are in class called member or instance variables not global.

在Java中,类中的变量称为成员变量或实例变量不是全局变量。

public class MyClass {
    int value1;
    int value2;

These two variables need to use base upon your reqiurements. First of all implement make and structure, think about what do you want to achieve. Then decide what classes and what members are needed for these classes(methods/ variables).

这两个变量需要根据你的需求来使用。首先实现make和structure,想想你想要实现什么。然后确定这些类(方法/变量)需要哪些类和成员。

As a example, these two int value1; int value2; variable may be needed when you want to perform another tasks using same values. Such as divide(), multiply(), etc.

作为一个例子,这两个int value1; int value2;当您想要使用相同的值执行其他任务时,可能需要变量。如divide(),multiply()等。

public class MyClass {
    int value1;
    int value2;

    public MyClass(int value1, int value2){
        this.value1 = value1;
        this.value2 = value2;
    }

    public int add(){
        return value1 + value2;
    }

    public int sub(){
        return value1 - value2;
    }

    public int divide(){
        return value1 / value2;
    }

    public int multiply(){
        return value1 * value2;
    }
}

Using like public int MyMethod(int value1, int value2){ method you only can perform task. You won't be able to save variables values to perform another task. Like that methods helpful to divide tasks.

使用like public int MyMethod(int value1,int value2){method你只能执行任务。您将无法保存变量值以执行其他任务。像那些有助于划分任务的方法。

public class MyClass {
    public int add(int value1, int value2){
        return value1 + value2;
    }

    public int sub(int value1, int value2){
        return value1 - value2;
    }

    public int divide(int value1, int value2){
        return value1 / value2;
    }

    public int multiply(int value1, int value2){
        return value1 * value2;
    }
}

But you can take two arguments each for methods perform task return value, that's all(all are different, donot have anything related).

但是你可以为方法执行任务返回值各取两个参数,这就是全部(都是不同的,没有任何相关的东西)。

Summary:

Both have unique functionality and roll to play according to your requirements.

两者都具有独特的功能,可根据您的要求进行播放。

Clean code is good book to read. And read these javanote, q1 and q2

清洁代码是一本好书。并阅读这些javanote,q1和q2

#1


1  

Which is a better way to do this, in terms of performance or other factors?

在性能或其他因素方面,哪种方法更好?

These ways have no relation with performance. These are way of designing.

这些方式与性能无关。这些是设计方式。

The first form (storing the arguments as fields):

第一种形式(将参数存储为字段):

public MyClass(int value1, int value2){
    this.value1 = value1;
    this.value2 = value2;
}

public int myMethod(){
    return value1 + value2;
}

makes sense if you want to save the passed information in the class in order to be able to invoke myMethod() when you want (the creation of the object and the computation may be differed in the time) but also to use value1 and value2 later in another methods of the instance.

如果你想在类中保存传递的信息以便能够在需要时调用myMethod()(对象的创建和计算可能在时间上有所不同),以及稍后使用value1和value2,这是有意义的在实例的另一种方法中。

The second form (using only the arguments) makes sense if you want only compute the result :

如果您只想计算结果,则第二种形式(仅使用参数)是有意义的:

public int myMethod(int value1, int value2){
    return value1 + value2;
}

Use one or the other one according to your need.

根据您的需要使用一个或另一个。

#2


1  

It depends very match on your intention.

这取决于你的意图。

  1. If you name you value1 and value2 as engineWeight and wheelsWeight then you can name your method:

    如果将name1和value2命名为engineWeight和wheelsWeight,则可以为方法命名:

    public int calculateWeightSum(){ return value1 + value2; }

    public int calculateWeightSum(){return value1 + value2; }

In such case it will have sense to have such kind of approach.

在这种情况下,采用这种方法是有意义的。

  1. The second approach(with method params) may be used as usual business logic of the object. You give variable to operate and it returns result. The result may depend on which object you make an invocation. That's the reason you don't mark this method as static. If you mark it as static, the object state won't affect the result.

    第二种方法(使用方法参数)可以用作对象的通常业务逻辑。您给变量操作并返回结果。结果可能取决于您进行调用的对象。这就是你不将此方法标记为静态的原因。如果将其标记为静态,则对象状态不会影响结果。

  2. Java doesn't have "global" variables. It has static public variables that can be treated very similarly. You can use them with both approaches, but the intention of using it here is very unclear. Probably in this case is better to make utility with static methods to do the job.

    Java没有“全局”变量。它具有可以非常相似地处理的静态公共变量。您可以将它们与两种方法一起使用,但在此处使用它的目的非常不明确。在这种情况下,可能最好使用静态方法来完成工作。

As for performance, it's pretty much the same in all the cases. But, in the approach with constructor you use additional memory to store field for each object. But, again. It depends very much on your intention, here it's difficult to judge until you provide more information about real task. For now I just described my ideas on how it's convenient to use different approaches.

至于性能,在所有情况下几乎都是一样的。但是,在构造函数的方法中,您使用额外的内存来存储每个对象的字段。但是,再一次。这在很大程度上取决于你的意图,在你提供有关真实任务的更多信息之前,很难判断。现在我刚才描述了我对如何使用不同方法的方便的想法。

#3


1  

In Java, variables that are in class called member or instance variables not global.

在Java中,类中的变量称为成员变量或实例变量不是全局变量。

public class MyClass {
    int value1;
    int value2;

These two variables need to use base upon your reqiurements. First of all implement make and structure, think about what do you want to achieve. Then decide what classes and what members are needed for these classes(methods/ variables).

这两个变量需要根据你的需求来使用。首先实现make和structure,想想你想要实现什么。然后确定这些类(方法/变量)需要哪些类和成员。

As a example, these two int value1; int value2; variable may be needed when you want to perform another tasks using same values. Such as divide(), multiply(), etc.

作为一个例子,这两个int value1; int value2;当您想要使用相同的值执行其他任务时,可能需要变量。如divide(),multiply()等。

public class MyClass {
    int value1;
    int value2;

    public MyClass(int value1, int value2){
        this.value1 = value1;
        this.value2 = value2;
    }

    public int add(){
        return value1 + value2;
    }

    public int sub(){
        return value1 - value2;
    }

    public int divide(){
        return value1 / value2;
    }

    public int multiply(){
        return value1 * value2;
    }
}

Using like public int MyMethod(int value1, int value2){ method you only can perform task. You won't be able to save variables values to perform another task. Like that methods helpful to divide tasks.

使用like public int MyMethod(int value1,int value2){method你只能执行任务。您将无法保存变量值以执行其他任务。像那些有助于划分任务的方法。

public class MyClass {
    public int add(int value1, int value2){
        return value1 + value2;
    }

    public int sub(int value1, int value2){
        return value1 - value2;
    }

    public int divide(int value1, int value2){
        return value1 / value2;
    }

    public int multiply(int value1, int value2){
        return value1 * value2;
    }
}

But you can take two arguments each for methods perform task return value, that's all(all are different, donot have anything related).

但是你可以为方法执行任务返回值各取两个参数,这就是全部(都是不同的,没有任何相关的东西)。

Summary:

Both have unique functionality and roll to play according to your requirements.

两者都具有独特的功能,可根据您的要求进行播放。

Clean code is good book to read. And read these javanote, q1 and q2

清洁代码是一本好书。并阅读这些javanote,q1和q2