在创建对象构造函数时,如何避免重复参数名称(即几乎与对象的属性相同的名称)?

发布于 2025-01-28 06:17:43 字数 927 浏览 3 评论 0原文

我会提前道歉

我是OOP编程的新手,因此,如果下面这是一个非常简单的对象构造

function Class(assign_to_a, assign_to_b){
    this.a=assign_to_a;
    this.b=assign_to_b;
}
object=new Class(2,3);

函数,我确实觉得我确实在重复自己, 。我已经命名了类内部的对象的属性,那么为什么我需要在一个参数上重写相同的(稍微更改)名称,其唯一目的是将其分配给已经具有描述性名称的属性?

为了更好地了解我要实现的目标,JavaScript已经为此提供了一个整洁的解决方案:

function Class(){
    this.a=arguments[0];
    this.b=arguments[1];
}
object=new Class(2,3);

这样,当我已经知道所有参数都将被分配给属性时,我不必提出/重写参数名称创建后的对象。

我的问题是,我认为没有太多其他语言像JavaScript一样具有参数对象(尽管我对此可能是错误的),因此我必须找到另一个解决方法。现在,我正在尝试在Java中创建同一类。

public class Class{
    int a, b;
  
    public Class(int assign_to_a, int assign_to_b){
        a = assign_to_a;
        b = assign_to_a;
    }

    public static void main(String[] args) {
        Class object = new Class(2, 3);
    }
}

是否有一种方法可以解决对象构造函数参数的重复名称,类似于我在JavaScript中所做的类似,还是我不得不忍受的?

I'm VERY new to OOP programming, so I apologize in advance if this is a stupid/irrelevant question

Below is a very simple object constructor in JavaScript

function Class(assign_to_a, assign_to_b){
    this.a=assign_to_a;
    this.b=assign_to_b;
}
object=new Class(2,3);

I do feel like I'm repeating myself a little. I've already named the attribute of the object inside the class, so why do I need to rewrite the same (slightly altered) name on a parameter whose sole purpose is to be assigned to an attribute that already has a descriptive name?

To better understand what I am trying to achieve, JavaScript already offers a neat solution for this:

function Class(){
    this.a=arguments[0];
    this.b=arguments[1];
}
object=new Class(2,3);

This way, I won't have to come up with/rewrite parameter names, when I already know that all the arguments will be assigned to an attribute of the object upon creation.

My problem is, I don't think too many other languages have an arguments object like JavaScript does (though I could be wrong about this), so I will have to find another workaround. Right now, I'm trying to create the same class in Java.

public class Class{
    int a, b;
  
    public Class(int assign_to_a, int assign_to_b){
        a = assign_to_a;
        b = assign_to_a;
    }

    public static void main(String[] args) {
        Class object = new Class(2, 3);
    }
}

Is there a way to work around coming up with repetitive names for the parameters of the object constructor, similar to what I did in JavaScript, or is this something I'll simply have to put up with?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百变从容 2025-02-04 06:17:43

您可以让构造函数只采用一个参数,而不是创建多个参数: arraylist 。然后,您可以通过将其从选行器的阵列列表中抽出来设置对象的私人和公共属性。在Java中,这似乎是与JavaScript的“参数对象”(在类型灵活性方面)最接近的匹配。

我没有在Java中进行编码,但是基于您的示例,我认为它看起来可能是这样的:

public class Class{
    int a, b;
  
    public Class(ArrayList ary){
        a = ary[0];
        b = ary[1];
    }

    public static void main(String[] args) {
        ArrayList ary = new ArrayList();
        ary.add(2);
        ary.add(3);
        Class object = new Class(ary);
    }
}

我不知道这是否会编译,但希望它足够好,可以传达该概念。

Instead of creating multiple parameters, you could have the constructor take only one argument: an ArrayList. Then you can set the object's private and public properties by pulling them out of the contructor's ArrayList. In Java, this seems to be the closest match to JavaScript's "arguments object" (in terms of type flexibility).

I don't code in Java, but based on your example I assume that it might look something like this:

public class Class{
    int a, b;
  
    public Class(ArrayList ary){
        a = ary[0];
        b = ary[1];
    }

    public static void main(String[] args) {
        ArrayList ary = new ArrayList();
        ary.add(2);
        ary.add(3);
        Class object = new Class(ary);
    }
}

I have no idea if that will even compile, but hopefully it is good enough to relay the concept.

白龙吟 2025-02-04 06:17:43

据我所知,您无法避免在Java中的方法签名中指定参数。

但是我认为这是Java的重要功能,因为它是在编译时间检查参数类型,并避免将错误的参数传递给函数时出现的所有问题。

如果要避免重复和样板代码,则有很多可以提供帮助的库,例如“ Project Lombok”。

As far as i know you can't avoid specify the arguments in a method signature in Java.

But I think that it's a great feature of Java, because it's check the parameters type at compilation time and avoid all the problems that comes when you pass the wrong arguments to a function.

If you want avoid repetitive and boilerplate code there are lot of libraries to help, like "Project Lombok".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文