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

Java:对象成员如何从父类调用方法?,java,methods,Java,Methods,也许这是不可能的,但目前我不确定 请看一个简单的示例: 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

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

请看一个简单的示例:

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;
  }
}
公共类作业{
私人文件;
公职(){
//确定要处理的作业的一些代码
this.jobDoc=作业到作业流程;
}
公共布尔readJob(){
//做一些验证工作并从作业中读取属性
字符串myproperty=this.job.getProperty(“jobtype”);
//我需要你的建议
//如何从父类调用方法??

parent.setjobtype(myproperty);//根据您的示例,作业类没有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
  }
}
在您的情况下,应用程序不是父类。为了调用应用程序的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;
  }
}
课堂作业{
私人文件;
公职(){
//确定要处理的作业的一些代码
this.jobDoc=作业到作业流程;
}
公共布尔读取作业(应用程序应用程序){
//做一些验证工作并从作业中读取属性
字符串myproperty=this.job.getProperty(“jobtype”);
//我需要你的建议
//如何从父类调用方法??

app.setjobtype(myproperty);//从父类调用方法。只需按原样编写该方法,因为子类包含父类的所有方法

另一种情况是,如果您在子类中重写了该方法,则可以使用
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!
}
这解决了您的问题,但我建议更改设计,因为
setObjectType()
方法似乎更适合于Job类。

谢谢大家! 对anhance来说,readJob方法很清楚。但是正如我在初始帖子中所说的。如果你这样做,你可以构建一个循环

无法从父级调用方法


Greetz Frank

尝试使用super关键字?您可以尝试使用此关键字吗?您的示例“Job”类没有父类(对象除外)
public Application() {
    this.myjob = new Job();
    // read the job to fill the application type
    myjob.readJob(this); // Passing the reference to the Application object
}