求救:编译显示“无法从静态上下文中引用非静态 方法”是怎么回事

gdufs_lswu 2011-12-19 05:01:56
import java.util.Scanner;
public class Salary{
String employeeId="";
double wage=0;
static double tax=0;
double realWage=0;
public double computeTax(){
tax=Tools.computeTax(wage);
return tax;
}
public double computeRealWage(){
realWage=wage-this.computeTax();
return realWage;
}
public static void main(String[] args){
Salary salary=new Salary();
Scanner sc=new Scanner (System.in);
System.out.println("职工号");
salary.employeeId=sc.nextLine();
System.out.println("税前工资");
salary.wage=sc.nextDouble();
System.out.println("职工号"+salary.employeeId+"税前

工资"+salary.wage+"扣税"+salary.computeTax()+"实发

工资"+salary.computeRealWage());
}
}
class Tools
{
final static int START_TAX=2000;
double tax;
public double computeTax(double wage)
{
if(wage-START_TAX<=0){
tax=0;
return tax;
}
if(wage-START_TAX<=500)
{
tax=(wage-2000)*0.05;
return tax;
}
if((wage-START_TAX>500)&&(wage-START_TAX<=2000))
{
tax=(wage-2000)*0.1-25;
return tax;
}
if(wage-START_TAX>2000)
{
tax=(wage-2000)*0.15-125;
return tax;
}
}
}
...全文
804 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
gdufs_lswu 2012-01-10
  • 打赏
  • 举报
回复
谢谢各位大虾点评,懂了哈
echola_2020 2011-12-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 xiongyu2006 的回复:]

如果必须要这样调用的话,得这样。供楼主参考
Java code

import java.util.Scanner;

class Tools
{
final static int START_TAX = 2000;
private static double tax;//这里要设置为static

public static double computeTax(d……
[/Quote]
+1
“无法从静态上下文中引用非静态 方法”:
public double computeTax(){
tax=Tools.computeTax(wage);
return tax;
}
这里用对类的静态方法调用,没有new Tools().computeTax(wage),
public double computeTax(double wage)
声明的是实例方法,只能通过对象实例调用。
还有return那一句的变量作用域问题。
xiongyu2006 2011-12-19
  • 打赏
  • 举报
回复
如果必须要这样调用的话,得这样。供楼主参考

import java.util.Scanner;

class Tools
{
final static int START_TAX = 2000;
private static double tax;//这里要设置为static

public static double computeTax(double wage) ////这里也要设置为static
{
if (wage - START_TAX <= 0)
{
tax = 0;
}
if (wage - START_TAX <= 500)
{
tax = (wage - 2000) * 0.05;

}
if ((wage - START_TAX > 500) && (wage - START_TAX <= 2000))
{
tax = (wage - 2000) * 0.1 - 25;
}
if (wage - START_TAX > 2000)
{
tax = (wage - 2000) * 0.15 - 125;
}
return tax;
}
}

public class Salary
{
String employeeId = "";
double wage = 0;
static double tax = 0;
double realWage = 0;

public double computeTax()
{
tax =Tools.computeTax(wage);
return tax;
}

public double computeRealWage()
{
realWage = wage - this.computeTax();
return realWage;
}

public static void main(String[] args)
{
Salary salary = new Salary();
Scanner sc = new Scanner(System.in);
System.out.println("职工号");
salary.employeeId = sc.nextLine();
System.out.println("税前工资");
salary.wage = sc.nextDouble();
System.out.println("职工号" + salary.employeeId + "税前工资" + salary.wage
+ "扣税" + salary.computeTax() + "实发工资"
+ salary.computeRealWage());
}
}
Ken_Wg 2011-12-19
  • 打赏
  • 举报
回复
静态属性和方法只能被静态到代码块访问,在非静态到代码块中访问静态到方法当然不行了,你可以将被调用的方法设置成静态的
xiongyu2006 2011-12-19
  • 打赏
  • 举报
回复

import java.util.Scanner;

class Tools
{
final static int START_TAX = 2000;
double tax;

public double computeTax(double wage)
{
if (wage - START_TAX <= 0)
{
tax = 0;
}
if (wage - START_TAX <= 500)
{
tax = (wage - 2000) * 0.05;

}
if ((wage - START_TAX > 500) && (wage - START_TAX <= 2000))
{
tax = (wage - 2000) * 0.1 - 25;
}
if (wage - START_TAX > 2000)
{
tax = (wage - 2000) * 0.15 - 125;
}
return tax;// 把大括号里面的return语句去掉,直接在外面return。
}
}

public class Salary
{
String employeeId = "";
double wage = 0;
static double tax = 0;
double realWage = 0;

public double computeTax()
{
tax = new Tools().computeTax(wage);// 这里没必要把此函数设置为static的。
return tax;
}

public double computeRealWage()
{
realWage = wage - this.computeTax();
return realWage;
}

public static void main(String[] args)
{
Salary salary = new Salary();
Scanner sc = new Scanner(System.in);
System.out.println("职工号");
salary.employeeId = sc.nextLine();
System.out.println("税前工资");
salary.wage = sc.nextDouble();
System.out.println("职工号" + salary.employeeId + "税前工资" + salary.wage
+ "扣税" + salary.computeTax() + "实发 工资"
+ salary.computeRealWage());
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧