Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java—跟踪对象类型_Java_Oop - Fatal编程技术网

Java—跟踪对象类型

Java—跟踪对象类型,java,oop,Java,Oop,假设我有抽象类Car,有x个具体类(RedCar,BlueCar…)扩展了类Car。然后我有一个有财产车的车库 我想为Garage类编写一个布尔方法,如果找到搜索的汽车(作为参数传递的具体类型(RedCar,BlueCar…)之一),它将循环遍历汽车并返回true,否则返回false 最好的方法是什么 现在,我有这样的想法: public boolean hasCar(Class<? extends Car> c) { for (int i = 0; i < this.

假设我有抽象类Car,有x个具体类(RedCar,BlueCar…)扩展了类Car。然后我有一个有财产车的车库

我想为Garage类编写一个布尔方法,如果找到搜索的汽车(作为参数传递的具体类型(RedCar,BlueCar…)之一),它将循环遍历汽车并返回true,否则返回false

最好的方法是什么

现在,我有这样的想法:

public boolean hasCar(Class<? extends Car> c) {
    for (int i = 0; i < this.cars.length; i++) {
        if (c.isInstance(this.cars[i])) {
            return true;
        }
    }
    return false;
}

public boolean hasCar(Class如果所有子类都没有向基类添加任何状态,也没有通过向基类添加或重写方法来更改或添加行为,那么就不应该有子类。只要有一个带有
type
属性的Car类就足够了

如果子类存在的理由充分,那么您确实可以使用enum,但这将阻止添加任何其他类型的car(同时不更改enum)


拥有一个搜索特定类型汽车的方法对我来说就像是设计的味道。这可能意味着汽车类没有足够的多态性:instanceof和Cast不是很面向对象。

如果所有子类都没有向基类添加任何状态,也没有通过向基类的/添加或重写方法来更改或添加行为类,那么就不应该有子类。只要有一个带有
类型
属性的Car类就足够了

如果子类存在的理由充分,那么您确实可以使用enum,但这将阻止添加任何其他类型的car(同时不更改enum)


拥有一种搜索特定类型汽车的方法对我来说就像一种设计味道。这可能意味着汽车类没有足够的多态性:instanceof和Cast不是很面向对象。

如果你确实坚持将不同颜色的汽车作为子类型,我会说你的第一个版本更好,第二个版本更好每次添加新类型的car时都会更新枚举,并且会涉及到构造函数的大量复制和粘贴样式编码


然而,我建议,如果你想在车库里搜索汽车的某些颜色,最好将颜色作为汽车的属性,而不是检查汽车的类型。在你的第二个例子中,代码看起来“更干净”,因为你没有使用反射,但你基本上得到了与汽车颜色相同的重复信息通过其类型和CarType属性来表示。简单地使用一个带有颜色值的单一汽车类型将使此代码更短、更整洁。尽管如此,我不知道您最终想要如何处理此代码,因此您创建子类型的方法可能仍然是最好的-请接受我所说的话。如果您确定我坚持使用不同颜色的汽车作为子类型,我想说你的第一个版本更好-你的第二个版本将涉及每次添加新汽车类型时更新枚举,并涉及大量的复制和粘贴样式编码


然而,我建议,如果你想在车库里搜索汽车的某些颜色,最好将颜色作为汽车的属性,而不是检查汽车的类型。在你的第二个例子中,代码看起来“更干净”,因为你没有使用反射,但你基本上得到了与汽车颜色相同的重复信息通过其类型和CarType属性来表示。简单地使用一个带有颜色值的单一汽车类型将使此代码变得更短、更整洁。尽管如此,我不知道您最终想要如何处理此代码,因此您创建子类型的方法可能仍然是最好的-请恕我直言。

枚举解决方案只有当你想确认找到了某一类型的汽车时,你的帖子才会起作用。而不是特定的汽车对象本身。此外,如果你的汽车子类除了类型变量之外没有任何区别,你可能不需要子类

第一种方法看起来不错,可以缩短(稍微缩短)如下:

public enum CarType{
    RED_CAR, BLUE_CAR
}

public abstract class Car{
    public CarType type;
    Car(CarType type){
        this.type = type;
    }
}

public class RedCar extends Car{
    public RedCar(){
        super(CarType.RED_CAR);
    }
}

public class BlueCar extends Car{
    public BlueCar(){
        super(CarType.BLUE_CAR);
    }
}

public class Garage{
    private Car[] cars;
    public boolean hasCar(CarType type) {
        for (int i = 0; i < this.cars.length; i++) {
            if(this.cars[i].type == type){
                return true;
            }
        }
        return false;
    }
}
public boolean hasCar(Class<? extends Car> c) {
    for (Car car : this.cars) {
        if (c.isInstance(car) {
            return true;
        }
    }
    return false;
}
public boolean matchesFilter(CarFilter filter)
{
 return filter.Color == "red"; //MAYBE case insensitive?
}

public boolean hasCar(Class您发布的枚举解决方案仅在您希望确认找到某一类型的汽车时有效。而不是特定的汽车对象本身。此外,如果您的汽车子类除了类型变量之外没有任何差异,您可能不需要子类

第一种方法看起来不错,可以缩短(稍微缩短)如下:

public enum CarType{
    RED_CAR, BLUE_CAR
}

public abstract class Car{
    public CarType type;
    Car(CarType type){
        this.type = type;
    }
}

public class RedCar extends Car{
    public RedCar(){
        super(CarType.RED_CAR);
    }
}

public class BlueCar extends Car{
    public BlueCar(){
        super(CarType.BLUE_CAR);
    }
}

public class Garage{
    private Car[] cars;
    public boolean hasCar(CarType type) {
        for (int i = 0; i < this.cars.length; i++) {
            if(this.cars[i].type == type){
                return true;
            }
        }
        return false;
    }
}
public boolean hasCar(Class<? extends Car> c) {
    for (Car car : this.cars) {
        if (c.isInstance(car) {
            return true;
        }
    }
    return false;
}
public boolean matchesFilter(CarFilter filter)
{
 return filter.Color == "red"; //MAYBE case insensitive?
}

public boolean hasCar(Class您可以使用的另一种方法是传递一个过滤器对象,并询问汽车本身是否与过滤器匹配。过滤器的实现细节可能取决于您的实际示例(甚至可以使用instanceof()进行比较),但在您给出的简化示例中,它可能是一个字符串或一个枚举,指定您要查找的颜色

public class CarFilter()
{
String Color;
}
在汽车的基类中,您有一个方法:

public boolean matchesFilter(CarFilter filter){} //maybe abstract?
在派生类RedCar中,该方法的实现方式如下:

public enum CarType{
    RED_CAR, BLUE_CAR
}

public abstract class Car{
    public CarType type;
    Car(CarType type){
        this.type = type;
    }
}

public class RedCar extends Car{
    public RedCar(){
        super(CarType.RED_CAR);
    }
}

public class BlueCar extends Car{
    public BlueCar(){
        super(CarType.BLUE_CAR);
    }
}

public class Garage{
    private Car[] cars;
    public boolean hasCar(CarType type) {
        for (int i = 0; i < this.cars.length; i++) {
            if(this.cars[i].type == type){
                return true;
            }
        }
        return false;
    }
}
public boolean hasCar(Class<? extends Car> c) {
    for (Car car : this.cars) {
        if (c.isInstance(car) {
            return true;
        }
    }
    return false;
}
public boolean matchesFilter(CarFilter filter)
{
 return filter.Color == "red"; //MAYBE case insensitive?
}

您可以使用的另一种方法是传递筛选器对象并询问汽车本身是否与筛选器匹配。筛选器的实施细节可能取决于您的实际示例(甚至可以使用instanceof()进行比较),但在您给出的简化示例中,它可能是一个字符串或一个枚举,指定您要查找的颜色

public class CarFilter()
{
String Color;
}
在汽车的基类中,您有一个方法:

public boolean matchesFilter(CarFilter filter){} //maybe abstract?
在派生类RedCar中,该方法的实现方式如下:

public enum CarType{
    RED_CAR, BLUE_CAR
}

public abstract class Car{
    public CarType type;
    Car(CarType type){
        this.type = type;
    }
}

public class RedCar extends Car{
    public RedCar(){
        super(CarType.RED_CAR);
    }
}

public class BlueCar extends Car{
    public BlueCar(){
        super(CarType.BLUE_CAR);
    }
}

public class Garage{
    private Car[] cars;
    public boolean hasCar(CarType type) {
        for (int i = 0; i < this.cars.length; i++) {
            if(this.cars[i].type == type){
                return true;
            }
        }
        return false;
    }
}
public boolean hasCar(Class<? extends Car> c) {
    for (Car car : this.cars) {
        if (c.isInstance(car) {
            return true;
        }
    }
    return false;
}
public boolean matchesFilter(CarFilter filter)
{
 return filter.Color == "red"; //MAYBE case insensitive?
}

要回答这个问题,我必须问我自己哪一个提供了最简单的可扩展性?我会说第一个实现。

要回答这个问题,我必须问我自己哪一个提供了最简单的可扩展性?我会说第一个实现。

这两种方法在我看来都是一样的,但我只想知道哪一种方法更有效这将如何处理经验丰富的程序您应该存储它们