是使用父类对象进行子类方法调用的可能性 [英] Is a Possibility for Child class methods calls with parent class object
本文介绍了是使用父类对象进行子类方法调用的可能性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
早上好,
子类方法可能与父类对象一起调用吗?
我可以实例化父类,也想检索Child类的方法.
有可能吗?
比Q
Hi morning,
Is a Possibility for Child class methods calls with parent class object?
I can instantiate parent class and also i want to retrieve Child class methods..
is it possible?
Than Q
推荐答案
这里有问题.在第一个中,尚不清楚它是什么意思: with 父类对象.中是模棱两可的短语.您是指父"类对象的参数,还是什么?通常,派生类的对象(子"和父"类)不是很精确的术语,可能意味着基类"和派生"类)可以完成基类对象的所有工作.
第二个问题则相反.如果您实例化了基类的对象,则它与其派生类无关.此外,术语检索"方法是没有意义的.反射中有类似的内容,但我不认为这与您要询问的内容有关.
如果您还有其他意思,请解释.这个问题充满不确定性.
—SA
There are questions here. In a first one, it''s not clear what does it mean: with parent class object. In is ambiguous phrase. Do you mean with parameter of a "parent" class object, or what? Very generally, and object of a derived class ("child" and "parent" are not quite precise terms, probably you mean "base" and "derived" classes) can do everything an object of a base class do.
The second question is the opposite. If you instantiated an object of a base class, it does not no anything about its derived class. Besides, the term "retrieve" a method makes no sense. There is something similar in Reflection, but I don''t thin this is related to what you meant to ask about.
If you mean something else, please explain. The question is full of uncertainties.
—SA
您认为战略模式"吗?好吧,您不会直接调用派生类的方法,而是会调用它们.
想象一个像这样的函数:
Do you think of "strategy pattern"? Well, you do not call the methods of the derived class directly - but you do call them.
Imagine a function like:
public virtual void DoSomething()
{
DoStep1();
DoStep2();
}
protected abstract void DoStep1();
protected virtual void DoStep2()
{
//some code
}
调用DoSomething时,将调用派生类的DoStep1方法以及可能的DoStep2方法.
这些是C#签名,但是这些概念在Java中也可用. [/Edit]
When DoSomething is called, the DoStep1 method, and possibly the DoStep2 method, of the derived class are being called.
Those are C# signatures, but the concepts are also available in Java. [/Edit]
这篇关于是使用父类对象进行子类方法调用的可能性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文