5-3作业,0_o.不知道怎么做了.

5-3作业,0_o.不知道怎么做了.

WeatherTest类:
package com.imooc.thread;
public class ReadWeather extends Weather implements Runnable{
	
    public Weather wea;
   
    public void run(){
    	wea.generate();
    	System.out.println("读取天气数据[温度:"+wea.getTempureture()+",湿度"+wea.getHunidity());
    	try {
			Thread.sleep(1);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

	
}

GenerateWeather类:
package com.imooc.thread;

public class GenerateWeather extends Weather implements Runnable{
	
        public Weather wea;
       
        public void run(){
        	wea.generate();
        	System.out.println("生成天气数据[温度:"+wea.getTempureture()+",湿度"+wea.getHunidity());
        	try {
				Thread.sleep(1);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }

		
}
ReadWeather类:
public class ReadWeather extends Weather implements Runnable{
	
    public Weather wea;
   
    public void run(){
    	wea.generate();
    	System.out.println("读取天气数据[温度:"+wea.getTempureture()+",湿度"+wea.getHunidity());
    	try {
			Thread.sleep(1);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

	
}
Weather类:
package com.imooc.thread;

import java.util.Random;

public class Weather {
	
            private static int tempureture;//表示温度
            private static int hunidity;//表示湿度
            
            public Weather(){
            	
            }
            
            public Weather(int tempureture,int hunidity){//
            	this.setHunidity(hunidity);
            	this.setTempureture(tempureture);
            }

			public int getTempureture() {
				return tempureture;
			}

			public void setTempureture(int tempureture) {
				this.tempureture = tempureture;
			}

			public int getHunidity() {
				return hunidity;
			}

			public void setHunidity(int hunidity) {
				this.hunidity = hunidity;
			}
            
            public void 
W(){
            	tempureture=((int)(1+Math.random()*100));//使用random获取随机数
            	hunidity=((int)(1+Math.random()*100));//使用random获取随机数
            	setTempureture(tempureture);
            	setHunidity(hunidity);
            }
            
            public void read(){
            	
            }
            
            
}


正在回答

登陆购买课程后可参与讨论,去登陆

2回答

read方法就是获取温度和湿度并输出。在ReadWeather类的run方法中,要完成的功能是调用read方法并休眠,不需要再写其他的输出语句了。GenerateWeather类也是同样的道理。这里要注意每个类和方法都有独立的功能,生成和读的内容不写在同一个方法里。祝学习愉快!

  • 瞳劫 提问者 #1
    改了一下,没办法在测试类试用start();
    2017-01-30 21:39:05
  • 瞳劫 提问者 #2
    这个问题解决了,现在就是不知道怎么同步,还有读读取数据总是显示为0,和生成的数据不一样O_o,我再把教程看一遍,实在不行要靠你指导一下了啊,感谢。
    2017-01-30 22:28:30
提问者 瞳劫 2017-01-30 21:40:09
package com.imooc.test;

import com.imooc.thread.GenerateWeather;
import com.imooc.thread.ReadWeather;

public class WeatherTest extends Thread{
              public static void main(String[] args) {
				  GenerateWeather a=new GenerateWeather();
			      a.start();//这里报错了
			}

			
			
}
package com.imooc.thread;

public class GenerateWeather extends Weather implements Runnable{
	
        public Weather wea;
       
        public void run(){
        	try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        	wea.generate();
        }

		
}
package com.imooc.thread;

import java.util.Random;

public class Weather {
	
            private static int tempureture;//表示温度
            private static int hunidity;//表示湿度
            
            public Weather(){
            	
            }
            
            public Weather(int tempureture,int hunidity){//
            	this.setHunidity(hunidity);
            	this.setTempureture(tempureture);
            }

			public int getTempureture() {
				return tempureture;
			}

			public void setTempureture(int tempureture) {
				this.tempureture = tempureture;
			}

			public int getHunidity() {
				return hunidity;
			}

			public void setHunidity(int hunidity) {
				this.hunidity = hunidity;
			}
            
            public void generate(){
            	tempureture=((int)(1+Math.random()*100));//使用random获取随机数
            	hunidity=((int)(1+Math.random()*100));//使用random获取随机数
            	System.out.println("读取天气数据[温度:"+getTempureture()+",湿度"+getHunidity());
            }
            
            public void read(){
            	
            }
            
            
}


问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星

相似问题

登录后可查看更多问答,登录/注册

请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师