传递子类 java_Java,将主类传递给子类,错误的编码风格?
請(qǐng)你容忍我,因?yàn)槲胰匀辉谧ゲ蹲ν廴恕?/p>
下面的示例從一個(gè)名為
Parent
在
main
方法。然后它會(huì)使該實(shí)例進(jìn)行各種計(jì)算。
接下來(lái)它會(huì)觸發(fā)一個(gè)名為
Child
通過(guò)
起源
引用實(shí)例
兒童
.
這個(gè)
兒童
只是坐在那里監(jiān)視事情,有時(shí),會(huì)觸發(fā)公共方法
起源
.
它起作用了。問(wèn)題是,
這種款式不好嗎
? 還有別的嗎
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);
}
}
謝謝您。
總結(jié)
以上是生活随笔為你收集整理的传递子类 java_Java,将主类传递给子类,错误的编码风格?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 员工股票期权行权是什么意思
- 下一篇: java中channelmessage,