Java 是否有任何数据字段变为;“静态”;没有明确声明?
我声明了没有“static”修饰符的私有数据字段,但是当我从一个方法调用该数据字段时。编译器表示,仅对一个数据字段“AnnualInterestate”和其他字段使用“对非静态方法的静态引用”似乎没问题 我还以同样的方式声明了其他数据,但它们没有问题。但在“AnnualInterestate”数据字段的情况下,它会给出错误Java 是否有任何数据字段变为;“静态”;没有明确声明?,java,Java,我声明了没有“static”修饰符的私有数据字段,但是当我从一个方法调用该数据字段时。编译器表示,仅对一个数据字段“AnnualInterestate”和其他字段使用“对非静态方法的静态引用”似乎没问题 我还以同样的方式声明了其他数据,但它们没有问题。但在“AnnualInterestate”数据字段的情况下,它会给出错误 import java.util.Date; import java.util.Scanner; public class Account { private int
import java.util.Date;
import java.util.Scanner;
public class Account {
private int id;
private double balance;
private double annualInterestRate;
private java.util.Date dateCreated;
public Account() {
}
public Account(int id, double balance, double interestRate) {
this.id = id;
this.balance = balance;
this.annualInterestRate = interestRate;
dateCreated = new java.util.Date();
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getBalance() {
return balance;
}
public void setInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
public double getInterestRate() {
return annualInterestRate;
}
public java.util.Date getDate(){
return dateCreated;
}
public double getMonthlyInterestRate() {
double monthlyInterestRate = annualInterestRate/12.0;
return monthlyInterestRate;
}
public double getmonthlyInterest() {
double monthlyInterestRate = annualInterestRate/12.0;
return monthlyInterestRate*balance;
}
public void withdraw(double balance) {
this.balance-=balance;
}
public void deposit(double balance) {
this.balance+=balance;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int id;
double balance,interestRate,mir;
Scanner in = new Scanner(System.in);
id = in.nextInt();
balance = in.nextDouble();
interestRate = in.nextDouble();
Account Person = new Account(id, balance, interestRate);
Person.withdraw(2500.0);
Person.deposit(3000.0);
mir = getmonthlyInterest();
System.out.println(balance + " " + mir + " " + dateCreated);
}
}
希望能够顺利运行稍微纠正一下使用Account类实例的主要方法:
public static void main(String[] args) {
// TODO Auto-generated method stub
int id;
double balance,interestRate,mir;
Scanner in = new Scanner(System.in);
id = in.nextInt();
balance = in.nextDouble();
interestRate = in.nextDouble();
Account myPerson = new Account(id, balance, interestRate);
myPerson.withdraw(2500.0);
myPerson.deposit(3000.0);
mir = myPerson.getmonthlyInterest();
System.out.println(balance + " " + mir + " " + myPerson.getDate());
}
你的意思是写
这个.annualInterestate
而不是annualInterestate.this
?我还怀疑您可能在某个地方有一些Person
类,与名为Person
的变量冲突(注意,按照惯例,变量名应该以小写字符开头,并且可能与它们所代表的对象相关)。您的代码中所有“this”用法都是错误的。应该是这个。
旁注:你真的不应该使用日期,它已经过时了。相反,请使用java.time
包中的类。此外,还应遵循java命名约定:变量名始终以小写开头。您的变量Person
应该是Person
。我是新java。然而,我做的更正仍然有同样的问题。