Public abstract class Profile <T extends ProfileOpt> {
..
}
Class A extends Profile<class C> {
..
}
Class B extends Profile<class D> {
..
}
Class D extends class C {
..
}
I have a method which has common code both in
class A
update(String xyz, C c) {
}
Class B
Update (String abc, D d) {
}
我理解在A类和B类中编写更新方法是一个坏主意,我需要重用代码。
我能想到的一种方法是仅使用此方法创建一个单独的Update类,然后实例化Update和A和B,并从具有相应参数的两个类进行方法调用。
有没有更好的方法,或者这从OOPS的角度来看是好的?
看起来你有重载版本的update
方法接受不同类型的参数。在这种情况下,我建议如下:
Profile
类中创建一个方法(因为它是A和B的父类),称之为update(String arg)
update
方法首先调用update(str)
并编写自定义代码以处理相应方法中的特定类型