繁体   English   中英

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

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

也许这是不可能的,但目前我不确定。

看一个简单的例子:

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;
  }
}

据我所知,我可以像 readJob(Application appl) 一样更改 readJob。 但是你可以建立循环。

欢迎任何提示!

亲切的问候弗兰克

根据您的示例,Job 类除了 Object 之外没有超类。 但是如果它确实有一个带有 setjobtype 方法的父类,你可以直接调用它。 例如:

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

在您的情况下 Application 不是父类。 为了调用应用程序的 setjobtype,您需要在 readJob 方法中创建应用程序类的实例,然后调用它。 或者为了为当前对象设置适当的值,您需要将它的一个实例传递给 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;
  }
}

从父类调用方法。 只需按原样编写该方法,因为子类包含父类的所有方法。

其他情况是,如果您在子类中覆盖了该方法,则可以使用super.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!
}

您可以调用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
}

这解决了您的问题,但我建议更改设计,因为setObjectType()方法在 Job 类中似乎更合适。

谢谢你们! 为了增强方法 readJob 是明确的。 但正如我在初始帖子中所说的那样。 如果你这样做,你可以建立一个循环!

不可能从父级调用方法!

格雷茨·弗兰克

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM