Let's consider two classes, super and sup extending super:
Class SuperClass{
int superDataMember;
}
Class SubClass extends SuperClass{
int subDataMember;
}
I need to have another CompositeClass class that may use super or sub composition depending on some criteria. My question is: Should I use
An instance of
SuperClassinsideCompositeClass- Thus, I can use either
SuperClassinstance orSubClassinstance. - But how will methods accessing
subDataMemberbehave if the accessed instance is ofSuperClass? Maybe an
instanceOfcheck may work at the beginning of each method?Class CompositeClass {
private SuperClass sc;
public getSuperDataMember () {// some code}
public getSubDataMember () {
if (this.sc instanceOf SubClass)
// some code
}
}
- Thus, I can use either
An instance of
SubClassinsideCompositeClassThus, I will lose the ability to access
SuperClassinstances!Class CompositeClass {
private SubClass sc;
public getSuperDataMember () {// some code}
public getSubDataMember () {// some code
}
}
Implement two versions of
CompositeClass, the first isSuperCompositeClasswith an instance ofSuperClassand the secondSubCompositeClassthat extends the first with an instance ofSubClass!
Aucun commentaire:
Enregistrer un commentaire