2014-01-13 50 views
0

我有一个超类和子类,并且我希望能够根据是否为超类实例调用方法来调用特定的方法实现,子类实例。我计划在if条件下使用instanceof操作符在Java中检查该条件。我想知道是否有任何其他方式(泛型,可能是?)来实现这一点比拥有这种if条件更好 - 反正这看起来并不整齐。此外,通过这种方法,我不得不做一个不安全的类型转换。当我不得不将objSuperClass类型转换为​​类型时。请建议。在超类和子类中实现具有相同名称但不同实现的方法

void doSomething (SuperClass obj) { 
    // Works the same way for super and sub class 
    int p = obj.getX1(obj); 
    int q = obj.getX2(obj); 

    int implementationSpecificVariable; 
    // choose behavior based on the actual class 
    if (obj instanceof SuperClass) { 
     implementationSpecificVariable = doForSuper(obj); 
    } else if (obj instanceof SubClass) { 
     implementationSpecificVariable = doForSub((SubClass) obj); 
    } 
} 
+2

如果你必须这样做,这表明你的设计可能有缺陷开始。你可以在'SuperClass'中没有方法并在'SubClass'中覆盖它吗? –

+1

为什么不简单在类SuperClass中实现该方法并在SubClass中重写它? – isnot2bad

回答

2

使用OOP!

class SuperClass { 
    public void doSomething() { /* implementation for SuperClass */ } 
} 

class SubClass extends SuperClass { 
    @Override 
    public void doSomething() { /* implementation for SubClass */ } 
} 

现在,只要你有一个参考任SuperClass或​​一个实例,要求它doSomething要么调用第一或第二的实现:

SuperClass obj = ...; // create instance of SuperClass or SubClass 
obj.doSomething(); 
0

你可以使用接口的概念或抽象类。一个在类中实现的接口可以让你完全不同的对待和实现相同的名字函数,我认为它适合你的情况。

搜索关于接口的Java和关于关于接口和抽象类的差别,他们将肯定非常方便,你有一天:)

0

你所描述被称为“Strategy Pattern”这基本上意味着你在运行时选择所需的行为。 Here是如何实现使用接口模式的例子:

public enum ShippingMethod { 
    FIRST_CLASS { 
     public double getShippingCost(double weightInPounds, double distanceInMiles) { 
      // Calculate the shipping cost based on USPS First class mail table 
     } 
    }, 
    FED_EX { 
     public double getShippingCost(double weightInPounds, double distanceInMiles) { 
      // Calculate the shipping cost based on FedEx shipping 
     }  
    }, 
    UPS { 
     public double getShippingCost(double weightInPounds, double distanceInMiles) { 
      // Calculate the shipping cost based on UPS table 
     }   
    }; 

    public abstract double getShippingCost(double weightInPounds, double distanceInMiles); 
}; 

public class SuperClass { 
    public ShippingMethod getShippingMethod() { 
    return ShippingMethod.FIRST_CLASS; 
    } 
} 

public class SubClass { 
    public ShippingMethod getShippingMethod() { 
    return ShippingMethod.FED_EX; 
    } 
} 

public class SubSubClass { 
    public ShippingMethod getShippingMethod() { 
    return ShippingMethod.UPS; 
    } 
} 

public class ShippingService { 
    public void shipOrder(SuperClass item) { 
    double shippingCost = item.getShippingMethod().getShippingCost(); 
    } 
    // or if you wanted to keep the service logic completely in the service, you could pass the service into the enum method call and have the strategy pick which method to execute 
    double shippingCost = item.getShippingMethod().getShippingCost(this); 
} 
1

Polymorphism是面向对象编程(OOP)的功能,它允许您为对象的各种儿童提供不同的实现。在编写代码时应尽可能多地使用avoid instance of and downcasting,而应该使用多态。

例如:

class Parent { 
    public void method() { 
     System.out.println("In the Parent"); 
    } 
} 

class ChildA extends Parent { 
    public void method() { 
     System.out.println("In Child A"); 
    } 
} 

class ChildB extends Parent { 
    //no method 
} 

class Test { 
    public static void main(String[] args) { 
     Parent parent = new Parent(); 
     Parent childA = new ChildA(); 
     Parent childB = new ChildB(); 
     ChildA anotherChildA = new ChildA(); 
     ChildB anotherChildB = new ChildB(); 

     parent.method(); //In the Parent 
     childA.method(); //In Child A 
     childB.method(); //In the Parent 
     anotherChildA.method(); //In Child A 
     anotherChildB.method(); //In the Parent 
    } 
} 

正如可以看到执行是由实例类型,而不是参考类型确定。因此,在您的示例中,您将在父级和每个孩子中实施doFor()方法。然后在父母的(或另一个班级)doSomething()中,您只需拨打this.doFor()而不是您的if/else语句。

相关问题