[英]Passing an object of the current class to another class
好吧,我不確定這是否是一個愚蠢的問題。
我試圖確保我在代碼中使用盡可能少的靜態方法,直到它完全混亂為止,我想創建盡可能好的OO方法。
創建當前所在類的對象然后傳遞值是否被認為是不好的做法 ?
可能嗎?
例如..
public class Details{
int age;
String name;
String countryOfBirth;
public Details(int age, String name, String countryOfBith){
this.age = age;
this.name = name
this.countryOfBirth = countryOfBirth;
}
// how would I use these values to then pass them to another class as an object?
Candidate can = new Candidate(Details
}
我需要知道這一點的原因是因為我希望在該特定類中使用與該類中的值相對應的方法。 如果您需要知道的話,我正在使用信號燈
例如,我想傳遞對象,但是我只想要一個特定的值。
這樣做是可以的,並且最常見的是,在控制台應用程序中,包含main
方法的類的實例通常是在main
方法本身中創建的,用於做某件事或另一件事。 以下說明了這一點:
public class Hello {
public static void main (String args[]) {
Hello hello = new Hello();
hello.say();
}
public void say() {
System.out.println ("Hello World!");
}
}
使用您在上下文中提供的示例,可以編寫
Candidate can = new Candidate(this);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.