Java使用不同的Java文件和类从类调用子类
我有一个主java文件:Java使用不同的Java文件和类从类调用子类,java,class,methods,call,subclass,Java,Class,Methods,Call,Subclass,我有一个主java文件: public class GroceryBill { private Employee clerk; private List<Item> receipt; private double total; private double internalDiscount; public GroceryBill(Employee clerk) { this.clerk = clerk; rec
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
}
这是我的类,它位于扩展原始java文件的另一个java文件中。如何从属于GroceryBill类的Item子类调用方法?您可以这样做
使用该对象创建到超类的对象尝试为其子类创建对象
例如:
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);
Object myItem=新项目(名称、价格、折扣)代码>看起来可疑。实际的问题一点也不清楚,你想实现什么以及在哪里(注意:把每个类放在它自己的文件中可能会对你有帮助)?我实现了这个,但它给了我一个“新的静态类的限定”错误,我正在试着调试。你真的需要为你的内部类使用静态修饰符吗?因为如果你删除了这个静态修饰符,你可以毫不费力地访问它。是的,我看到它是类“Item”的“static”修饰符,但是给定的超类不能更改,这包括去掉static。
GroceryBill.Item = new GroceryBill.Item(name, price, discount);