2012-11-06 4 views
0

이 프로그램은 사용자 입력 정보를 받아서 arraylist 직원 객체에 저장합니다. 왜 이러한 오류가 발생했는지 정확히 알 수는 없습니다. 어떤 도움이 필요합니까?객체를 인스턴스화하려고 할 때 "정적 변수가 아닌 정적 변수를 참조 할 수 없습니다."- 왜 그런가요?

코드 :

import java.util.*; 
import java.io.*; 
import java.io.File; 
import java.io.FileReader; 
import java.util.ArrayList; 

public class P 
{ 
//Instance Variables 
private static String empName; 
private static String wage; 
private static double wages; 
private static double salary; 
private static int numHours; 
private static double increase; 

// static ArrayList<String> ARempName = new ArrayList<String>(); 
// static ArrayList<Double> ARwages = new ArrayList<Double>(); 
// static ArrayList<Double> ARsalary = new ArrayList<Double>(); 
static ArrayList<Employee> emp = new ArrayList<Employee>(); 

public static void main(String[] args) throws Exception 
{  
    Employee emp1 = new Employee("", 0);  


clearScreen(); 
printMenu(); 
question(); 
exit(); 
} 

public static void printArrayList(ArrayList<Employee> emp) 
{ 
    for (int i = 0; i < emp.size(); i++){ 
      System.out.println(emp.get(i)); 
} 
} 

public static void clearScreen() 
{ 
    System.out.println("\u001b[H\u001b[2J"); 
} 

private static void exit() 
{ 
System.exit(0); 
} 

private static void printMenu() 
{ 
System.out.println("\t------------------------------------"); 
System.out.println("\t|Commands: n - New employee  |"); 
System.out.println("\t|   c - Compute paychecks |"); 
System.out.println("\t|   r - Raise wages   |"); 
System.out.println("\t|   p - Print records  |"); 
System.out.println("\t|   d - Download data  |"); 
System.out.println("\t|   u - Upload data   |"); 
System.out.println("\t|   q - Quit    |"); 
System.out.println("\t------------------------------------"); 
System.out.println(""); 
} 

private static void question() 
{ 
System.out.print("Enter command: "); 
Scanner q = new Scanner(System.in); 
String input = q.nextLine(); 
input.replaceAll("\\s","").toLowerCase(); 

boolean valid = (input.equals("n") || input.equals("c") || input.equals("r") || input.equals("p") || input.equals("d") || input.equals("u") || input.equals("q")); 

if (!valid){ 
    System.out.println("Command was not recognized; please try again."); 
printMenu(); 
question(); 
} 
else if (input.equals("n")){ 
    System.out.print("Enter the name of new employee: "); 
    Scanner stdin = new Scanner(System.in); 
    empName = stdin.nextLine(); 
    emp1.setName(empName); 
    System.out.print("Hourly (h) or salaried (s): "); 
    Scanner stdin2 = new Scanner(System.in); 
    wage = stdin2.nextLine(); 
    wage.replaceAll("\\s","").toLowerCase(); 
    if (!(wage.equals("h") || wage.equals("s"))){ 
    System.out.println("Input was not h or s; please try again"); 
    } 
    else if (wage.equals("h")){ 
    System.out.print("Enter hourly wage: "); 
    Scanner stdin4 = new Scanner(System.in); 
    wages = stdin4.nextDouble(); 
    emp1.setWage(wages); 
    printMenu(); 
    question();} 
    else if (wage.equals("s")){ 
    System.out.print("Enter annual salary: "); 
    Scanner stdin5 = new Scanner(System.in); 
    salary = stdin5.nextDouble(); 
    emp1.setWage(salary); 
    printMenu(); 
    question();}} 
else if (input.equals("c")){ 
    System.out.print ("Enter number of hours worked by " + empName); 
    Scanner stdin = new Scanner(System.in); 
    numHours = stdin.nextInt(); 
    System.out.println("Pay: "); 
    System.out.print("Enter number of hours worked by " + empName); 
     Scanner stdin2 = new Scanner(System.in); 
     numHours = stdin2.nextInt(); 
     System.out.println("Pay: "); 
    printMenu(); 
    question();} 
else if (input.equals("r")){ 
    System.out.print("Enter percentage increase: "); 
    Scanner stdin = new Scanner(System.in); 
    increase = stdin.nextDouble(); 
    System.out.println("\nNew Wages"); 
    System.out.println("---------"); 
//  System.out.println(Employee.toString()); 
    printMenu(); 
    question(); 
    } 
else if (input.equals("p")){ 
    printArrayList(emp); 
    printMenu(); 
    question(); 
} 
else if (input.equals("q")){ 
    exit(); 
} 
} 


public abstract class Employee { 
private String name; 
private double wage; 

protected Employee(String name, double wage){ 
    this.name = name; 
    this.wage = wage; 
} 

public String getName() { 
    return name; 
} 

public double getWage() { 
    return wage; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public void setWage(double wage) { 
    this.wage = wage; 
} 

public void percent(double wage, double percent) { 
    wage *= percent; 
} 
} 

public class HourlyEmployee extends Employee { 
//Instance Variables 
public String result; 

public HourlyEmployee(String name, double wage){ 
    super(name, wage); 
} 

public void computePay(double wage){ 
    if (numHours <= 40){ 
    wage *= numHours;} 
    else { 
    wage = numHours * (1.5 * wage);} 
} 

/* public void toString(){ 
    System.out.println("Name: " + name); 
}*/ 
} 

public class SalariedEmployee extends Employee { 
    //Instance Variables 
private double salary; 
private int salHours = 2080; 

    public SalariedEmployee(String name, double wage){ 
     super(name, wage); 
    } 

    public void computePay(double wage){ 
     salary = (salHours * salary)/52;} 

public double getSalary(){ 
    return salary; 
} 

public void setSalary(double salary) { 
    this.salary = salary; 
} 

/*  public void toString(){ 
     System.out.print("Name: " name + getSalary() + "/year"); 
    }*/ 
} 
} 

오류 :

javac P.java 
P.java:24: non-static variable this cannot be referenced from a static context 
    Employee emp1 = new Employee("", 0);  
        ^
P.java:82: cannot find symbol 
symbol : variable emp1 
location: class P 
    emp1.setName(empName); 
    ^
P.java:94: cannot find symbol 
symbol : variable emp1 
location: class P 
    emp1.setWage(wages); 
    ^
P.java:101: cannot find symbol 
symbol : variable emp1 
location: class P 
    emp1.setWage(salary); 
    ^
4 errors 

어떤 힌트 감사보다 더 많은 것이 올바른 방향으로 날 가리 키도록!

당신은이 : 당신은 추상에 객체를 생성하지 못할 prolly 때문에

+1

* 자세한 설명 제목을 입력하십시오. –

+0

[정적이 아닌 변수는 정적 컨텍스트 (Java)에서 참조 할 수 없습니다] (http://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static -context-java) – EJP

답변

1

당신은

 Employee emp1 = HiredEmployee("", 0) or SalaryEmployee("", 0); 
0

귀하의 코드를 수정하기 쉽습니다 .. .. 로컬로 초기화 된 추상 클래스, emp1 밤은을 인스턴스화 할 수 없습니다 대체 :

public abstract class Employee {} 

public static class Employee {} 

추상 클래스를 인스턴스화 할 수 없으며 직원 클래스가 정적이라고 생각한다고 생각합니다. 이어서

는로

final Employee emp1 = new Employee("", 0); 

이동 :

private static void question() {} 

emp1은 그 기능에 사용되며, 그렇지 않으면 같이, (그 "범위 밖"이다) 표시 여부 것처럼 대안으로 전역 변수로 선언하십시오.

0

non-static variable this cannot be referenced from a static context

Employee는 비 정적 내부 클래스와 같은 그 외 클래스의 인스턴스를 둘러싸 가져야한다. (

public abstract class Employee { 

public static abstract class Employee { 

및 작동한다 : 따라서 당신은 P

메커니즘은 Java Tutorial > Nested Classes

변경이 라인을 설명하지 않고 Employee을 만들 수 없습니다 다른 Employee 하위 유형도 변경해야 함)