For example...
if a > 0 then
return a
else
return b
How do you change this form of if clause beautifully??
@Override
public int compareTo(Object obj) {
if (this.make.compareTo(((Car) obj).getMake()) != 0) {
return (this.make.compareTo(((Car) obj).getMake()));
} } else {
return (this.model.compareTo(((Car) obj).getModel()));
}
}
I'm really worried lol QT
if문
I don't know Java well, but the code doesn't seem like my business, so I'm writing an answer to interfere.
First of all, considering the question you asked, I think it can be organized into trivial operator and local variable in this way.
@Override
public int compareTo(Object obj) {
int make = this.make.compareTo(((Car) obj).getMake());
return make != 0 ? make : this.model.compareTo(((Car) obj).getModel()));
}
But actually, this isn't really the problem. What is the real problem with this code? The problem is that the object (Car) obj
is still alive, but he's so bad at taking care of himself that he can't compare himself to something else (this.make
here), and he's handing over his wallet to the newspaper delivery man. What else is the newspaper delivery man talking about? It's a very famous fable in object-oriented programming theory, and the newspaper delivery man doesn't ask for the customer's wallet at the door, saying he'll collect the subscription fee from the customer next month. Usually, the newspaper delivery man only asks, "Please give me a subscription fee," and how much money you have in your wallet and how much you have to pay for the subscription fee is something that newspaper subscriber customers have to do on their own.
In short, The story should be configured so that other objects that use an object do not write the properties of that object as it is. If you review the current code again with this principle, To sum up, given now compareTo()
corresponds to the newspaper delivery and (Car)obj
corresponds to the customer, for example, it is better to fix the from determining if it is a situation to write
getMake()
now. compareTo()
should only receive the determined result as it is and do something, but should not call the getMake()
method first This is itif... else...
I think it's not a problem to be advanced conditioning, but a context. Actually, I'm not sure what code this is, so I can't get any more specific interference. I hope it helps!
© 2024 OneMinuteCode. All rights reserved.