0
0

オブジェクト指向3

Last updated at Posted at 2024-07-28

はじめに

 【オブジェクト指向】  【Java = ジャバ】言語で考えてみました。

以下を参照。

スーパークラス

継承元の親クラス


/**
  * 「人」を表す Person クラス
  */

public class Person {

    private int age;
    private String name;

	public Person(int age, String name) {
		this.age = age;
		this.name = name;
	}

    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

}


サブクラス

スーパークラスを継承している子クラス


/**
  * 「従業員」を表す Employee クラス
  */

public class Employee extends Person {

  private int employeeID;

// コンストラクタの宣言
  public Employee(int age, String name, int employeeID) {

    /* 親クラスの age と name 引数のコンストラクタを呼び出す */
    super(age, name);
    // コンストラクタの宣言
    this.employeeID = employeeID;
  }


  // getterメソッド
  public int getEmployeeID() {
    return this.employeeID;
  }


  // setterメソッド
  public void setEmployeeID(int employeeID) {
    this.employeeID = employeeID;
    }


}


インスタンス

Employee クラスを利用するテストプログラム


class EmployeeTest {
  public static void main(String[] args) {

    Employee p = new Employee(25, "田中 太郎", 111 );
    
    System.out.println(p.getAge()); // 25
    
    System.out.println(p.getName()); // 田中 太郎
    
    System.out.println(p.getEmployeeID()); // 111
    
  }

}

実行結果

25
田中 太郎
111

オブジェクト指向4

参考文献

Java関連情報サイト様

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0