【问题标题】:Method with 3 parameters but accepting one?具有 3 个参数但接受一个参数的方法?
【发布时间】:2015-03-04 15:07:21
【问题描述】:

有没有办法让 Method 接受 3 个或更多参数但可以接受 1 个?

public void getAllSongs(String one, String two, String Three){
dosomething
}
getAllSongs("tada");

也许不是最好的解释方式,但我不知道该怎么做。 我想以更多方式使用相同的方法.. 有可能吗?

【问题讨论】:

  • 您是否在考虑类似varargs 的内容?另一种方法是方法重载。
  • 你可以使用数组只使用一个函数 --> 看我下面的帖子

标签: java methods parameters


【解决方案1】:

另一种方法是编写类似的代码

public void getAllSongs(String... songs){
    for(String song : songs){
       //do somethihg
    }
}

这样你就可以像这样调用你的代码了

getAllSongs("song");
getAllSongs("song1", "song2"......)

【讨论】:

    【解决方案2】:

    您可以使用方法重载:

    public void getAllSongs(String one ){
      getAllSongs(one,null,null);
    }
    
    public void getAllSongs(String one, String two, String Three){
    dosomething
    }
    getAllSongs("tada");
    

    【讨论】:

      【解决方案3】:

      可以像这样调用具有可变数量参数的方法

      public void getAllSongs(String ... songs)
      

      【讨论】:

        【解决方案4】:

        没有。 但它可能在 C++/C# 中(使用默认值分配参数) 例如。 :

        public void getAllSongs(String one, String two=string.Empty, String Three=string.Empty){
        dosomething
        }
        getAllSongs("tada");
        

        但是,您也可以使用 Pattern

            public void getAllSongs(String one, String two, String Three){
                dosomething
                }
            public void getAllSongs(String one, String two){
                getAllSongs(one,two,"");
                }
            public void getAllSongs(String one){
                getAllSongs(one,"","");
                }
                getAllSongs("tada");
        

        【讨论】:

        • 它是我会使用它的模式,但我很懒,试图寻找是否有办法只制作一个。但看来我必须工作:)
        【解决方案5】:

        我不知道你想问什么。 但我认为应该是

            public void getAllSongs(String... number) {
            // do something
            }
        

        【讨论】:

          【解决方案6】:

          用不同数量的参数重载相同的方法,如下所示:

          public void getAllSongs(String arg1) {
              getAllSongs(arg1, "default arg2", "default arg3");
          }
          
          public void getAllSongs(String arg1, String arg2) {
              getAllSongs(arg1, arg2, "default arg3");
          }
          
          public void getAllSongs(String arg1, String arg2, String arg3) {
              // do stuff with args
          }
          

          【讨论】:

            【解决方案7】:

            您可以通过使用(重载)具有不同数量的输入值的方法来解决它。用户可以只用一/二/三...值调用一个。如有必要,这个会调用具有更多值的那个。可能是这样的:

            public void getAllSongs(String one){
              getAllSongs(one, null, null)
            }
            
            public void getAllSongs(String one, String two){
              getAllSongs(one, two, null)
            }
            
            public void getAllSongs(String one, String two, String Three){
            dosomething
            }
            

            一个电话可能是:

            getAllSongs("bla","blub");
            

            使用数组的另一种可能性:

            public void getAllSongs(ArrayList<String> songs){ 
            do something 
            }; 
            
            List<String> songs= new ArrayList<String>();
            songs.add("bla");
            songs.add("blub");
            getAllSongs(songs);
            

            ArrayList 是一个数组,其大小取决于其中值的数量。在你的函数中,你可以迭代这个数组

            【讨论】:

            • 另一种可能性是使用包含歌曲标题的数组。借助此功能,您可以使用动态数量的歌曲
            • 这基本上就是 varargs 在后台所做的,它只是允许您跳过手动创建数组
            【解决方案8】:

            当然可以,这叫做多态性,或者更具体地说是方法重载

            在Java中,可以在一个类中定义两个或多个同名方法,只要参数列表或参数不同。这个概念被称为方法重载。

            看看Polymorphism in Java – Method Overloading and Overriding

            例子:

            void demo (int a) {
               System.out.println ("a: " + a);
            }
            void demo (int a, int b) {
               System.out.println ("a and b: " + a + "," + b);
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2019-06-28
              • 1970-01-01
              • 2017-12-29
              • 1970-01-01
              • 2014-01-24
              • 1970-01-01
              • 2022-01-17
              • 2019-09-21
              相关资源
              最近更新 更多