Created
September 5, 2016 04:52
-
-
Save tyoshikawa1106/5613d8d7050b421ff19e0bf852bdb9e5 to your computer and use it in GitHub Desktop.
オブジェクト指向サンプル - Apex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public virtual with sharing class Employee { | |
// 社員名 | |
private String name; | |
// 役職 | |
private String position; | |
/** | |
* コンストラクタ | |
*/ | |
public Employee() { | |
} | |
// 社員名のゲッタ | |
public String getName() { | |
return name; | |
} | |
// 役職のゲッタ | |
public String getPosition() { | |
return position; | |
} | |
/// 社員名のセッタ | |
public void setName(String name) { | |
this.name = name; | |
} | |
// 役職のセッタ | |
public void setPosition(String position) { | |
this.position = position; | |
} | |
// 出勤メソッド | |
public void clockIn() { | |
System.debug(this.name + ' 出勤しました。'); | |
} | |
// 退勤メソッド | |
public void clockOut() { | |
System.debug(this.name + ' 退勤しました。'); | |
} | |
// 仕事メソッド | |
public virtual void work() { | |
System.debug(this.position + 'です。仕事します。'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public with sharing class PartTime extends Employee { | |
// シフト | |
private String shift; | |
/** | |
* コンストラクタ | |
*/ | |
public PartTime() { | |
} | |
// シフトのゲッタ | |
public String getShift() { | |
return shift; | |
} | |
// シフトのセッタ | |
public void setShift(String shift) { | |
this.shift = shift; | |
} | |
// 社員クラスのメソッドをオーバーライド | |
public override void work() { | |
System.debug(getPosition() + 'です。シフトの間、仕事します。'); | |
} | |
// 社員クラスのメソッドを呼び出す | |
public void empWork() { | |
super.work(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public with sharing class Company { | |
/** | |
* コンストラクタ | |
*/ | |
public Company() { | |
// 社員クラスのインスタンスを生成 | |
Employee emp = new Employee(); | |
// アルバイトクラスのインスタンスを生成 | |
PartTime part = new PartTime(); | |
// 役職名を格納 | |
emp.setPosition('一般社員'); | |
part.setPosition('アルバイト'); | |
// 仕事 | |
emp.work(); | |
part.work(); | |
part.empWork(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment