首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

【关于对象】没法在静态方法中引用非静态变量

2013-03-04 
【关于对象】无法在静态方法中引用非静态变量本帖最后由 yi_remember 于 2013-03-01 11:44:51 编辑/** * 计

【关于对象】无法在静态方法中引用非静态变量
本帖最后由 yi_remember 于 2013-03-01 11:44:51 编辑

/**
 * 计算贷款。
 */
package Ten;

import java.util.Date;
import java.util.Scanner;

public class TestLoanClass {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter yearly interest rate, for example, 8.25: ");
double annualInterestRate = input.nextDouble();

System.out.print("Enter number of yearr as an integer: ");
double numberOfYears = input.nextDouble();

/*
 * 下列代码中的loanAmount显示错误。
 * Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
 * Cannot make a static reference to the non-static field loanAmount
 * 
 */
TestLoanClass loan = new TestLoanClass(annualInterestRate, numberOfYears, loanAmount);

System.out.printf("The loan was created of %s\n" +
"The monthly payment is %.2f\nThe total payment is %.2f\n", 
loan.getLoanDate().toString(), loan.getMonthlyPayment(), loan.getTotalPayment());

}

private double annualInterestRate;
private int numberOfYears;
private double loanAmount;
private Date loanDate;

public TestLoanClass(){
this(2.5, 1, 1000);
}
public TestLoanClass(double annualInterestRate, int numberOfYears, double loanAmount){
this.annualInterestRate = annualInterestRate;
this.numberOfYears = numberOfYears;
this.loanAmount = loanAmount;
loanDate = new Date();
}

public double getAnnualInterestRate(){
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate){
this.annualInterestRate = annualInterestRate;
}
public int getNumberOfYears(){
return numberOfYears;
}
public void setNumberOfYears(int numberOfYears){
this.numberOfYears = numberOfYears;
}
public double getLoanAmount(){
return loanAmount;
}
public void setLoanAmount(double loanAmount){
this.loanAmount = loanAmount;
}

public double getMonthlyPayment(){
double monthlyInterestRate = annualInterestRate /  1200;
double monthlyPayment = loanAmount * monthlyInterestRate / (1 - (Math.pow(1 / (1 + monthlyInterestRate), numberOfYears * 12)));
return monthlyPayment;
}
public double getTotalPayment(){
double totalPayment = getMonthlyPayment() * numberOfYears * 12;
return totalPayment;
}
public Date getLoanDate(){
return loanDate;
}
}
static
[解决办法]

改为
import java.util.Date;
import java.util.Scanner;
 
public class TestLoanClass {
    


   private static double annualInterestRate;
   private static int numberOfYears;
   private static double loanAmount;
   private Date loanDate;
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter yearly interest rate, for example, 8.25: ");
        annualInterestRate = input.nextDouble();
         
        System.out.print("Enter number of yearr as an integer: ");
        numberOfYears = (int) input.nextDouble();
         
        /*
         * 下列代码中的loanAmount显示错误。
         * Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
         * Cannot make a static reference to the non-static field loanAmount
         * 
         */
        TestLoanClass loan = new TestLoanClass(annualInterestRate, numberOfYears, loanAmount);
         
        System.out.printf("The loan was created of %s\n" +
            "The monthly payment is %.2f\nThe total payment is %.2f\n", 
            loan.getLoanDate().toString(), loan.getMonthlyPayment(), loan.getTotalPayment());
        input.close();
         
    }

     
    public TestLoanClass(){
        this(2.5, 1, 1000);
    }
    public TestLoanClass(double annualInterestRate, int numberOfYears, double loanAmount){
        this.annualInterestRate = annualInterestRate;
        this.numberOfYears = numberOfYears;
        this.loanAmount = loanAmount;
        loanDate = new Date();
    }
     
    public double getAnnualInterestRate(){
        return annualInterestRate;
    }
    public void setAnnualInterestRate(double annualInterestRate){
        this.annualInterestRate = annualInterestRate;
    }
    public int getNumberOfYears(){
        return numberOfYears;
    }


    public void setNumberOfYears(int numberOfYears){
        this.numberOfYears = numberOfYears;
    }
    public double getLoanAmount(){
        return loanAmount;
    }
    public void setLoanAmount(double loanAmount){
        this.loanAmount = loanAmount;
    }
     
    public double getMonthlyPayment(){
        double monthlyInterestRate = annualInterestRate /  1200;
        double monthlyPayment = loanAmount * monthlyInterestRate / (1 - (Math.pow(1 / (1 + monthlyInterestRate), numberOfYears * 12)));
        return monthlyPayment;
    }
    public double getTotalPayment(){
        double totalPayment = getMonthlyPayment() * numberOfYears * 12;
        return totalPayment;
    }
    public Date getLoanDate(){
        return loanDate;
    }
}


[解决办法]
TestLoanClass类 独立写到一个文件中

import java.util.Date;

public class TestLoanClass {
private double annualInterestRate;
private int numberOfYears;
private double loanAmount;
private Date loanDate;

public TestLoanClass(){
this(2.5, 1, 1000);
}
public TestLoanClass(double annualInterestRate, int numberOfYears, double loanAmount){
this.annualInterestRate = annualInterestRate;
this.numberOfYears = numberOfYears;
this.loanAmount = loanAmount;
loanDate = new Date();
}

public double getAnnualInterestRate(){
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate){
this.annualInterestRate = annualInterestRate;
}
public int getNumberOfYears(){
return numberOfYears;
}
public void setNumberOfYears(int numberOfYears){
this.numberOfYears = numberOfYears;
}
public double getLoanAmount(){
return loanAmount;
}
public void setLoanAmount(double loanAmount){
this.loanAmount = loanAmount;
}

public double getMonthlyPayment(){
double monthlyInterestRate = annualInterestRate /  1200;
double monthlyPayment = loanAmount * monthlyInterestRate / (1 - (Math.pow(1 / (1 + monthlyInterestRate), numberOfYears * 12)));
return monthlyPayment;
}
public double getTotalPayment(){
double totalPayment = getMonthlyPayment() * numberOfYears * 12;
return totalPayment;
}
public Date getLoanDate(){
return loanDate;
}

}


然后在public class TestLoanClass {    
    public static void main(String[] args)  测试
不然就要把所有的方法编写成staitc的

热点排行