传递子类 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);

}

}

谢谢您。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值