我可以从条件运算符返回多个值 [英] Can I Return Multiple Values From Conditional Operator

查看:92
本文介绍了我可以从条件运算符返回多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要调用一个方法,比如说method2,它接受可变数量的参数,并且不能传递空值。我必须从method1调用这个方法2,它有两个参数,一个是默认参数。

场景如下:

I need to call a method,say method2, which accept variable number of parameters, and can not pass null values. I have to call this method2 from method1, which have two parameters, one is default parameter.
The scenario is like this:

void method1(string $p1,string $p2="")
{
    method2($p1,$p2,OtherParams);
}



因此可以在不指定两个参数的情况下调用method1。这会在调用method2时出现问题。 method2的所有参数都是字符串类型。



我已根据参数2($ P2)编写代码,如下所示,但客户端不接受代码重复。 (if条件中有更多代码)




Thus method1 can be called without specifying both the parameters. This makes problem while I call method2. All the parameters of method2 are of string type.

I have write code based on the parameter2($P2), as given bellow, but the client does not accept as code repetition. (there are much more code inside the if condition)

void method1(string $p1,string $p2="")
{
   if($p2=="")
    {
     method2($p1,$p2, OtherParams);
    //some other code
    }
   else
    {
        method2($p1 , OtherParams);
        //same code as in if block
    }
}







如何在不重复的情况下调用method2?

(Defenition) Method2()不可用,并由其他人编码。所以我无法更改该代码的代码.method2()根据参数的数量有不同的功能。)



我可以使用具有多个返回值的条件运算符吗?像这样:






how can I call the method2 without repetition?
(Defenition of Method2() is unavailable, and coded by someone else. so I can not change code for that one. The method2() have different functionality based on the number of parameters.)

Can I use conditional operator with multiple number of returning values? like this:

void method1(string $p1,string $p2="")
{
    method2($p1   (($p2=="")?(,) : (,$p2 ,) OtherParams);
    //other codes
}

推荐答案

p1, string
p1,string


p2 =
{
method2(
p2="") { method2(


p1,


这篇关于我可以从条件运算符返回多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆