简体   繁体   English

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

[英]Creating class with variables vs passing variables as parameters into a method

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. 假设我正在创建一个带有两个方法的类,该方法采用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? 最好有两个实例变量来存储这两个值,然后有一个使用它们的方法,还是在调用该方法时仅将变量作为参数传递?

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? 还是只使用静态方法会更好,因此我不需要每次使用该方法时都创建一个对象?

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() (对象的创建和计算的时间可能会有所不同)是有意义的,但是以后还要使用value1value2在实例的另一种方法中。

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. 根据需要使用一种或另一种。

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). 然后确定这些类需要哪些类和哪些成员(方法/变量)。

As a example, these two int value1; int value2; 例如,这两个int value1; int value2; int value1; int value2; variable may be needed when you want to perform another tasks using same values. 当您想使用相同的值执行其他任务时,可能需要该变量。 Such as divide() , multiply() , etc. 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. 使用像public int MyMethod(int value1, int value2){方法,您只能执行任务。 You won't be able to save variables values to perform another task. 您将无法保存变量值来执行其他任务。 Like that methods helpful to divide tasks. 像那样的方法有助于划分任务。

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 并阅读这些javanoteq1q2

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: 如果将value1和value2命名为engineWeight和wheelsWeight,则可以命名方法:

    public int calculateWeightSum(){ return value1 + value2; public int computeWeightSum(){返回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. Java没有“全局”变量。 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. 在这种情况下,最好使用静态方法来完成此工作。

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. 现在,我仅描述了有关使用不同方法的便利性的想法。

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

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