内部类

package com.tech.Apple;

import com.sun.javaws.Main;

import java.util.ArrayList;

public class Invoice {
private static class Item{
String description;
int quantity;
double unitPrice;

public String getDescription() {
return description;
}

public int getQuantity() {
return quantity;
}

public double getUnitPrice() {
return unitPrice;
}

public Item() {
}

public Item(String description, int quantity, double unitPrice) {
this.description = description;
this.quantity = quantity;
this.unitPrice = unitPrice;
}
double price(){
return quantity * unitPrice;
}
}
private ArrayList<Item> items = new ArrayList<>();
public void add(Item item){
items.add(item);
System.out.println(item.getDescription() + " " + item.getQuantity() + " " + item.getUnitPrice() + " " + item.price());
}
public void addItem(String description, int quantity, double unitPrice){
Item item = new Item();
item.description = description;
item.quantity = quantity;
item.unitPrice = unitPrice;
System.out.println(item.getDescription() + " " + item.getQuantity() + " " + item.getUnitPrice() + " " + item.price());
}


public static void main(String[] args) {
Invoice.Item newItem = new Invoice.Item("BlackWell Toaster",2,19.95);
System.out.println(newItem.getDescription() + " " + newItem.getQuantity() + " " + newItem.getUnitPrice() + " " + newItem.price());
Invoice iv = new Invoice();
iv.add(newItem);
iv.addItem("pen",2,5);

}

}
原文地址:https://www.cnblogs.com/zj1234/p/8604684.html