集册 Java实例教程 定义自定义类以容纳另一个类

定义自定义类以容纳另一个类

欢马劈雪     最近更新时间:2020-01-02 10:19:05

593
定义自定义类以容纳另一个类
/** 来 自 N o w J a v a . c o m - 时  代  Java**/

import java.util.*;


class Storefront {

  private LinkedList catalog = new LinkedList();


  public void addItem(String id, String name, String price, String quant) {


    Item it = new Item(id, name, price, quant);

    catalog.add(it);

  }


  public Item getItem(int i) {

    return (Item) catalog.get(i);

  }


  public int getSize() {
  /**
  时 代 J a v a 提供 
  **/

    return catalog.size();

  }


  public void sort() {

    Collections.sort(catalog);

  }

}

class Item{

  private String id;


  private String name;

  private String price;

  private String quantity;

  

  public Item(String id, String name, String price, String quant){

    this.id = id;


    this.name = name;

    this.price = price;

    this.quantity = quantity;

  }

  

  public String getId() {

    return id;

  }

  public void setId(String id) {

    this.id = id;

  }

  

  

  public String getName() {

    return name;

  }

  public void setName(String name) {

    this.name = name;

  }

  public String getPrice() {

    return price;

  }

  public void setPrice(String price) {

    this.price = price;

  }

  public String getQuantity() {

    return quantity;

  }

  public void setQuantity(String quantity) {

    this.quantity = quantity;

  }

  

}

public class Main {

  public static void main(String[] arguments) {

    Storefront store = new Storefront();

    store.addItem("C01", "MUG", "9.99", "150");

    store.addItem("C02", "LG MUG", "12.99", "82");

    store.addItem("C03", "MOUSEPAD", "10.49", "800");

    store.addItem(
展开阅读全文