Design Patterns: What is a Type

A very simple question, but after reading Design Designs: Elements of reusable OO Software, I'm a little confused.

The book states

"The type of an object refers only to its interface - a set of requests to which it can respond. An object can have many types and objects of different classes can have the same type."

Can someone better explain what a Type is? I also do not understand how one object can have several types ... if the book does not talk about polymorphism ....

+3
source share
3 answers

Maybe an example helps clarify. Suppose a class is a special type of type.

  class Dog : Animal {...};
  class Fish : Animal {...};
  Dog lassie = new Dog();  
        // An object can have many types: 
        // lassie is a dog and an Animal

  Fish nemo = new Fish();  
    // objects of different classes can have the same type: 
    // lassie and nemo are animals
+1
source

.

, . .

, Java ( ), ( /).

, .

"" ( ) - , square, rectangle, circle .. "", setPosition(), setSize() drawShape().

+1

. , , .

, , (). , . , , , .

IEntity, IMammal ISwimmer , , IEntity ISwimmer. - , , , , .

,

0

Source: https://habr.com/ru/post/1782399/


All Articles