Classes and Inheritance
in
Programming Questions
•
3 years ago
/*
I did my best to provide pseudo code to describe my question.
Class spoon exists just so that I can have smoogle and smo reside in the same array and be
different types of objects. I have a place holder function class.get_force() so that when I add smoogle and
smo to sum I can call get_force() on both objects. It calls the over riden version of the method( which I didn't
expect it would do!)
The whole point of this is that though smoogle and smo have the exact same methods they differ in how they
arive at their output. I need to add these objects values together and want I class that handles all of that in the
background.
different types of objects. I have a place holder function class.get_force() so that when I add smoogle and
smo to sum I can call get_force() on both objects. It calls the over riden version of the method( which I didn't
expect it would do!)
The whole point of this is that though smoogle and smo have the exact same methods they differ in how they
arive at their output. I need to add these objects values together and want I class that handles all of that in the
background.
This is a very important part of what I am working on and would love opinions it's no fun to program on an island.
Does any one have any alternative ideas to how I should handle this?
Is this the best way to go about things?
Is there a more correct way to do this?
I was very surprised that this example calls the overridden method and not the original place holder in spoon.
Does any one have any alternative ideas to how I should handle this?
Is this the best way to go about things?
Is there a more correct way to do this?
I was very surprised that this example calls the overridden method and not the original place holder in spoon.
I expected since the array test was of the type spoon it would call the method held in spoon and not the actual objects class.
*/
- class spoon{
- spoon(){
- }
- int get_force(){
- return 0;
- }
- }
- class smoogle extends spoon{
- int force;
- void set_force(int force){
- this.force = force;
- }
- int get_force(){
- return force;
- }
- }
- class smo extends spoon{
- int force;
- void set_force(int force){
- this.force = force*force;
- }
- int get_force(){
- return force;
- }
- }
- smoogle smoogle = new smoogle();
- smo smo = new smo();
- class sum{
- int num;
- spoon[] test = new spoon[0];
- sum(){
- }
- void add_to_system(spoon smo){
- test=(spoon[])append(test,smo);
- num++;
- }
- int print_sum(){
- int smo=0;
- for( int x=0; x < test.length;x++){
- smo += test[x].get_force() ;
- }
- return smo;
- }
- }
- sum sum = new sum();
- sum.add_to_system(smoogle);
- sum.add_to_system(smo);
- smoogle.set_force(4);
- smo.set_force(4);
- println(sum.print_sum());
- /*
- Originally I used the class below to handle the objects as totally seperate objects.
- class sum{
- int num_smo;
- int num_smoogle;
- smo[] smo = new smo[0];
- smoogle [] smoggle = new smoogle[0];
- sum(){
- }
- void add_smo_to_system(spoon new_smo){
- smo =(smo[])append(smo, new_smo);
- num_smo++;
- }
- void add_smoogle_to_system(spoon new_smoogle){
- smoogle =(smoogle[])append(smoogle, new_smoogle);
- num_smoogle++;
- }
- int print_sum(){
- int total=0;
- for( int x=0; x < smo.length;x++){
- total += smo[x].get_force() ;
- }
- for( int x=0; x < smo.length;x++){
- total += smoogle[x].get_force() ;
- }
- return total;
- }
- }
- */
1