传递子类 java_Java,将主类传递给子类,错误的编码风格?
請你容忍我,因為我仍然在抓捕爪哇人。
下面的示例從一個名為
Parent
在
main
方法。然后它會使該實例進行各種計算。
接下來它會觸發一個名為
Child
通過
起源
引用實例
兒童
.
這個
兒童
只是坐在那里監視事情,有時,會觸發公共方法
起源
.
它起作用了。問題是,
這種款式不好嗎
? 還有別的嗎
Java思維方式
做這種工作?
public class Parent {
// main function that fires up the program
public static void main() {
// creates an instance of himself
// and fires go that does all sorts of fuzzy calculus
Parent parent = new Parent();
parent.go();
// creates a new thread of child and starts it
Child child = new Child(parent);
child.start();
}
private void go() {
// all sort of initializations
}
public void getDataFromChild(int data) {
// will get data from running child thread
}
}
public class Child extends Thread {
private Parent parent;
// child constructor grabs Parent instance into "o"
public Child(Parent o) {
parent = o;
}
// this class main loop
public void run() {
while(1==1) {
doSomething();
try {
sleep(1000);
}
catch(Exception e) { }
}
}
private void doSomething() {
parent.getDataFromChild(1);
}
}
謝謝您。
總結
以上是生活随笔為你收集整理的传递子类 java_Java,将主类传递给子类,错误的编码风格?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 员工股票期权行权是什么意思
- 下一篇: java中channelmessage,