更新内容2020/10/31
famliyname变量变为常量,放入people接口中
在mother类中添加私有内部类cook
增加children接口,son类实现children接口功能
son类增加静态内部类play
增加接口pet,在主方法里使用匿名内部类实现pet接口
public static void main(String[] args) {
father Jack = new father();
Jack.setName("Jack");
Jack.setAge(30);
Jack.show();
mother m = new mother();
m.setName("Jenny");
m.setAge(27);
m.show();
m.assesscook();
son s = new son();
s.setName("Tom");
s.setAge(7);
s.show();
s.school();
s.like();
son.play s1 = new son.play();
String petname = "mimi";
new pet() {
public void eat() {
System.out.println("eat");
}
public void shot() {
System.out.println(petname+" miaomiao!");
}
}.shot();
}
}
interface people{
String familyname = "Black";
void show();
}
class father implements people{
public father() {
super();
}
public father(String name, int age) {
super();
this.name = name;
this.age = age;
}
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void show() {
System.out.println("My name is "+name+" "+familyname);
System.out.println("I am "+age+" "+"olds");
}
}
class mother extends father{
public mother() {
super();
}
public mother(String name, int age) {
super(name, age);
}
private class cook{
public cook() {
super();
System.out.println("Are you hungry?:)");
}
}
public void assesscook() {
cook c = new cook();
}
}
interface children extends people{
void school();
void like();
}
class son extends mother
implements children{
public void school() {
System.out.println("primary school");
}
public void like() {
System.out.println("play computer games");
}
static class play{
public play() {
System.out.println("PS4,XBOX ONE,Nintendo switch");
}
}
}
interface pet{
void eat();
void shot();
}
运行结果`
My name is Jack Black
I am 30 olds
My name is Jenny Black
I am 27 olds
Are you hungry?:)
My name is Tom Black
I am 7 olds
primary school
play computer games
PS4,XBOX ONE,Nintendo switch
mimi miaomiao!