Javaのお勉強



クラス@Javaのお勉強

クラスを作成して利用します。

Customer(顧客)クラスを作成します。
属性は、id、name、zipCodeです。

コンストラクタとSetCustomerメソッドで顧客情報をセットします。
printCustomerメソッドで顧客情報を表示できるようにします。

呼び出しでは、コンストラクタで顧客情報のインスタンスを作成。
セットした顧客情報をprintCustomerで表示します。
その後、SetCustomerにて、顧客情報を変更し、
printCustomerで再度顧客情報を表示します。

●Customer

//顧客情報クラス
public class Customer {

    //顧客情報
    private String id;
    private String name;
    private String zipCode;

    //コンストラクタ
    public Customer() {
    }

    //コンストラクタ
    public Customer(String id,String name,String zipCode) {
        this.id = id;
        this.name = name;
        this.zipCode = zipCode;
    }

    //顧客情報をセット
    public void SetCustomer(String id,String name,String zipCode) {
        this.id = id;
        this.name = name;
        this.zipCode = zipCode;
    }

    //顧客情報を表示
    public void printCustomer() {
        System.out.println("お客様情報");
        System.out.println(this.id);
        System.out.println(this.name);
        System.out.println(this.zipCode);
    }
}



●呼び出し

//顧客インスタンス作成
Customer cus = new Customer("001","名前","999-9999");

//顧客情報表示
cus.printCustomer();

//顧客情報変更
cus.SetCustomer("001","名前変更","000-0000");

//顧客情報表示
cus.printCustomer();




Copyright (C) 2008-2026 Javaのお勉強. All Rights Reserved.