5

Can anyone explain how this code snippet works... The actual code itself is not relevant as it was from a short tutorial on using an MVP pattern for Android.

My main question is how this code structure works and whether this is an inner class, of sorts, or maybe a transaction.. I haven't seen a code structure like this in Java and I would like to undertsand it to learn from it as it seems efficient and minimal.

public void loadCustomer(int id) {
    (mCustomerModel.load(id)) {
        mCustomerView.setId(mCustomerModel.getId());
        mCustomerView.setFirstName(mCustomerModel.getFirstName());
        mCustomerView.setLastName(mCustomerModel.getLastName());
    }
}
4

1 回答 1

-2

这是直截了当的,但肯定显得有些不寻常。mCustomerModel.load(id) 周围的圆括号是多余的,在这种情况下,接下来三行周围的花括号也是多余的。在另一种情况下,如果在该块中声明了一个局部变量,则它的范围将仅限于该块。下面的代码是等价的:

public void loadCustomer(int id) {
    mCustomerModel.load(id);
    mCustomerView.setId(mCustomerModel.getId());
    mCustomerView.setFirstName(mCustomerModel.getFirstName());
    mCustomerView.setLastName(mCustomerModel.getLastName());

}

编辑:错过了圆括号内的行应该是 if 语句的意图。在这种情况下,一旦添加了“if”,代码就很容易解释了。

于 2013-05-16T22:56:04.973 回答