如何在java中从子级调用重写函数?
我想制作一个界面,从用户的角度来看,我希望它看起来干净,他们可以用这种语法编写代码。
public class child extends parent {
@Override
public void run() {
}
}
Main 位于父级中,但如何在父级中调用重写函数。 另外,我不希望“child”这个名字是强制性的,所以我不能直接调用它。
PS:这是我要重写的运行函数。
public class parent{
public static void main(String[] args) {
run();
}
}
I want to make an interface and from user point of view i want it to look clean and they can write their code with this syntax.
public class child extends parent {
@Override
public void run() {
}
}
Main is in the parent but how can one call a overriden function in parent.
Also i don't want the name "child" to be mandatory so i can't call it directly.
PS:this is the run function i want to override.
public class parent{
public static void main(String[] args) {
run();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
创建父级并运行
abstract
。抽象对于一个类来说意味着这个类不能直接实例化,但是如果有子类则可以实例化。方法的抽象意味着该方法在抽象类中定义,但不在抽象类中实现。相反,子类必须提供抽象方法的实现或声明自己是抽象的。Make Parent and run
abstract
. Abstract for a class means that this class cannot be directly instantiated, but can be instantiated if there is a subclass. Abstract for a method means that the method is defined in the abstract class, but is not implemented in the abstract class. Instead subclasses must provide an implementation of the abstract method or declare themselves to be abstract.调用所属类的重写函数使用..
super.run();
EX:
我从你那里得到的是..你想从父类调用子函数..
因为这就像普通的类函数一样,因为只有黑白关系父子到子,没有子子到父子
的关系
所以你只需创建子对象,然后调用子类的函数...
所以你的父类的主要内容可能是这样的..
to call overridden function of perent class use..
super.run();
EX:
what i get from you is ..u want to call child function from parent class..
For that is seams like a normal class function coz there is only relationship b/w parent-to-child no relation in child-to-parent
So u just make object of child and then call function of child class...
So your main of parent class may be look like this..
要在父用户中调用覆盖的函数,类似于以下之一:
To call an overwritten function in the parent user something like one of these:
您的意思是要调用父级中定义的 run() 函数吗?
Do you mean you want to call the run() function defined in the parent?
您可以使用“super”关键字调用超级(父)类方法。像这样。
<代码>
公共类子类扩展父类{
@覆盖
公共无效运行(){
超级.run();
}
}
You can call super(Parent) class methods using 'super' keyword. Like this.
public class child extends parent {
@Override
public void run() {
super.run();
}
}
我想您对 main() 函数和您所指的一些随机 Main 类感到困惑。一般来说,当涉及到继承时,您不能从基类调用派生类的重写函数。在这种情况下,您要做的就是创建一个基类指针并使其指向派生类对象。在这种情况下,当您使用基类指针调用函数时,将调用重写的派生类的函数。
我不确定我是否完全回答了你..
这可能对你有帮助.. -> http://www.oodesign.com/dependency-inversion-principle.html
I suppose you are confusing between main() function and some random Main class that you are referring to. In general when it comes to inheritance you cannot call a derived class's overridden function from a base class. In that case what you do is create a base class pointer and make it point to derived class object. In that case when you use the base class pointer to invoke the function the overriden derived classs' function gets called.
I am not sure if I answered you entirely..
This might help you.. -> http://www.oodesign.com/dependency-inversion-principle.html
不确定我理解是否正确。但父级无法知道它的受保护/公共方法是否、在哪里以及如何被重写,因此它无法调用被重写的实现。
如果您必须这样做,那么您的类层次结构设计可能是错误的。
Not sure if i understand correctly. But parent can't know if, where and how it's protected/public methods will be overriden so it cant call the overridden implementations.
If you have to do it, you probably got your class hierarchy design wrong.
你可能需要这样的东西。
You probably need something like this.
我不确定我是否正确回答了你的问题。据我了解,您正在尝试做一些事情,例如从父项中调用子项的运行。如果是这种情况,下面是执行此操作的代码片段。
你的孩子的实现如下所示。
希望这对您有帮助。
I am not sure whether I am answering your question right. From what I understood is you are trying to do some thing like calling child's run from Parent. If that is the case below is the code snippet to do it.
And your child implementation is like as shown below.
Hope this helps you.