【发布时间】:2014-06-08 18:21:59
【问题描述】:
我们将重点关注 5 个重要的类:Person、Car、House、Job 和 ISP。这是一个类的例子,这样我们你就可以对我想说的有一个直观的了解。语法会有点不对。
public class Person() {
private Car car;
private House house;
private Job job;
private ISP isp;
public Person(Car car, House house, Job job, ISP isp) {
this.car = car;
this.house = house;
...
}
public Car getCar() {
...
}
}
public class Car() {
private Make make;
private Model model;
private Color color;
...
}
public class House() {
...
}
我决定创建一个静态类;此类将仅包含将接受参数并执行某种操作的静态方法。这里有 3 个例子,我想知道哪种方式是最好的方式。我非常想弄清楚什么时候应该通过包含所有必需信息的课程,什么时候应该只通过细节,并找出它们之间的优缺点。
public static boolean hasStableLife(Person person) { //This is a made up method and the logic may not make sense at all
Car car = person.getCar();
House house = house.getHouse();
Job job = job.getJob();
if(car.getValue() > 40000 && (house.getValue() > 300000 && house.hasNoLoans()) && job.isFullTime()) {
return true;
}
return false;
}
public static boolean hasStableLife(Car car, House house, Job job) { //This is a made up method and the logic may not make sense at all
if(car.getValue() > 40000 && (house.getValue() > 300000 && house.hasNoLoans()) && job.isFullTime()) {
return true;
}
return false;
}
public static boolean hasStableLife(long carValue, long houseValue, boolean hasNoLoans, boolean isFullTime) { //This is a made up method and the logic may not make sense at all
if(carValue > 40000 && (houseValue > 300000 && hasNoLoans) && isFullTime) {
return true;
}
return false;
}
【问题讨论】:
标签: java methods parameters