我在静态方法中声明了两个参数,并且声明了一个新的变量,然后在别的类中,我使用了这三个变量,但是会报错说找不到该变量,这是为什么呢,以下是我的源码和报错信息
package src;
class CashierTask extends Thread {
// TODO: your code here
CashierTask(){
super();
}
Queue queue = new Queue();
public void run(){
try{
if(queue.size() == 0){
interrupted();
}
else{
int y;//该cashier接customers[i]所用时间
y = queue.front() * 100;//头元素*100之后再pop掉
queue.pop();
Thread.sleep(y);
}
}catch(InterruptedException e){
System.out.print("");
}
}//每个线程要做的事
}
class Cashier {
// TODO: your code her
int z;
Cashier(){
for(z = 0;z < n;z++){
new CashierTask().start();
}
}
}
class Queue {
// TODO: your code here
int i;
int front;
int rear;
int maxsize;
int[] arr;
int front()
{
return arr[front];
}
void pop()
{
front = (front + 1) % maxsize;
}
void push(int value)
{
rear = (rear + 1) % maxsize;
arr[rear] = value;
}
int size()
{
return (rear + maxsize - front + 1) % maxsize;
}
Queue(){
front = 1;
rear = 0;
maxsize = N + 1;
arr = new int[maxsize];
for(i = 0;i < N;i++){
push(customers[i]);
}
}//构造函数
}
public class Supermarket {
public static long serveCustomer(int[] customers, int n) {
// TODO: implement intialization here
int N;//顾客数量
N = customers.length;
// ===================================================
long start = System.currentTimeMillis();//运行时间毫秒数
// TODO: implement processing tasks here
new Queue();
new Cashier();//new了cashier就等于new了cashiertask,new了cashiertask就等于new了queue
int elapsedTime = Long.valueOf((System.currentTimeMillis() - start) / 100).intValue();
// ===================================================
return elapsedTime;
}
}
方法内定义的变量只能在当前方法内使用。