English 中文(简体)
我如何利用私人经营者来缩短这一守则?
原标题:How can I shorten this code using ternary operators?

我有以下法典:

public class direction do(direction)
    if(istrue) {
        left = do(left);
    } else {
        right = do(right);
    }
}

我很想知道,是否有办法缩短这一期限。 我尝试使用私人经营者,但有一些困难。 建议

问题回答

举例来说,对经营人没有明智的使用。

Java(和其他类似C的语文)的客座操作员的价值是,操作员的演讲是expression,而发言是statement。 言论产生价值,但声明不是。 如果你不使用由私人经营人expression<>em>产生的价值,那么你就没有使用私人经营人的商业。 做不到。

For your interest, here s an example of a valid use of the ternary operator:

int max(int x, int y) {
    return x > y? x : y;
}

You might use a ternary expression if the other programmers on your team don t mind. So if your example were in valid Java:

public void Do(Direction direction)
{
    (istrue)? left = Do(left) : right = Do(right);
}

You could omit the braces in the if:

public void Do(Direction direction)
{
    if ( istrue )
        left = Do(left);
    else
        right = Do(right);
}

Or you even could have a one liner:

public void Do(Direction d)    { (istrue)? left = Do(left) : right = Do(right); }

Above all, choose a style that s clear and not too clever. Use more lines if they make your code easier to read and understand.

通常有一线人可以读懂,但在某些情形下(对我而言),特别是如果你有非常相似的小方法的家庭:

public String getFirstName()        { return first_name; }
public String getLastName()         { return last_name; }
public String getAddress1()         { return address1; }
public String getAddress2()         { return address2; }
public String getCity()             { return city; }
public String getState()            { return state; }
public String getZip()              { return zip; }

您的代码要求您根据具体情况将两种不同的变量(leftright/code>)加以分配。 一种永恒的表述赢得你们的帮助,因为它不能在任务左侧使用。

That leaves the RHS of the (two) assignments. While you could in theory use a ternary expression, it will probably make the code longer.

因此,简短的答案是,一名私人经营人获得了一定帮助。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...