Inheritance and polymorphism - ease of use and purity

In the project, our team uses lists of objects to perform bulk operations on data sets, which all must be processed in a similar way. In particular, different objects would ideally act in the same way, which would be very easily achieved with polymorphism. The problem I am facing is that inheritance implies that it is a relationship, not a relationship. For example, several objects have a damage counter, but to make it easy to use in the list of objects, you can use polymorphism, except that it is understood that this is a relation that will not be true. (A person is not a damage counter.)

The only solution I can think of is to have a member of the class returning the correct type of the object when it is implicitly cast instead of relying on inheritance. Would it be better to discard that a / has an ideal in exchange for ease of programming?

Edit: To be more specific, I use C ++, so using polymorphism will allow different objects to "act the same" in the sense that derived classes can be in the same list and controlled by a virtual function of the base class. Using an interface (or simulating them through inheritance) is similar to the solution I would like to use.

+5
source share
10

. (++) . /. :

class Damage {
    virtual void addDamage(int d) = 0;
    virtual int getDamage() = 0;
};

class Person : public virtual Damage {
    void addDamage(int d) {
        // ...
        damage += d * 2;
    }

    int getDamage() {
        return damage;
    }
};

class Car : public virtual Damage {
    void addDamage(int d) {
        // ...
        damage += d;
    }

    int getDamage() {
        return damage;
    }
};

Person, Car 'is-a' Damage, Damage. (, ) . . .

+3

, , ( #):

public interface IDamageable
{
    void AddDamage(int i);
    int DamageCount {get;}
}

:

public class Person : IDamageable

public class House : IDamageable

, DamageCount , , , - .

+5

, , :

class IDamageable {
  virtual DamageCounter* damage_counter() = 0;
};
class DamageCounter {
  ...
};

, , functions_counter(). , vtable , . :

class Damageable {
 public:
  DamageCounter damage_counter() { return damage_counter_; }
 private:
  DamageCounter damage_counter_;
};

Cool , -.

+1

. " " , . , , , , , , . , .

, Damageable, DamageCounter. , , . ( , , .) Damageable, , ( ).

( ++ ), mixins, , .

0

, ',' vs '', Inheritance vs Composition.

... " - " .

:

http://www.artima.com/designtechniques/compoinh.html

.

@ . , , , , .

0

:/

, , " ", , . ;

, .

? ? ? ?

, ,

" " . ?

0

@Kevin

, ',' vs '', Inheritance vs Composition.

... " - " .

. , , vector<Person|Car> vector<with::getDamage()> - . Object, , getDamage() .

, . " is-a has-a , , ?"

0

" " , , -, , , .

, . "" , . . , , , , , "" ...

0

@

" " . ?

, , . addDamage(). - :

foreach (obj in mylist)
    obj.addDamage(1)

, , ( ). :.

class Person : DamageCounter {}
class Car : DamageCounter {}

foreach (DamageCounter d in mylist)
    d.addDamage(1)

Person Car .

0

. - , , ().

0

All Articles