如何在java中从子级调用重写函数?

发布于 2024-12-16 23:07:31 字数 354 浏览 0 评论 0原文

我想制作一个界面,从用户的角度来看,我希望它看起来干净,他们可以用这种语法编写代码。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

烟花肆意 2024-12-23 23:07:31

创建父级并运行abstract。抽象对于一个类来说意味着这个类不能直接实例化,但是如果有子类则可以实例化。方法的抽象意味着该方法在抽象类中定义,但不在抽象类中实现。相反,子类必须提供抽象方法的实现或声明自己是抽象的。

public abstract class Super {

    public static void main(String[] args) {
        Super s = new Sub();
        s.main(); 
    }

    public abstract void run();

    public void main() {
        System.out.println("Calling sub class's implementation of run");
        // The super class does not know the implementation of run
        // but it does know that there must be an implementation to use.
        run();
        System.out.println("Done!");
    }

}

class Sub extends Super {

    @Override
    public void run() {
        System.out.println("sub class implementation of run");
    }

}

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.

public abstract class Super {

    public static void main(String[] args) {
        Super s = new Sub();
        s.main(); 
    }

    public abstract void run();

    public void main() {
        System.out.println("Calling sub class's implementation of run");
        // The super class does not know the implementation of run
        // but it does know that there must be an implementation to use.
        run();
        System.out.println("Done!");
    }

}

class Sub extends Super {

    @Override
    public void run() {
        System.out.println("sub class implementation of run");
    }

}
缱绻入梦 2024-12-23 23:07:31

调用所属类的重写函数使用..
super.run();
EX:

public class child extends parent {   
  @Override
  public void run() {  
  super.run(); 
   }
  }

我从你那里得到的是..你想从父类调用子函数..
因为这就像普通的类函数一样,因为只有黑白关系父子到子,没有子子到父子
的关系
所以你只需创建子对象,然后调用子类的函数...
所以你的父类的主要内容可能是这样的..

          public static void main(String[] args) {
          new child().run();            
    }

to call overridden function of perent class use..
super.run();
EX:

public class child extends parent {   
  @Override
  public void run() {  
  super.run(); 
   }
  }

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..

          public static void main(String[] args) {
          new child().run();            
    }
花辞树 2024-12-23 23:07:31

要在父用户中调用覆盖的函数,类似于以下之一:

super.run();

((parent) this).run();

To call an overwritten function in the parent user something like one of these:

super.run();

((parent) this).run();
ㄟ。诗瑗 2024-12-23 23:07:31

您的意思是要调用父级中定义的 run() 函数吗?

public class child extends parent 
{
  @Override
  public void run() 
  {
    super.run();    // call parent's run() function
    this.doStuff(); // call child's additional functionality
  }
 }

Do you mean you want to call the run() function defined in the parent?

public class child extends parent 
{
  @Override
  public void run() 
  {
    super.run();    // call parent's run() function
    this.doStuff(); // call child's additional functionality
  }
 }
独享拥抱 2024-12-23 23:07:31

您可以使用“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();
}
}

北城孤痞 2024-12-23 23:07:31

我想您对 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

同展鸳鸯锦 2024-12-23 23:07:31

不确定我理解是否正确。但父级无法知道它的受保护/公共方法是否、在哪里以及如何被重写,因此它无法调用被重写的实现。

如果您必须这样做,那么您的类层次结构设计可能是错误的。

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.

寻找我们的幸福 2024-12-23 23:07:31

你可能需要这样的东西。

public Parent
{
  public final void run()
  {
    this.runChild
  }

  protected abstract void runChild();
}

public class Child extends Parent
{
  public static void main(String[] args)
  {
    run();
  }

  protected void runChild()
  {
    ....
  }
}

You probably need something like this.

public Parent
{
  public final void run()
  {
    this.runChild
  }

  protected abstract void runChild();
}

public class Child extends Parent
{
  public static void main(String[] args)
  {
    run();
  }

  protected void runChild()
  {
    ....
  }
}
流心雨 2024-12-23 23:07:31

我不确定我是否正确回答了你的问题。据我了解,您正在尝试做一些事情,例如从父项中调用子项的运行。如果是这种情况,下面是执行此操作的代码片段。

 public abstract class Parent{

    public abstract void run();

    public void mainMethod(){
      //This automatically calls run from child
       run();
    }
  }

你的孩子的实现如下所示。

public class child extends parent {
  @Override
  public void run() {

   //Do the stuff you want to

   }
 }



public class MainClass{

    public static void main(String args[]){
       Parent obj = new Child();
       // This inturn takes care of the rest.
       obj.mainMethod();
       //Some other child
       obj = new Child2();
       obj.mainMethod();
    }

}

希望这对您有帮助。

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.

 public abstract class Parent{

    public abstract void run();

    public void mainMethod(){
      //This automatically calls run from child
       run();
    }
  }

And your child implementation is like as shown below.

public class child extends parent {
  @Override
  public void run() {

   //Do the stuff you want to

   }
 }



public class MainClass{

    public static void main(String args[]){
       Parent obj = new Child();
       // This inturn takes care of the rest.
       obj.mainMethod();
       //Some other child
       obj = new Child2();
       obj.mainMethod();
    }

}

Hope this helps you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文