在Java中使用继承避免数据类的动态类型转换
我有3个数据类在Java中使用继承避免数据类的动态类型转换,java,inheritance,casting,code-duplication,Java,Inheritance,Casting,Code Duplication,我有3个数据类 @Data class A { private int a; } @Data class B extends A { private int b; } @Data class C extends A { private int c; } 类B和C之间有一些公共字段,这些字段保存在其父类A中。 下面是tester类 class TesterClass { static String bOrC = "C"; // input from some
@Data
class A
{
private int a;
}
@Data
class B extends A
{
private int b;
}
@Data
class C extends A
{
private int c;
}
类B和C之间有一些公共字段,这些字段保存在其父类A中。
下面是tester类
class TesterClass
{
static String bOrC = "C"; // input from some decision
public static void main(String[] args) // assume this to be the client
{
A a;
if (bOrC.equals("B")) {
B b = new B();
b.setB(11);
a = b;
} else {
C c = new C();
c.setC(12);
a = c;
}
a.setA(10);
doSomething(bOrC, a);
}
// Below are the service methods
// only this method in the service exposed
public static void doSomething(String bOrC, A a) {
if (bOrC.equals("B")) {
doSomethingWithB(a);
} else if (bOrC.equals("C")) {
doSomethingWithC(a);
}
}
public static void doSomethingWithB(A a) {
B b = (B) a; // possible ClassCastException
System.out.println(b.getA());
System.out.println(b.getB());
}
public static void doSomethingWithC(A a) {
C c = (C) a; // possible ClassCastException
System.out.println(c.getA());
System.out.println(c.getC());
}
}
现在我看到的问题是不安全的动态类型转换,它可能会遇到类转换问题。一种可能的解决方案是创建单独的数据对象,并在B类和C类中分别为这两个对象设置公共字段(对于我的实际情况来说太多了),然后如下所示:
public class TesterClass
{
static String bOrC = "C"; // input from some decision
public static void main(String[] args)
{
if (bOrC.equals("B")) {
B b = new B();
b.setA(10); // duplication
b.setB(11);
doSomethingWithB(b);
} else {
C c = new C();
c.setA(10); // duplication
c.setC(12);
doSomethingWithC(c);
}
}
public static void doSomethingWithB(B b) {
System.out.println(b.getA());
System.out.println(b.getB());
}
public static void doSomethingWithC(C c) {
System.out.println(c.getA());
System.out.println(c.getC());
}
}
我正在寻找一种方法来避免这种动态类型转换,但同时避免重复公共变量。有人能提出解决方案吗?对于您正在解释的行为,抽象是一种解决方案。在类A中创建抽象方法doSomething(…),并分别在子类B和C中实现它。通过这样做,您不需要使用静态方法,处理将基于B或C对象本身的实例来完成
@Data
class A
{
private int a;
public abstract void doSomething();
}
@Data
class B extends A
{
private int b;
public void doSomething(){
/*.... do something here
* here you can also access parent public methods and properties.
* as you have already annotated with @Data you will have access to getA() method, * hence you can also use parent properties.
*/
}
}
@Data
class C extends A
{
private int c;
public void doSomething(){
/*.... do something here
* here you can also access parent public methods and properties.
* as you have already annotated with @Data you will have access to
* getA() method, * hence you can also use parent properties.
*/
}
现在您可以按如下方式使用它
public static void main(Strings[] args){
A a;
B b = new B();
b.setB(10);
b.doSomething();
C c = new C();
c.setC(30);
c.doSomething();
}
你能不能把
bb
传给doSomethingWithB
和cc
传给doSomethingWithC
?我很抱歉给你弄糊涂了。我对代码进行了一些编辑。你能再检查一遍吗?嗨,尼米什,你能根据我的例子提供一个示例代码片段吗?那将非常有帮助