public class Main {
public static void main(String[] args) {
Student student = new Student("和島 茂", "青森", 2, 3124000);
student.introduce();
}
}
// 人間の抽象クラス
abstract class Human {
protected String name; // 名前
// 自己紹介メソッド
public abstract void introduce();
}
// 学生クラス
class Student extends Human {
String campus; // 所属キャンパス
int grade; // 学年
int id; // 学籍番号
// コンストラクタ
public Student(String name, String campus, int grade, int id) {
this.name = name;
this.campus = campus;
this.grade = grade;
this.id = id;
}
// 自己紹介メソッドの実装
public void introduce() {
System.out.print("私は" + name + "です。");
System.out.print(campus + "キャンパスの" + grade + "年生です。");
System.out.println("学籍番号は" + id + "です。");
}
}
実行結果
public class Main {
public static void main(String[] args) {
Student student = new Student("和島 茂", "青森", 2, 3124000);
student.introduce();
Worker worker = new Worker("山田 太郎", "トヨタ", "営業部", 5963000);
worker.introduce();
}
}
// 人間の抽象クラス
abstract class Human {
protected String name; // 名前
// 自己紹介メソッド
public abstract void introduce();
}
// 学生クラス
class Student extends Human {
String campus; // 所属キャンパス
int grade; // 学年
int id; // 学籍番号
// コンストラクタ
public Student(String name, String campus, int grade, int id) {
this.name = name;
this.campus = campus;
this.grade = grade;
this.id = id;
}
// 自己紹介メソッドの実装
public void introduce() {
System.out.print("私は" + name + "です。");
System.out.print(campus + "キャンパスの" + grade + "年生です。");
System.out.println("学籍番号は" + id + "です。");
}
}
// 社会人クラス
class Worker extends Human {
String company; // 会社名
String department; // 部署名
int id; // 社員番号
// コンストラクタ
public Worker(String name, String company, String department, int id){
this.name = name;
this.company = company;
this.department = department;
this.id = id;
}
// 自己紹介メソッドの実装
public void introduce(){
System.out.print("私は" + name + "です。");
System.out.print(company + "の" + department + "に所属しています。");
System.out.println("社員番号は" + id + "です。");
}
}
実行結果
public class Main {
public static void main(String[] args) {
Human human = new Human("和島 茂");
human.walk(10);
human.walk(20);
}
}
// 歩くもののインターフェイス
interface iWalker {
void walk(int distance); // 歩くメソッド
}
// 人間クラス
class Human implements iWalker {
String name; // 名前
int position; // 位置
// コンストラクタ
Human(String name) {
this.name = name;
this.position = 0;
}
// 歩くメソッド
public void walk(int distance) {
this.position += distance;
System.out.println(this.name + "が" + distance + "歩きました。現在位置は" + this.position + "です。");
}
}
実行結果
public class Main {
public static void main(String[] args) {
Human human = new Human("和島 茂");
human.walk(10);
human.walk(20);
Bird bird = new Bird("ジョナサン");
bird.walk(10);
bird.fly(50);
}
}
// 歩くもののインターフェイス
interface iWalker {
void walk(int distance); // 歩くメソッド
}
// 飛ぶもののインターフェイス
interface iFlyer {
void fly(int distance); // 飛ぶメソッド
}
// 人間クラス
class Human implements iWalker {
String name; // 名前
int position; // 位置
// コンストラクタ
Human(String name) {
this.name = name;
this.position = 0;
}
// 歩くメソッド
public void walk(int distance) {
this.position += distance;
System.out.println(this.name + "が" + distance + "歩きました。現在位置は" + this.position + "です。");
}
}
// 鳥クラス
class Bird implements iWalker, iFlyer {
String name; // 名前
int position; // 位置
// コンストラクタ
Bird(String name) {
this.name = name;
this.position = 0;
}
// 歩くメソッド
public void walk(int distance) {
this.position += distance;
System.out.println(this.name + "が" + distance + "歩きました。現在位置は" + this.position + "です。");
}
// 飛ぶメソッド
public void fly(int distance) {
this.position += distance;
System.out.println(this.name + "が" + distance + "飛びました。現在位置は" + this.position + "です。");
}
}
実行結果