简体   繁体   English

Java:对象成员如何从父类调用方法?

[英]Java: How can a object member call a method from parent class?

maybe that isn't possible, but currently I'm not sure about it.也许这是不可能的,但目前我不确定。

Take a look at simple example:看一个简单的例子:

public class Job {
  private Document jobDoc;

  public JOB() {
    // some code to determine the job to process
    this.jobDoc = job_to_process;
  }

  public boolean readJob() {
    // do some validation stuff and read properties from Job
    String myproperty = this.job.getProperty("jobtype");

    // Here I need your advise
    // How to call a method from the parent class??
    parent.setjobtype(myproperty); // <= parent is just for Demonstration!
  }
}

public class Application {
  private Job myjob = null;
  private String jobtype = "";

  public Application() {
    this.myjob = new Job();
    // read the job to fill the application type
    myjob.readJob(); // <= this method should call setjobtype from this class!!
  }

  public void setjobtype(String type) {
   this.jobtype = type;
  }
}

From what I know I could change the readJob like readJob(Application appl).据我所知,我可以像 readJob(Application appl) 一样更改 readJob。 But then you are able to build circulars.但是你可以建立循环。

Any tipps are welcome!欢迎任何提示!

Kindly regards Frank亲切的问候弗兰克

As per you example the Job class dont have a super class other than Object.根据您的示例,Job 类除了 Object 之外没有超类。 But if it did had a parent class with setjobtype method you could directly invoke it.但是如果它确实有一个带有 setjobtype 方法的父类,你可以直接调用它。 eg:例如:

class ParentJob {
    String jobType;
    public void setjobtype(String jobType){
        this.jobType = jobType;
    }
}

class Job extends ParentJob{
  private Document jobDoc;

  public Job() {
    // some code to determine the job to process
    this.jobDoc = job_to_process;
  }

  public boolean readJob() {
    // do some validation stuff and read properties from Job
    String myproperty = this.job.getProperty("jobtype");

    setjobtype(myproperty); // This will call parent method
  }
}

In your case Application is not the parent class.在您的情况下 Application 不是父类。 In order to invoke the setjobtype of application you need to create an instance of application class in readJob method and then invoke it.为了调用应用程序的 setjobtype,您需要在 readJob 方法中创建应用程序类的实例,然后调用它。 Or in order to set proper values to current object you need to pass an instance of it to readJob method and then invoke the setjobtype method like或者为了为当前对象设置适当的值,您需要将它的一个实例传递给 readJob 方法,然后调用 setjobtype 方法,例如

class Job{
  private Document jobDoc;

  public Job() {
    // some code to determine the job to process
    this.jobDoc = job_to_process;
  }

  public boolean readJob(Application app) {
    // do some validation stuff and read properties from Job
    String myproperty = this.job.getProperty("jobtype");

    // Here I need your advise
    // How to call a method from the parent class??
    app.setjobtype(myproperty); // <= parent is just for Demonstration!
  }
}

public class Application {
  private Job myjob = null;
  private String jobtype = "";

  public Application() {
    this.myjob = new Job();
    // read the job to fill the application type
    myjob.readJob(this); // <= this method should call setjobtype from this class!!
  }

  public void setjobtype(String type) {
   this.jobtype = type;
  }
}

To call a method from parent class.从父类调用方法。 Simply write that method as it is because child class contains all method of parent classes.只需按原样编写该方法,因为子类包含父类的所有方法。

Other situation is if you have overridden that method in child class then you can use super.method()其他情况是,如果您在子类中覆盖了该方法,则可以使用super.method()

But condition important in the both cases is you have proper access modifier for the function.但在这两种情况下,重要的条件是您对该函数具有适当的访问修饰符。 It must not be private.它不能是私人的。

To call the setJobType() method of the Application class, you need a reference to an object of this class in the readJob() method.为了调用setJobType()的应用程序类的方法,你需要在这个类的一个对象的引用readJob()方法。

public boolean readJob(Application application) {
    // do some validation stuff and read properties from Job
    String myproperty = this.job.getProperty("jobtype");

    // Here I need your advise
    // How to call a method from the parent class??
    application.setjobtype(myproperty); // <= parent is just for Demonstration!
}

And you can call readJob() passing the reference to the application:您可以调用readJob()将引用传递给应用程序:

public Application() {
    this.myjob = new Job();
    // read the job to fill the application type
    myjob.readJob(this); // Passing the reference to the Application object
}

This solves your problem, but I would suggest to change the design as the setObjectType() method seems to be more appropriate in the Job class .这解决了您的问题,但我建议更改设计,因为setObjectType()方法在 Job 类中似乎更合适。

Thanks guys!谢谢你们! To anhance the method readJob was clear.为了增强方法 readJob 是明确的。 But as I said in the inital post.但正如我在初始帖子中所说的那样。 If you doing it this way you can build a Loop!如果你这样做,你可以建立一个循环!

It is not possible to call a method from the parent!不可能从父级调用方法!

Greetz Frank格雷茨·弗兰克

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 一个Java线程object怎么能从原来的class调用一个方法? - How can a Java thread object call a method from the original class? 父类对实现类的对象的java调用方法 - java call method from parent on object from implemented class 如何在JAVA中从父类的内部类中调用子类中的重写方法? - How can I call the overridden method in a child class from a parent class's inner class in JAVA? 如何在Java中使用Child对象调用Parent类的方法 - How to call method of Parent class using Child object in Java 子类对象如何通过Java中的extend关键字访问父类方法或数据成员 - How child class object accesses parent class method or Data Member via extends keyword in Java Java:如何从类中创建的对象调用类方法? - Java: How to call a class method from an object created in the class? 调用子类的父类方法 class object java - call parent class's method on a child class object java 有没有办法从java中的子类对象调用父类方法而不修改方法 - is there any way to call parent class method from child class object in java without modifying methods 属性类如何在Java中为其“父”类调用方法? - How can a property class can call a method for its 'parent' class in Java? 父类可以在Java中调用动态创建的子类方法吗? - Can Parent Class call Dynamically created child class method in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM