Java 为什么我的程序中只有三个对象中的一个运行?
我对java非常陌生,并试图掌握如何在彼此之间使用类。这是一个汽车银行贷款项目。我已经创建了Bil(Car)类的三个不同对象,当我通过方法bilLån()运行这些对象时,它只运行行中的最后一个对象,在本例中是“Opel”-对象。它确实在运行,所以一定是逻辑错误 班克尔班Java 为什么我的程序中只有三个对象中的一个运行?,java,object,subclass,Java,Object,Subclass,我对java非常陌生,并试图掌握如何在彼此之间使用类。这是一个汽车银行贷款项目。我已经创建了Bil(Car)类的三个不同对象,当我通过方法bilLån()运行这些对象时,它只运行行中的最后一个对象,在本例中是“Opel”-对象。它确实在运行,所以一定是逻辑错误 班克尔班 import java.util.Scanner; import java.text.DecimalFormat; public class bankLån { DecimalFormat df1 = new Dec
import java.util.Scanner;
import java.text.DecimalFormat;
public class bankLån {
DecimalFormat df1 = new DecimalFormat("#.##");
double price;
int years;
double interest;
double interestDiv;;
double oneYr = 14.99;
double threeYr = 10.99;
double fiveYr = 6.99;
double tenYr = 2.99;
double monthlyInterCalc;
double monthlyPayment;
double monthlyInterest;
int months = 12;
Scanner input = new Scanner(System.in);
bankLån() {
}
public void carLoan() {
System.out.println();
System.out.println("You've picked: "+Car.carBrand+"!");
price = (Car.carPrice) - (Car.downPay);
System.out.println("With a down payment of "+Car.downPay);
System.out.println("It will cost you: "+price+"!");
System.out.println("Our interests are: ");
System.out.println("1 year = "+oneYr+"% \t 3 years = "+threeYr+"% \t 5 years = "+fiveYr+"% \t 10 years = "+tenYr+"%");
do {
System.out.println("Please input amount of years: ");
years = input.nextInt();
if (years == 1) {
interest = oneYr;
}
else if (years == 3) {
interest = threeYr;
}
else if (years == 5) {
interest = fiveYr;
}
else if (years == 10) {
interest = tenYr;
}
else {
System.out.println("That amount of years ain't available, please try again.");
}
}while(!(years == 1 || years == 3 || years == 5 || years == 10));
interestDiv = interest / 100;
monthlyInterCalc = (price / years) / (months);
monthlyInterest = monthlyInterCalc * interestDiv;
monthlyPayment = monthlyInterCalc + (monthlyInterCalc * interestDiv);
System.out.println("Your interest will be: "+interest+"% and will cost you around: "+df1.format(monthlyInterest)+ " every month.");
System.out.print("The monthly payment will be around: "+df1.format(monthlyPayment)+".");
System.out.println();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Car ferrari = new Car("Ferrari","Red",1000000,50000);
Car volvo = new Car("Volvo","White",170000,40000);
Car opel = new Car("Opel","Blue",40000,10000);
ferrari.carLoan();
opel.carLoan();
volvo.carLoan();
}
}
班车
public class Car extends bankLån{
static String carBrand;
static String carColor;
static double carPrice;
static double downPay;
Car(String brand, String color, double price, double downP) {
carBrand = brand;
carColor = color;
carPrice = price;
downPay = downP;
}
}
在类Bil中,所有变量都是静态的。这意味着它们持有的值对于这个类的所有不同对象都是相同的。这会使您的对象法拉利和沃尔沃与欧宝具有相同的属性值,因为欧宝是在上一节中设置的
删除这些变量之前的static关键字
carLoan方法将Car作为静态类而不是对象进行访问
System.out.println("You've picked: "+Car.carBrand+"!");
price = (Car.carPrice) - (Car.downPay);
Car不是实例对象,它引用的是静态类,因此出现错误“无法静态引用非静态字段Bil.carBrand”
要解决此问题,请在方法中获取一个Car对象,并访问它而不是类
public void carLoan(Car car) {
System.out.println();
System.out.println("You've picked: "+car.carBrand+"!");
price = (car.carPrice) - (car.downPay);
System.out.println("With a down payment of "+car.downPay);
System.out.println("It will cost you: "+price+"!");
System.out.println("Our interests are: ");
System.out.println("1 year = "+oneYr+"% \t 3 years = "+threeYr+"% \t 5 years = "+fiveYr+"% \t 10 years = "+tenYr+"%");
do {
System.out.println("Please input amount of years: ");
years = input.nextInt();
if (years == 1) {
interest = oneYr;
}
else if (years == 3) {
interest = threeYr;
}
else if (years == 5) {
interest = fiveYr;
}
else if (years == 10) {
interest = tenYr;
}
else {
System.out.println("That amount of years ain't available, please try again.");
}
}while(!(years == 1 || years == 3 || years == 5 || years == 10));
interestDiv = interest / 100;
monthlyInterCalc = (price / years) / (months);
monthlyInterest = monthlyInterCalc * interestDiv;
monthlyPayment = monthlyInterCalc + (monthlyInterCalc * interestDiv);
System.out.println("Your interest will be: "+interest+"% and will cost you around: "+df1.format(monthlyInterest)+ " every month.");
System.out.print("The monthly payment will be around: "+df1.format(monthlyPayment)+".");
System.out.println();
}
@Farhan Qasim说您需要删除
静态
但若您想在每个对象中进行处理,则需要在方法bilLån()
中传递对象引用,如
public void bilLån(Bil mBill) {
System.out.println();
System.out.println("You've picked: "+Bil.carBrand+"!");
price = (mBill.carPrice) - (mBill.downPay);
System.out.println("With a down payment of "+mBill.downPay);
System.out.println("It will cost you: "+price+"!");
System.out.println("Our interests are: ");
System.out.println("1 year = "+oneYr+"% \t 3 years = "+threeYr+"% \t 5 years = "+fiveYr+"% \t 10 years = "+tenYr+"%");
do {
System.out.println("Please input amount of years: ");
years = input.nextInt();
if (years == 1) {
interest = oneYr;
}
else if (years == 3) {
interest = threeYr;
}
else if (years == 5) {
interest = fiveYr;
}
else if (years == 10) {
interest = tenYr;
}
else {
System.out.println("That amount of years ain't available, please try again.");
}
}while(!(years == 1 || years == 3 || years == 5 || years == 10));
interestDiv = interest / 100;
monthlyInterCalc = (price / years) / (months);
monthlyInterest = monthlyInterCalc * interestDiv;
monthlyPayment = monthlyInterCalc + (monthlyInterCalc * interestDiv);
System.out.println("Your interest will be: "+interest+"% and will cost you around: "+df1.format(monthlyInterest)+ " every month.");
System.out.print("The monthly payment will be around: "+df1.format(monthlyPayment)+".");
System.out.println();
}
在主要方法中:
public static void main(String[] args) {
// TODO Auto-generated method stub
Bil ferrari = new Bil("Ferrari","Red",1000000,50000);
Bil volvo = new Bil("Volvo","White",170000,40000);
Bil opel = new Bil("Opel","Blue",40000,10000);
ferrari.bilLån(ferrari);
opel.bilLån(volvo);
volvo.bilLån(opel);
}
您的所有字段都是静态的,这意味着它们存在于每个类中,而不是每个实例中。删除static
限定符。Bil
扩展bankLån
的事实很奇怪。汽车是银行贷款吗?我们之间有“是”的关系吗?我能建议您只使用一种语言:Bil
,Lån
是丹麦语还是挪威语(对不起,不知道是哪种语言)carBrand
,carColor
等均为英语。坚持使用其中一个。嘿,安迪!很抱歉,现在除了“main”类的名称之外,所有内容都是英文的。我听到了,我一直在尝试删除它们,但是对象的值出现了错误,“bil.carBrand”,例如在方法中,“不能静态引用非静态字段bil.carBrand”。这是我首先将其更改为static的唯一原因。@JimNotCarry我不确定您是如何访问carLoan()方法的,因为它是Main类中的一个方法,而不是Car类。将该方法移动到car类,使用该方法而不是使用变量car。
public static void main(String[] args) {
// TODO Auto-generated method stub
Bil ferrari = new Bil("Ferrari","Red",1000000,50000);
Bil volvo = new Bil("Volvo","White",170000,40000);
Bil opel = new Bil("Opel","Blue",40000,10000);
ferrari.bilLån(ferrari);
opel.bilLån(volvo);
volvo.bilLån(opel);
}