From 15c96f90ce15de40f98833e8f63898bc74a66f35 Mon Sep 17 00:00:00 2001 From: lee Date: Sun, 5 Dec 2021 18:56:07 +0000 Subject: [PATCH 1/2] constructor extensions from parent class. --- src/main/java/com/github/hcsp/inheritance/Cat.java | 8 +++----- src/main/java/com/github/hcsp/inheritance/Dog.java | 6 ++---- src/main/java/com/github/hcsp/inheritance/Rat.java | 6 ++---- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/github/hcsp/inheritance/Cat.java b/src/main/java/com/github/hcsp/inheritance/Cat.java index f86dc38..6f83491 100644 --- a/src/main/java/com/github/hcsp/inheritance/Cat.java +++ b/src/main/java/com/github/hcsp/inheritance/Cat.java @@ -1,16 +1,14 @@ package com.github.hcsp.inheritance; -public class Cat { +public class Cat extends Animal{ + private String name; public Cat(String name) { + super(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..08231fc 100644 --- a/src/main/java/com/github/hcsp/inheritance/Dog.java +++ b/src/main/java/com/github/hcsp/inheritance/Dog.java @@ -1,15 +1,13 @@ package com.github.hcsp.inheritance; -public class Dog { +public class Dog extends Animal { private String name; public Dog(String name) { + super(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..7b673fe 100644 --- a/src/main/java/com/github/hcsp/inheritance/Rat.java +++ b/src/main/java/com/github/hcsp/inheritance/Rat.java @@ -1,15 +1,13 @@ package com.github.hcsp.inheritance; -public class Rat { +public class Rat extends Animal { private String name; public Rat(String name) { + super(name); this.name = name; } - public void sayMyName() { - System.out.println("我的名字是" + name); - } public void zhizhi() { System.out.println("吱吱" + name); From 4710f413f9a2d6108e15adb2815a83080f0ca1c0 Mon Sep 17 00:00:00 2001 From: lee Date: Sun, 5 Dec 2021 19:18:34 +0000 Subject: [PATCH 2/2] constructor extensions from parent class. --- src/main/java/com/github/hcsp/inheritance/Cat.java | 2 +- src/main/java/com/github/hcsp/inheritance/Dog.java | 2 +- src/main/java/com/github/hcsp/inheritance/Rat.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/github/hcsp/inheritance/Cat.java b/src/main/java/com/github/hcsp/inheritance/Cat.java index 6f83491..b23f440 100644 --- a/src/main/java/com/github/hcsp/inheritance/Cat.java +++ b/src/main/java/com/github/hcsp/inheritance/Cat.java @@ -6,7 +6,7 @@ public class Cat extends Animal{ public Cat(String name) { super(name); - this.name = name; + } public void meow() { diff --git a/src/main/java/com/github/hcsp/inheritance/Dog.java b/src/main/java/com/github/hcsp/inheritance/Dog.java index 08231fc..691ad7c 100644 --- a/src/main/java/com/github/hcsp/inheritance/Dog.java +++ b/src/main/java/com/github/hcsp/inheritance/Dog.java @@ -5,7 +5,7 @@ public class Dog extends Animal { public Dog(String name) { super(name); - this.name = name; + } diff --git a/src/main/java/com/github/hcsp/inheritance/Rat.java b/src/main/java/com/github/hcsp/inheritance/Rat.java index 7b673fe..782afd1 100644 --- a/src/main/java/com/github/hcsp/inheritance/Rat.java +++ b/src/main/java/com/github/hcsp/inheritance/Rat.java @@ -5,7 +5,7 @@ public class Rat extends Animal { public Rat(String name) { super(name); - this.name = name; + }