如何在不重复代码的情况下子类化它

发布于 2024-11-20 00:08:39 字数 710 浏览 3 评论 0原文

我有类 Dad 和子类 Son。我想创建一个 Dad 的子类和一个覆盖 Dad 方法的 Son 子类。

在不重复代码的情况下执行此操作的最佳方法是什么?我无法修改 DadSon

鉴于...

public class Dad {

public void doSomething() {}

}

public class Son extends Dad {

}

...我想创建...

public class DadSubclass extends Dad {

@Overrides
public void doSomething() {        
    // My code 
}

}

public class SonSubclass extends Son {

@Overrides
public void doSomething() { 
    // My code 
}

}

...而不重复 // 我的代码

明显的解决方案是创建一个辅助类并为两者调用它,但是如果我想调用受保护的方法,这是有问题的,并且不允许我使用相同的包创建子类。

有更好的解决方案吗?

I have class Dad with subclass Son. I'd like to create a subclass of Dad and a subclass of Son that overrides a method of Dad.

What would be the best way of doing this without repeating code? I can not modify Dad and Son.

Given...

public class Dad {

public void doSomething() {}

}

public class Son extends Dad {

}

...I'd like to create...

public class DadSubclass extends Dad {

@Overrides
public void doSomething() {        
    // My code 
}

}

public class SonSubclass extends Son {

@Overrides
public void doSomething() { 
    // My code 
}

}

...without repeating // My code.

The obvious solution would be to create a helper class and call it for both, but this is problematic if I want to call protected methods, and I'm not allowed to create the subclasses with the same package.

Is there a better solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沩ん囻菔务 2024-11-27 00:08:39

创建一个公共帮助器类并调用它。

Create a common helper class and call it.

傲性难收 2024-11-27 00:08:39

假设您的代码没有访问成员变量,我只需将此代码放入静态实用程序类中。如果不是这种情况,您仍然可以通过传入一个公共超类 - 'Dad' public static void mycode(Dad d) 来完成此操作。如果您需要子类本身的特定变量,我会重新考虑您的类结构。

Assuming your code isn't accessing member variables, I would just put this code in a static utility class. If this isn't the case, you can still do this by passing in a common superclass - that of 'Dad' public static void mycode(Dad d). If you need specific variables in the subclasses themselves, I would rethink your class structure.

浪荡不羁 2024-11-27 00:08:39

这里你真正想要的是这样的:

class DadSonSubclass extends Dad, Son {
    public void doSomething() {
        //mycode
    }
}

这是多重继承,Java 不支持这种继承。因此,您唯一的选择是创建一个助手/实用程序类,这是完全可以接受的。如果您需要调用受保护的方法,只需将 Dad 对象传递给帮助器类并创建公共回调方法来访问此信息。

What you really want here is something like this:

class DadSonSubclass extends Dad, Son {
    public void doSomething() {
        //mycode
    }
}

This is multiple inheritance, which is not supported by Java. So your only option would be to create a helper/utility class, which is perfectly acceptable. If you need to call protected methods, just pass the Dad object in to the helper class and create public callback methods to access this info.

梦回梦里 2024-11-27 00:08:39

也许更好,也许不是,这取决于你的观点,但它肯定可以做到。将您的代码放入帮助程序类中,并使用回调让该帮助程序访问它所需的受保护方法:

interface Callback {
    void foo();
    void bar();
    void one();
    void two();
}

class Helper {
    static void helpMe(Callback callback) {
        // My code
    }
}

class DadSubclass extends Dad {
    @Override
    public void doSomething() {
        Helper.helpMe(new Callback() {
            public void foo() {
                DadSubclass.this.foo();
            }

            public void bar() {
                DadSubclass.this.bar();
            }

            public void one() {
                throw new UnsupportedOperationException("one() doesn't exist in Dad");
            }

            public void two() {
                throw new UnsupportedOperationException("two() doesn't exist in Dad");
            }
        });
    }
}

class SonSubclass extends Son {
    @Override
    public void doSomething() {
        Helper.helpMe(new Callback() {
            public void foo() {
                SonSubclass.this.foo();
            }

            public void bar() {
                SonSubclass.this.bar();
            }

            public void one() {
                SonSubclass.this.one();
            }

            public void two() {
                SonSubclass.this.two();
            }
        });
    }
}

Maybe better, maybe not, depending on your point of view, but it can certainly be done. Put your code into a helper class, and use a callback to give that helper access to the protected methods it needs:

interface Callback {
    void foo();
    void bar();
    void one();
    void two();
}

class Helper {
    static void helpMe(Callback callback) {
        // My code
    }
}

class DadSubclass extends Dad {
    @Override
    public void doSomething() {
        Helper.helpMe(new Callback() {
            public void foo() {
                DadSubclass.this.foo();
            }

            public void bar() {
                DadSubclass.this.bar();
            }

            public void one() {
                throw new UnsupportedOperationException("one() doesn't exist in Dad");
            }

            public void two() {
                throw new UnsupportedOperationException("two() doesn't exist in Dad");
            }
        });
    }
}

class SonSubclass extends Son {
    @Override
    public void doSomething() {
        Helper.helpMe(new Callback() {
            public void foo() {
                SonSubclass.this.foo();
            }

            public void bar() {
                SonSubclass.this.bar();
            }

            public void one() {
                SonSubclass.this.one();
            }

            public void two() {
                SonSubclass.this.two();
            }
        });
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文