java - One Class/Multiple Constructors vs. Multiple Classes (Inheritance) -
i'm trying implement following tree-like structure @ moment (i.e., man, woman, child, specialman, specialwoman, specialchild). there neater/alternate (less repeating code) way approach it?
public class person { int hat; int one_glove; } public class man extends person { int coat; int shorts; } public class woman extends person { int coat; } public class child extends person { int shorts; } public class specialman extends man { int second_glove; } public class specialwoman extends woman { int second_glove; } public class specialchild extends child { int second_glove; }
what i'm thinking having class person contain variables , having multiple constructors -> linked each specific object type?
public class person{ int hat; int one_glove; int coat; int shorts; int second_glove; public person(int coat;int shorts; int hat; int one_glove;){} //man public person(int coat;int hat; int one_glove;){} //woman public person(int coat;int shorts; int hat; int one_glove; int second_glove;) {} //specialman etc... }
simply inherit person class , use super
> public class person{ int hat; int one_glove; int coat; int shorts; int second_glove; public person(int a,int b,int c,int d,int e){ hat = a; one_glove = b; coat = c; shorts = d; second_glove = e; } } class man extends person(){ man(int a,int b,int c,int d,int e){ super(a,b,c,d,e); } }
Comments
Post a Comment