diff --git a/src/main/java/com/github/hcsp/inheritance/Animal.java b/src/main/java/com/github/hcsp/inheritance/Animal.java new file mode 100644 index 0000000..5b8400c --- /dev/null +++ b/src/main/java/com/github/hcsp/inheritance/Animal.java @@ -0,0 +1,10 @@ +package com.github.hcsp.inheritance; + +public class Animal { + protected String name; + + public void sayMyName(){ + System.out.println("我的名字是"+name); + } + +} diff --git a/src/main/java/com/github/hcsp/inheritance/Cat.java b/src/main/java/com/github/hcsp/inheritance/Cat.java index f86dc38..6dc50fc 100644 --- a/src/main/java/com/github/hcsp/inheritance/Cat.java +++ b/src/main/java/com/github/hcsp/inheritance/Cat.java @@ -1,17 +1,14 @@ package com.github.hcsp.inheritance; -public class Cat { - private String name; +public class Cat extends Animal{ public Cat(String name) { this.name = name; } - public void sayMyName() { - System.out.println("我的名字是" + name); - } - public void meow() { System.out.println("喵" + name); } + + } diff --git a/src/main/java/com/github/hcsp/inheritance/Dog.java b/src/main/java/com/github/hcsp/inheritance/Dog.java index 24e713a..7e58fcd 100644 --- a/src/main/java/com/github/hcsp/inheritance/Dog.java +++ b/src/main/java/com/github/hcsp/inheritance/Dog.java @@ -1,16 +1,11 @@ package com.github.hcsp.inheritance; -public class Dog { - private String name; +public class Dog extends Animal { public Dog(String name) { this.name = name; } - public void sayMyName() { - System.out.println("我的名字是" + name); - } - public void wang() { System.out.println("汪" + name); } diff --git a/src/main/java/com/github/hcsp/inheritance/Rat.java b/src/main/java/com/github/hcsp/inheritance/Rat.java index bc517d1..04a2317 100644 --- a/src/main/java/com/github/hcsp/inheritance/Rat.java +++ b/src/main/java/com/github/hcsp/inheritance/Rat.java @@ -1,17 +1,15 @@ package com.github.hcsp.inheritance; -public class Rat { - private String name; +public class Rat extends Animal{ public Rat(String name) { this.name = name; } - public void sayMyName() { - System.out.println("我的名字是" + name); - } - public void zhizhi() { System.out.println("吱吱" + name); + } + + }