package zhidao;
public abstract class Sensor extends Thread
{
public static void main ( String[] args )
{
Sensor sensor = new Sensor (""){};
DaySensor daySensor = sensor.new DaySensor ("daySensor");
TemperatureSensor tSensor = sensor.new TemperatureSensor ("tSensor");
LocationSensor locationSensor = sensor.new LocationSensor ("locationSensor");
HRBPSensor hSensor = sensor.new HRBPSensor ("hSensor");
WaterSensor waterSensor = sensor.new WaterSensor ("waterSensor");
daySensor.start ();
tSensor.start ();
locationSensor.start ();
hSensor.start ();
waterSensor.start ();
}
// 构造函数
protected Sensor ( String dataSrc )
{
this.setName (dataSrc);
this.setPriority (Thread.MIN_PRIORITY);
}
public void run ()
{
Thread thread = null;
try
{
while (null != ( thread = Thread.currentThread () ))
{
String name = thread.getName ();
if ("daySensor".equals (name))
{
thread.interrupt ();
thread = null;
System.out.println ("I am daySensor");
}
else if ("tSensor".equals (name))
{
thread.interrupt ();
thread = null;
System.out.println ("I am tSensor");
}
else if ("locationSensor".equals (name))
{
thread.interrupt ();
thread = null;
System.out.println ("I am locationSensor");
}
else if ("hSensor".equals (name))
{
thread.interrupt ();
thread = null;
System.out.println ("I am hSensor");
}
else if ("waterSensor".equals (name))
{
thread.interrupt ();
thread = null;
System.out.println ("I am waterSensor");
}
Thread.sleep (2000);
}
}
catch (InterruptedException e)
{}
}
private void publishData ()
{}
// 下面是5个sensor
public class DaySensor extends Sensor
{
protected DaySensor ( String dataSrc )
{
super (dataSrc);
}
}
public class TemperatureSensor extends Sensor
{
// 重写 publishData()
private void publishData ()
{}
protected TemperatureSensor ( String dataSrc )
{
super (dataSrc);
}
}
public class LocationSensor extends Sensor
{
protected LocationSensor ( String dataSrc )
{
super (dataSrc);
}
}
public class HRBPSensor extends Sensor
{
protected HRBPSensor ( String dataSrc )
{
super (dataSrc);
}
}
public class WaterSensor extends Sensor
{
protected WaterSensor ( String dataSrc )
{
super (dataSrc);
}
}
}
public abstract class Sensor extends Thread{
public static void main( String[] args ) {
//需要在main函数里面初始化5个sensor, 并且分别建立5个线程
//Sensor必须是抽象类,建立5个sensor的时候编译器说需要一个
sensor的对象,可是sensor是抽象类不能建立对象..
Sensor s1=new DaySensor("DaySensor");
Sensor s2=new TemperatureSensor("TemperatureSensor");
Sensor s3=new LocationSensor("LocationSensor");
Sensor s4=new HRBPSensor("HRBPSensor");
Sensor s5=new DaySensor("WaterSensor");
s1.start();
s2.start();
s3.start();
s4.start();
s5.start();
/*解释:你的五个子类是父类的内部类,而主方法main是static静 态的,你new一个动态的内部类是不行的,就像方法调用 ,静态的方法不能调用动态的方法*/
/*方案:(1)将所有的内部类都用static修饰;(2)将五个子类定义 在Sensor类的外面或者单独用其他的文件定义*/
}
//构造函数
protected Sensor( String dataSrc ){}
public void run(){}
private void publishData(){}
// 下面是5个sensor
public static class DaySensor extends Sensor{
protected DaySensor( String dataSrc ) {
super( dataSrc );
}
}
public static class TemperatureSensor extends Sensor{
//重写 publishData()
private void publishData(){}
protected TemperatureSensor( String dataSrc ) {
super( dataSrc );
}
}
public static class LocationSensor extends Sensor{
protected LocationSensor( String dataSrc ) {
super( dataSrc );
}
}
public static class HRBPSensor extends Sensor{
protected HRBPSensor( String dataSrc ) {
super( dataSrc );
}
}
public static class WaterSensor extends Sensor{
protected WaterSensor( String dataSrc ) {
super( dataSrc );
}
}
}
应该初始化抽象类的子类。
在main函数中
Sensor[] s=new Sensor[]{
new DaySensor(),
new TemperatureSensor(),
new LocationSensor(),
new WaterSensor(),
new HRBPSensor()
};
得到5个Sensor对象的数组