【问题标题】:Java calling a subclass from a class using a different java file and classJava 使用不同的 java 文件和类从类调用子类
【发布时间】:2015-02-07 09:39:34
【问题描述】:

我有这个主要的 java 文件:

public class GroceryBill {
    private Employee clerk;
    private List<Item> receipt;
    private double total;
    private double internalDiscount;

    public GroceryBill(Employee clerk) {
        this.clerk = clerk;
        receipt = new ArrayList<Item>();
        total = 0.0;
        internalDiscount = 0.0;
    }

    public void add(Item i) {
        receipt.add(i);
        total += i.getPrice();
        internalDiscount += i.getDiscount();
    }

    public double getTotal() {
        return Math.rint(total * 100) / 100.0;
    }

    public Employee getClerk() {
        return clerk;
    }

    public void printReceipt() {
        System.out.println(this);
    }

    private String valueToString(double value) {
        value = Math.rint(value * 100) / 100.0;
        String result = "" + Math.abs(value);
        if(result.indexOf(".") == result.length() - 2) {
            result += "0";
        }
        result = "$" + result;
        return result;
    }

    public String receiptToString() {
        String build = "items:\n";
        for(int i = 0; i < receipt.size(); i++) {
            build += "   " + receipt.get(i);
            if(i != receipt.size() - 1) {
                build += "\n";
            }
        }
        return build;
    }

    public String toString() {
        return receiptToString() + "\ntotal: " + valueToString(total);
    }

    public String discountToString() {
        return receiptToString() + "\nsub-total: " + valueToString(total) + "\ndiscount: " + valueToString(internalDiscount) + "\ntotal: " + valueToString(total - internalDiscount);
    }

    public static class Employee {
        private String name;

        public Employee(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }
    }

    public static class Item {
        private String name;
        private double price;
        private double discount;

        public Item(String name, double price, double discount) {
            this.name = name;
            this.price = price;
            this.discount = discount;
        }

        public double getPrice() {
            return price;
        }

        public double getDiscount() {
            return discount;
        }

        private String valueToString(double value) {
            String result = "" + Math.abs(value);
            if(result.indexOf(".") == result.length() - 2) {
                result += "0";
            }
            result = "$" + result;
            return result;
        }

        public String toString() {
            return name + " " + valueToString(price) + " (-" + valueToString(discount) + ")";
        }
    }

//  REPLACEME

}

我想调用并能够使用“Item”子类中的 getDiscount() 等,但我不知道如何在扩展 GroceryBill 的不同文件 + 类中访问它。

public class DiscountBill extends GroceryBill {
    
    private int myCount;
    private double myDiscount;
    private double myPrice;
    private String myName;
    
    public DiscountBill(Employee clerk, boolean preferred) {
        super(clerk);
        String name = "";
        double price = 0;
        double discount = 0;
        GroceryBill.Item myBill = new GroceryBill.Item(name, price, discount);
        Object myItem = new Item(name, price, discount);
        myDiscount = myBill.getDiscount();
        myPrice = ((GroceryBill.Item) myItem).getPrice();
        if (preferred && myDiscount > 0.0) {
            myCount++;
        }
    }
    
    public int getDiscountCount() {
        return myCount;
    }
    
    public double getDiscountAmount() {
        return myDiscount;
    }
    
    public double getDiscountPercent() {
        return (myPrice / getDiscountCount()) * 100;
    }
    
}

这是我的类,它位于扩展原始文件的不同 java 文件中。如何从属于 GroceryBill 类的 Item 子类调用方法?

【问题讨论】:

  • Object myItem = new Item(name, price, discount); 看起来很可疑。实际的问题根本不清楚,你想实现什么以及在哪里(注意:将每个类放在它自己的文件中可能会对你有所帮助)?

标签: java class methods call subclass


【解决方案1】:

你可以这样做

使用该对象为超类创建对象尝试为其子类创建对象。

例如:

public class Sample {
class InnerClass
{
    public void foo()
    {
        System.out.println("Helolo");
    }
}
}

基类是:

public class BaseClass extends Sample {
public static void main(String[] args) {
    Sample sObj = new Sample();
    Sample.InnerClass innerObj = sObj.new InnerClass();
    innerObj.foo();
}
}

你可以按照这种方式调用内部类方法

访问内部静态类:

OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();

在您的情况下,只需添加如下语句:

GroceryBill.Item = new GroceryBill.Item(name, price, discount);

【讨论】:

  • 我实现了这个,但它给了我一个“合格的新静态类”错误,我正在尝试调试。
  • 你是否真的需要为你的内部类添加静态修饰符,因为如果你删除了那个静态修饰符,你就可以毫无问题地访问它。
  • 是的,我看到它是“Item”类的“静态”修饰符,但给定的超类不能更改,包括取出静态。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-22
  • 1970-01-01
  • 2017-10-08
  • 2019-07-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多