Skip to content

Commit

Permalink
添加一个父类来继承 (#247)
Browse files Browse the repository at this point in the history
添加一个父类来继承 (#247)
  • Loading branch information
ChangzhengH authored Nov 22, 2021
1 parent 6250f10 commit 1733d77
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 19 deletions.
9 changes: 9 additions & 0 deletions src/main/java/com/github/hcsp/inheritance/Animal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.github.hcsp.inheritance;

public class Animal {
protected String name;

public void sayMyName() {
System.out.println("我的名字是" + name);
}
}
7 changes: 1 addition & 6 deletions src/main/java/com/github/hcsp/inheritance/Cat.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
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);
}
Expand Down
7 changes: 1 addition & 6 deletions src/main/java/com/github/hcsp/inheritance/Dog.java
Original file line number Diff line number Diff line change
@@ -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);
}
Expand Down
9 changes: 2 additions & 7 deletions src/main/java/com/github/hcsp/inheritance/Rat.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
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);
super.name = name;
}

public void zhizhi() {
Expand Down

0 comments on commit 1733d77

Please sign in to comment.