[英]Abstract class values not registering in 'extended' class
我写了 Java 代码,使用抽象来计算联邦和 state 从工资中计算所得税。
代码如下。
import java.util.Scanner;
abstract class USA_Tax{
protected final double income_tax = 0.15;
protected double pre_tax_amount;
protected double tax_to_fed;
public void federal_tax(){
Scanner input = new Scanner (System.in);
System.out.println ("Enter pre-tax income: ");
double pre_tax_amount = input.nextDouble();
System.out.println ("Money owned to federal government: ");
tax_to_fed = (double)(pre_tax_amount*0.15);
System.out.println (tax_to_fed);
input.close();
}
}
class California_Tax extends USA_Tax{
protected final double ca_income_tax = 0.10;
protected double after_california_tax;
protected double total_money_left;
public void ca_tax(){
after_california_tax = (double)(pre_tax_amount * ca_income_tax);
System.out.println("Money owned to CA government: " + after_california_tax);
total_money_left = (pre_tax_amount) - (after_california_tax + tax_to_fed);
System.out.println ("Total take home:" + total_money_left);
}
}
public class class_abstraction{
public static void main (String[] args){
California_Tax tax1 = new California_Tax();
tax1.federal_tax();
tax1.ca_tax();
}
}
当我运行代码时,似乎只有来自抽象 class 的值才能正确弹出。 扩展 class 中的 after_california_tax 和 total_money_left 变量的值显示为 0.0。 下面是一个例子 output。
Enter pre-tax income:
100000
Money owned to federal government:
15000.0
Money owned to CA government: 0.0
Total take home:-15000.0
我还尝试从第一个 class 中删除“抽象”,但没有任何改变。
我究竟做错了什么?
在public void federal_tax()
中,您重新定义变量pre_tax_amount
:
double pre_tax_amount = input.nextDouble();
因此,您没有使用上面定义的成员变量。 您需要在已声明的变量中分配值:
pre_tax_amount = input.nextDouble();
这是你搞砸的地方,你创建了一个不需要的新局部变量,使用你定义的实例变量代替。
double pre_tax_amount = input.nextDouble();
修改完成后的代码
import java.util.Scanner;
abstract class USA_Tax{
protected final double income_tax = 0.15;
protected double pre_tax_amount;
protected double tax_to_fed;
public void federal_tax(){
Scanner input = new Scanner (System.in);
System.out.println ("Enter pre-tax income: ");
pre_tax_amount = input.nextDouble();
System.out.println ("Money owned to federal government: ");
tax_to_fed = (double)(pre_tax_amount*0.15);
System.out.println (tax_to_fed);
input.close();
}
}
class California_Tax extends USA_Tax{
protected final double ca_income_tax = 0.10;
protected double after_california_tax;
protected double total_money_left;
public void ca_tax(){
after_california_tax = (double)(pre_tax_amount * ca_income_tax);
System.out.println("Money owned to CA government: " + after_california_tax);
total_money_left = (pre_tax_amount) - (after_california_tax + tax_to_fed);
System.out.println ("Total take home:" + total_money_left);
}
}
public class test{
public static void main (String[] args){
California_Tax tax1 = new California_Tax();
tax1.federal_tax();
tax1.ca_tax();
}
}
虽然我认为其他答案可以解决您的具体问题。 感觉您对抽象 class 的用途没有真正的了解。 一般来说,抽象 class 是一个 class,它没有定义所有行为,并且该行为必须由决定使用该 class 的实施者/程序员定义。在您的示例中,如何计算不同的税率。
要使用实际抽象,我们需要一个具有实际抽象方法的父对象 class。
public abstract class Tax {
private double income;
public Tax(double income) {
this.income = income;
}
public void printCalculation() {
System.out.println("Money owned to federal government: ");
double tax_to_fed = income * getTaxAmount();
System.out.println(tax_to_fed);
}
public abstract double getTaxAmount();
}
然后我们有两个实现类。
public class UsaTax extends Tax {
public UsaTax(double income) {
super(income);
}
@Override
public double getTaxAmount() {
return .15;
}
}
public class CaliforniaTax extends Tax {
public CaliforniaTax(double income) {
super(income);
}
@Override
public double getTaxAmount() {
return .10;
}
}
最后我们有我们的主要 class。
import java.util.Scanner;
public class TaxApplication {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.println ("Enter pre-tax income: ");
double pre_tax_amount = input.nextDouble();
UsaTax usaTax = new UsaTax(pre_tax_amount);
usaTax.printCalculation();
CaliforniaTax californiaTax = new CaliforniaTax(pre_tax_amount);
californiaTax.printCalculation();
// We could even create an anonymous inner class
Tax texasTax = new Tax(pre_tax_amount) {
@Override
public double getTaxAmount() {
return 0;
}
};
texasTax.printCalculation();
input.close();
}
}
output:
Enter pre-tax income:
10000
Money owned to federal government:
1500.0
Money owned to federal government:
1000.0
Money owned to federal government:
0.0
Process finished with exit code 0
我相信您首先尝试创建抽象类的原因是在父 class 中使用扫描仪。通过将扫描仪行为从您的 Tax class 中分离出来,我们减少了该类与用户输入方法的耦合,并允许我们编写class 只考虑它的行为,而不是它需要做什么以及如何正确处理用户输入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.