java.sun.com の場合、com.sun.java maruno.com の場合、com.maruno
package パッケージ名;
$ javac -d パッケージ・ルート -classpath クラス・パス ソースファイル名
$ java -cp ユーザークラスパス クラス名
import パッケージ名.クラス名; または import パッケージ名.*;Top
-- sources +-- application +-- TestProduct.java +-- product +-- Product.java +-- DiscountInterface.java +-- AbstractProduct.java +-- DiscountProduct.java +-- SetProduct.java -- classes +-- application +-- TestProduct.class +-- product +-- Product.class +-- ProductInterface.class +-- AbstructProduct.class +-- DiscountProduct.class +-- SetProduct.class
□ "Product.java"
- // 1201
- package product;
- // 抽象商品クラスです。
- // 商品を扱うクラスが持っていなければならないメソッドを提供しています。
- public abstract class AbstractProduct {
- private String code; // 商品コード
- private String name; // 商品名
- // 商品コードに値を設定します。
- public void setCode(String code) {
- this.code = code;
- }
- // 商品コードの値を取得します。
- public String getCode() {
- return this.code;
- }
- // 商品名に値を設定します。
- public void setName(String name) {
- this.name = name;
- }
- // 商品名の値を取得します。
- public String getName() {
- return this.name;
- }
- // 商品定価の値を取得します。
- public abstract int getPrice() ;
- }
□ "DiscountInterface.java"
- // 1201
- // 商品クラスです。
- // ひとつの商品に対するフィールドやフィールドを操作するためのメソッドを
- // 提供しています。
- package product;
- public class Product extends AbstractProduct {
- private int price; // 商品定価
- // 商品クラスのコンストラクター。
- public Product(String code, String name, int price) {
- setCode(code);
- setName(name);
- setPrice(price);
- }
- // 商品クラスのコンストラクター。
- public Product(String code, String name, String price) {
- setCode(code);
- setName(name);
- setPrice(price);
- }
- // 商品定価に値を設定します。
- public void setPrice(int price) {
- if (price < 0) {
- this.price = 0;
- return;
- }
- this.price = price;
- }
- // 商品定価に値を設定します。
- public void setPrice(String price) {
- setPrice(Integer.parseInt(price));
- }
- // 商品定価の値を取得します。
- public int getPrice() {
- return this.price;
- }
- }
□ "DiscountProduct.java"
- // 1201
- package product;
- // 割引インターフェースです。
- // 割引される商品情報が持っていなければならないメソッドの実装を強制しています。
- interface DiscountInterface {
- // 商品割引率の値を設定します。
- void setDiscount(int discount);
- // 商品割引率の値を取得します。
- int getDiscount();
- }
□ "SetProduct.java"
- // 1201
- package product;
- // 割引商品クラスです。
- // ひとつの商品に対するフィールドやフィールドを操作するためのメソッドを
- // 提供しています。
- public class DiscountProduct extends Product implements DiscountInterface {
- private int discount; // 商品割引率
- // 割引商品クラスのコンストラクター。
- public DiscountProduct(String code, String name, int price) {
- this(code, name, price, 0);
- }
- // 割引商品クラスのコンストラクター。
- public DiscountProduct(String code, String name, String price) {
- this(code, name, price, "0");
- }
- // 割引商品クラスのコンストラクター。
- public DiscountProduct(String code, String name, int price, int discount) {
- super(code, name, price);
- setDiscount(discount);
- }
- // 割引商品クラスのコンストラクター。
- public DiscountProduct(String code, String name, String price, String discount) {
- super(code, name, price);
- setDiscount(discount);
- }
- // 商品割引率に値を設定します。
- public void setDiscount(int discount) {
- if (discount < 0 || discount > 100) {
- this.discount = 0;
- return;
- }
- this.discount = discount;
- }
- // 商品割引率に値を設定します。
- public void setDiscount(String discount) {
- setDiscount(Integer.parseInt(discount));
- }
- // 商品割引率の値を取得します。
- public int getDiscount() {
- return this.discount;
- }
- // 商品の売値の値を取得します。
- public int getPrice() {
- return (int)(super.getPrice() * (1 - discount / 100.0));
- }
- }
□ "TestProduct.java"
- // 1201
- package product;
- // セット商品クラスです。
- // ひとつまたは複数の商品を集めて、割引したものをセット商品として扱います。
- // セット商品に対するフィールドやフィールドを操作するためのメソッドを
- // 提供しています。
- public class SetProduct extends AbstractProduct implements DiscountInterface {
- private int discount; // セット商品割引率
- private AbstractProduct[] products; // セット商品リスト
- // セット商品クラスのコンストラクター。
- public SetProduct(String code, String name, int discount, AbstractProduct[] products) {
- setCode(code);
- setName(name);
- setDiscount(discount);
- setProducts(products);
- }
- // 商品の売値の値を取得します。
- public int getPrice() {
- int price = 0;
- for (int i = 0; i < products.length; i++) {
- price += products[i].getPrice();
- }
- return (int)(price * (1 - discount / 100.0));
- }
- // 商品割引率に値を設定します。
- public void setDiscount(int discount) {
- if (discount < 10 || discount > 100) {
- this.discount = 0;
- return;
- }
- this.discount = discount;
- }
- // 商品割引率に値を設定します。
- public void setDiscount(String discount) {
- setDiscount(Integer.parseInt(discount));
- }
- // 商品割引率の値を取得します。
- public int getDiscount() {
- return discount;
- }
- // 商品割引率に値を設定します。
- public void setProducts(AbstractProduct[] products) {
- this.products = products;
- }
- }
- // 1201
- package application;
- import product.*;
- // 商品クラスをテストするクラスです。
- public class TestProduct {
- public static void main(String[] args) {
- AbstractProduct[] p = new AbstractProduct[4];
- p[0] = new DiscountProduct("A001", "パソコン", 188000, 20);
- p[1] = new Product("P001", "プリンター", 24000);
- p[2] = new DiscountProduct("C001", "CD-ROM", 6500, 10);
- p[3] = new Product("M001", "USBメモリー", 9800);
- System.out.println("商品配列のインスタンスを生成しました。 p = " + p);
- for (int i = 0; i < p.length; i++) {
- System.out.println("商品のインスタンスを生成しました。 p[" + i + "] = " + p[i]);
- System.out.println(" code = " + p[i].getCode());
- System.out.println(" name = " + p[i].getName());
- System.out.println(" price = " + p[i].getPrice());
- }
- SetProduct s = new SetProduct("SA01", "パソコン・セット", 10, p);
- System.out.println("セット商品のインスタンスを生成しました。 s = " + s);
- System.out.println(" code = " + s.getCode());
- System.out.println(" name = " + s.getName());
- System.out.println(" price = " + s.getPrice());
- }
- }
$ javac -d classes sources/product/*.java <- 商品 $ javac -d classes -classpath classes sources/application/*.java <- アプリケーション $ java -cp classes application.TestProduct <- 実行 商品配列のインスタンスを生成しました。 p = [Lproduct.ProductInterface;@1004901 商品のインスタンスを生成しました。 p[0] = product.DiscountProduct@1b90b39 code = A001 name = パソコン price = 150400 商品のインスタンスを生成しました。 p[1] = product.Product@18fe7c3 code = P001 name = プリンター price = 24000 商品のインスタンスを生成しました。 p[2] = product.DiscountProduct@b8df17 code = C001 name = CD-ROM price = 5850 商品のインスタンスを生成しました。 p[3] = product.Product@13e8d89 code = M001 name = USBメモリー price = 9800 セット商品のインスタンスを生成しました。 s = product.SetProduct@cf2c80 code = SA01 name = パソコン・セット price = 171045
$ mkdir -p maruno/classes maruno/sources
- package com.maruno.mypack;
- public class Pack01 {
- public void printData() {
- System.out.println( "Pack01" );
- }
- }
□ コンパイル
$ javac -d ../classes Pack01.java
- import com.maruno.mypack.Pack01;
- public class Pack02 {
- public static void main( String[] args ) {
- Pack01 pack01;
- pack01 = new Pack01();
- pack01.printData();
- }
- }
□ コンパイル
$ javac -classpath ../classes Pack02.java
□ 実行結果
$java -cp .:../classes com.maruno.mypack.Pack02 Pack01