import java.util.*;
class Human {
private String name;
private int age;
private char gender;
public Human(String name, int age, char gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
// 基本の自己紹介メソッド
public void sayHello() {
System.out.println("こんにちは、私の名前は" + name + "です。");
}
// Getter(子クラスからプライベート変数を使うために必要ニダ)
public String getName() { return name; }
}
class Student extends Human {
private int number;
public Student(String name, int age, char gender, int number) {
super(name, age, gender);
this.number = number;
}
// 親のメソッドを「上書き」して、学生専用の挨拶にするニダ!
@Override
public void sayHello() {
// super.getName() で親クラスにある名前を呼んでるニダ
System.out.println("チース!ウリは" + getNumber() + "番の" + getName() + "ニダ!");
}
public int getNumber() { return number; }
}
class Main {
public static void main(String[] args) {
// Humanとして作成
Human h = new Human("佐藤", 40, 'W');
// Studentとして作成
Student s = new Student("田中", 20, 'M', 1);
h.sayHello(); // 「こんにちは...」と表示される
s.sayHello(); // 「チース!...」と表示されるニダ!
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: