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

时间:2015-09-15 11:05:50

标签: 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 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。但是你可以建立通告。

欢迎任何tipp!

请问 弗兰克

4 个答案:

答案 0 :(得分:0)

根据您的示例,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;
  }
}

答案 1 :(得分:0)

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

其他情况是,如果您在子类中重写了该方法,那么您可以使用super.method()

但在这两种情况下重要的条件是你有适当的访问修饰符的功能。它不能是私人的。

答案 2 :(得分:0)

要调用Application类的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类中似乎更合适。

答案 3 :(得分:0)

谢谢你们! 为了增强方法readJob是清楚的。但正如我在初始职位上所说的那样。如果你这样做,你可以建立一个循环!

无法从父级调用方法!

Greetz Frank