20261027
This commit is contained in:
@@ -1,4 +1,14 @@
|
||||
|
||||
//親クラス
|
||||
public class Animal {
|
||||
|
||||
String name;
|
||||
|
||||
public void run() {
|
||||
System.out.println(name + "は走りました。");
|
||||
}
|
||||
|
||||
public void eat() {
|
||||
System.out.println(name + "は食事をしました。");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
6
Question13-Extends/src/main/java/Cat.java
Normal file
6
Question13-Extends/src/main/java/Cat.java
Normal file
@@ -0,0 +1,6 @@
|
||||
//子クラス
|
||||
public class Cat extends Animal {
|
||||
public void Catprint () {
|
||||
|
||||
}
|
||||
}
|
||||
6
Question13-Extends/src/main/java/Dog.java
Normal file
6
Question13-Extends/src/main/java/Dog.java
Normal file
@@ -0,0 +1,6 @@
|
||||
//子クラス
|
||||
public class Dog extends Animal {
|
||||
public void Dogprint() {
|
||||
System.out.println(name + "はドックフードを食べました。");
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,19 @@ public class Question13_01 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO 自動生成されたメソッド・スタブ
|
||||
//Catクラスのインスタンス生成
|
||||
Cat cat = new Cat();
|
||||
cat.name = "タマ";
|
||||
|
||||
Dog dog = new Dog();
|
||||
dog.name = "ポチ";
|
||||
|
||||
cat.run();
|
||||
cat.eat();
|
||||
cat.Catprint();
|
||||
|
||||
dog.run();
|
||||
dog.Dogprint();
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user