English 中文(简体)
2. 理解遗产和多变
原标题:understanding Inheritance and Polymorphism

My question is regarding Inheritance and Polymorphism.

I have a class A which is the super class. Classes B and C extends A and are subclasses.

Now i have a function in other class(not A or B or C) that takes argument of type A. So polymorphism allows us to pass the argument as A , B or C to that function. Now in that function, inorder to access B s properties, i have to typecast the received argument to B.

是否打造了良好的方案拟订技术? 难道这里使用多吗?

Thanks.

最佳回答

只有当你知道什么可以实现时,才会进行播种。

The use of polymorphism here is that the method in the other class can be written in a generic way, and a single piece of code can thus operate on different types, which, though, have some interface (class A) in common.

因此,......播种(取决于语言文化)是一种非常坏的做法。 采用多形态法(取决于......),例如采用B类的论据,采用不同的签字方法->B)

Update: added Java pseudocode

class A { int m() {} }
class B extends A { int b() {} }
class C extends A {}
class $ { 
  static void x(A $$) {} 
  static void x(B $$) {}
}
public class P {
  public static void main(String[] a) {
    $.x(new A()); //calls $::x(A)
    $.x(new B()); //calls $::x(B)
  }
}
问题回答

我假定你们想要的是这样的东西(旧法典):

void foo(A obj) {
    if (obj.isOfClass(B)) {
        // Access a property that B has, but A doesn t.
    }
}

In that case you need to typecast:

void foo(A obj) {
    if (obj.isOfClass(B)) {
        ((B)obj).propertyOfB();
    }
}

根据语言,有另一种选择。 以Ruby和Pin-C等语文,经常使用所谓的duck打字。 想法是:如果它像一个 d子和象一个uck子一样步行,它就必须是一个uck子。 换言之,这里重要的是ette/em>一个目标是什么,但该目标是否执行methods:

void foo(Object obj) {
    if (obj.hasMethod(quack)) {
        obj.quack();
    }
}

经常使用uck打字的语文通常更具有“动力”性质。 例如,在C++中,它具有非常严格的类型系统,几乎不可能做uck打,而前述的Rub和C目标则属于自然。





相关问题
Subclass check, is operator or enum check

A couple of friends was discussing the use of inheritance and how to check if a subclass is of a specific type and we decided to post it here on Stack. The debate was about if you should implement a ...

C++ Class Inheritance problem

Hi I have two classes, one called Instruction, one called LDI which inherits from instruction class. class Instruction{ protected: string name; int value; public: Instruction(string ...

Overloading a method in a subclass in C++

Suppose I have some code like this: class Base { public: virtual int Foo(int) = 0; }; class Derived : public Base { public: int Foo(int); virtual double Foo(double) = 0; }; ...

Embedding instead of inheritance in Go

What is your opinion of this design decision? What advantages does it have and what disadvantages? Links: Embedding description

Extending Flex FileReference class to contain another property

I want to extend the FileReference class of Flex to contain a custom property. I want to do this because AS3 doesn t let me pass arguments to functions through event listeners, which makes me feel sad,...

Interface Inheritance in C++

I have the following class structure: class InterfaceA { virtual void methodA =0; } class ClassA : public InterfaceA { void methodA(); } class InterfaceB : public InterfaceA { virtual ...

热门标签