![](/img/trans.png)
[英]How can a Java thread object call a method from the original class?
[英]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.