Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

继承练习 #115

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 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,16 @@
package com.github.hcsp.inheritance;

public class Animal {
public String name;

public Animal(String name){
this.name=name;
}

public void sayMyName(String name){
System.out.println("我的名字是"+name);
}
public void call(String voice,String name){
System.out.println(voice+name);
}
}
24 changes: 17 additions & 7 deletions src/main/java/com/github/hcsp/inheritance/Cat.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
package com.github.hcsp.inheritance;

public class Cat {
private String name;
public class Cat extends Animal{
Cat(String name){
super(name);
}
// public void cat(String name,String voice){
// super.sayMyName(name);
// super.call(voice,name);
// }

public Cat(String name) {
this.name = name;
}
// private String name;
//
// public Cat(String name) {
// this.name = name;
// }

public void sayMyName() {
System.out.println("我的名字是" + name);
super.sayMyName(name);
}

public void meow() {
System.out.println("喵" + name);
super.call("喵",name);
}


}
21 changes: 14 additions & 7 deletions src/main/java/com/github/hcsp/inheritance/Dog.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
package com.github.hcsp.inheritance;

public class Dog {
private String name;

public Dog(String name) {
this.name = name;
public class Dog extends Animal {
public Dog(String name){
super(name);
}
// public void dog(String name,String voice){
// super.sayMyName(name);
// super.call(voice,name);
// }
// private String name;
//
// public Dog(String name) {
// this.name = name;
// }

public void sayMyName() {
System.out.println("我的名字是" + name);
super.sayMyName(name);
}

public void wang() {
System.out.println("汪" + name);
super.call("汪",name);
}
}
24 changes: 16 additions & 8 deletions src/main/java/com/github/hcsp/inheritance/Rat.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
package com.github.hcsp.inheritance;

public class Rat {
private String name;

public Rat(String name) {
this.name = name;
public class Rat extends Animal{
public Rat(String name){
super(name);
}

// public void rat(String name,String voice){
// super.sayMyName(name);
// super.call(voice,name);
// }
// private String name;
//
// public Rat(String name) {
// this.name = name;
// }
//
public void sayMyName() {
System.out.println("我的名字是" + name);
super.sayMyName(name);
}

public void zhizhi() {
System.out.println("吱吱" + name);
super.call("吱吱",name);

}
}