教程书里说如果添加其他类直接在main函数后面添加,可是我自己试了一下不行,怎么回事?
public class Pets {
private String petsName;
public String getPetsName(){
return petsName;
}
public void setPetsName(String petsName){
this.petsName=petsName;
}
// 下面是构造方法
public Pets(){
}
public Pets(String petsName){
this.petsName=petsName;
}
public void eat(){ //声明eat变量
System.out.println(petsName+"eating foods");
}
public class Instrument{ //创建instrument类
private String instrmentName; //instrmentname 为私有
public String getInstrmentName(){
return instrmentName;
}
public void setInstrmentName(String InstrmentName){
this.instrmentName=instrmentName;
}
public Instrument(String instrmentName){
this.instrmentName=instrmentName;
}
public Instrument(){
}
public void play(){
System.out.println(instrmentName+"play ");
}
}
}
///////下面是Dog类
package bitch;
public class Dog extends Pets{
public Dog(String dogName){
// 调用父类的构造方法
super(dogName);
}
//覆盖父类的eat方法
public void eat(){
System.out.println(super.getPetsName()+"eat dogfoods");
}
}
///////下面是cobaya类
package bitch;
public class Cobaya extends Pets {
public Cobaya(String cobayaName){
//diaoyong fulei de gouzaofangfa
super(cobayaName);
}
// cover father's the way of eating
public void eat(){
System.out.println(super.getPetsName()+"eating vagetable");
}
}
//////下面是pettest类
package bitch;
public class PetsTest {
public void testEat(Pets pets){
pets.eat();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
PetsTest test=new PetsTest();
test.testEat(new Dog("jiwawa"));
test.testEat(new Cobaya("helanzhu"));
}
}