java编程:线程
发布网友
发布时间:2022-05-05 08:23
我来回答
共3个回答
热心网友
时间:2023-10-24 11:09
//用来测试的类
public Class Test{
public static void main(String[] str){
Test t=new Test();
new Thread(new Wugui(100,t)).start();
new Thread(new Tuzi(200,t)).start();
}
Boolean flag=true; //标识谁先到
//判断胜负
public void Judeg(String name){
if(flag){
System.out.println(name+"胜利!");
flag=false;
}
else{
System.out.println(name+"气喘吁吁的到了,结果还是输了.....");
}
}
}
//角色类,抽象类,因为兔子和乌龟都有相同的属性方法,所以用继承
abstract class Role implements Runnable{
private int suDu; //速度
private juLi=0; //当前已前进距离
private Test t; //用于测试的类
private roleName; //角色名称
//构造方法,初始化数据
public Role(int suDu,String name,Test t){
this.suDu=suDu;
this.t=t;
this.roleName=name;
}
//线程方法,用于前进(1000是总距离)
public run(){
while(juLi>1000){
juLi+=suDu;
System.out.println(this.roleName+"已前进"+juLi+"米");
}
t.Judeg(roleName);
}
}
//乌龟继承自角色类
class WuGui extends Role{
public WuGui(int suDu,Test t){
super(suDu,"乌龟",t);
}
}
//兔子继承自角色类
class TuZi extends Role{
public TuZi(int suDu,Test t){
super(suDu,"兔子",t);
}
}
这个纯记事本手打,没有测试(网吧没办法测试),可能有语法错误,自己改改啦,大概思路是这样的:共有四个类,一个测试类,一个角色类(兔子和乌龟的父类,抽象类),一个兔子类,一个乌龟类,兔子和乌龟继承自角色类,因为兔子和乌龟都有相同的属性和方法,所以用继承更方便,更符合面向对象特征(假如后面又来了小猫、小狗、小鸡。。。怎么办,岂不是都要为它们写一个类?答案是不用,直接继续角色类它们都会有相同的特征),它们构造方法需要一个Test对象,Java中的类属于引用类型,所以在不同类中使用的Test是同一个Test,故可以判断出胜负,好了,说这么多了,不懂再问我,今天一上午在线哦!(网吧包早,嘿嘿!)追问调试不出来 好多错误
热心网友
时间:2023-10-24 11:10
/**
* 兔子
* @author Administrator
*
*/
public class Rabbit extends Thread{
//速度(x/秒)
private double runSpeedSecond;
//跑步总长度
private double totalLength;
public double getTotalLength() {
return totalLength;
}
public void setTotalLength(double totalLength) {
this.totalLength = totalLength;
}
public double getRunSpeedMinute(){
return this.runSpeedSecond;
}
public Rabbit(double speed){
this.runSpeedSecond = speed;
}
@Override
public void run() {
double time = 0;
while(totalLength > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
totalLength = totalLength-runSpeedSecond;
time++;
}
System.out.println("兔子已经跑完,用时"+time/10+"秒");
}
}
/**
* 乌龟类
* @author Administrator
*
*/
public class Tortoise implements Runnable{
//速度(x/秒)
private double runSpeedSecond;
//跑步总长度
private double totalLength;
public double getTotalLength() {
return totalLength;
}
public void setTotalLength(double totalLength) {
this.totalLength = totalLength;
}
public double getRunSpeedMinute(){
return this.runSpeedSecond;
}
public Tortoise(double speed){
this.runSpeedSecond = speed;
}
@Override
public void run() {
double time = 0;
while(totalLength > 0) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
totalLength = totalLength-runSpeedSecond;
time++;
}
System.out.println("乌龟已经跑完,用时"+time/10+"秒");
}
}
public class TestRabbitAndTortoise {
/**
* @param args
*/
public static void main(String[] args) {
Rabbit ra = new Rabbit(3.2);
Tortoise to = new Tortoise(1.1);
double length = 121.3;
ra.setTotalLength(length);
to.setTotalLength(length);
ra.start();
new Thread(to).start();
}
}
热心网友
时间:2023-10-24 11:10
额,这个还真不会。