以前学习过的定义数组方法是数据类型int  名称zsj [] =new int[3],如:int zsj[]=new int[3];这个是为数组开辟空间,大小为3
 还有个对象数组的定义方法是类名称 数组名称[] = new 类名称[长度] 如:Person per[] = new Person[3] ;开辟了三个空间大小的数组
那么接下来的代码中红色部分有个这样的定义,private Pet[] pets ;我是这样理解的,Pet是个接口,Pet[]是不是表示接口数组,然后定义了一个对象pets,是不是数组在接口中的定义就是 Pet[] pets ?跟以前的定义数组的方式都不一样了,想了好久都想不通,还是Pet[]是返回值类型呢??
interface Pet{ // 定义宠物接口
public String getName() ;
public String getColor() ;
public int getAge() ;
}
class Cat implements Pet{ // 猫是宠物,实现接口
private String name ; // 宠物名字
private String color ; // 宠物颜色
private int age ; // 宠物年龄
public Cat(String name,String color,int age){
this.setName(name) ;
this.setColor(color) ;
this.setAge(age) ;
}
public void setName(String name){
this.name = name ;
}
public void setColor(String color){
this.color = color;
}
public void setAge(int age){
this.age = age ;
}
public String getName(){
return this.name ;
}
public String getColor(){
return this.color ;
}
public int getAge(){
return this.age ;
}
};
class Dog implements Pet{ // 狗是宠物,实现接口
private String name ; // 宠物名字
private String color ; // 宠物颜色
private int age ; // 宠物年龄
public Dog(String name,String color,int age){
this.setName(name) ;
this.setColor(color) ;
this.setAge(age) ;
}
public void setName(String name){
this.name = name ;
}
public void setColor(String color){
this.color = color;
}
public void setAge(int age){
this.age = age ;
}
public String getName(){
return this.name ;
}
public String getColor(){
return this.color ;
}
public int getAge(){
return this.age ;
}
};
class PetShop{ // 宠物商店
private Pet[] pets ; // 保存一组宠物
private int foot ;
public PetShop(int len){
if(len>0){
this.pets = new Pet[len] ; // 开辟数组大小
}else{
this.pets = new Pet[1] ; // 至少开辟一个空间
}
}
public boolean add(Pet pet){ // 增加的是一个宠物
if(this.foot<this.pets.length){
this.pets[this.foot] = pet ; // 增加宠物
this.foot ++ ;
return true ;
}else{
return false ;
}
}
public Pet[] search(String keyWord){
// 应该确定有多少个宠物符合要求
Pet p[] = null ;
int count = 0 ; // 记录下会有多少个宠物符合查询结果
for(int i=0;i<this.pets.length;i++){
if(this.pets[i]!=null){ // 表示此位置有宠物
if(this.pets[i].getName().indexOf(keyWord)!=-1
||this.pets[i].getColor().indexOf(keyWord)!=-1){
count++ ; // 修改查找到的记录数
}
}
}
p = new Pet[count] ; // 开辟指定的大小空间
int f = 0 ; // 增加元素的位置标记
for(int i=0;i<this.pets.length;i++){
if(this.pets[i]!=null){ // 表示此位置有宠物
if(this.pets[i].getName().indexOf(keyWord)!=-1
||this.pets[i].getColor().indexOf(keyWord)!=-1){
p[f] = this.pets[i] ;
f++ ;
}
}
}
return p ; }
};
public class PetShopDemo{
public static void main(String args[]){
PetShop ps = new PetShop(5) ; // 五个宠物
ps.add(new Cat("白猫","白色的",2)) ; // 增加宠物,成功
ps.add(new Cat("黑猫","黑色的",3)) ; // 增加宠物,成功
ps.add(new Cat("花猫","花色的",3)) ; // 增加宠物,成功
ps.add(new Dog("拉步拉多","黄色的",3)) ; // 增加宠物,成功
ps.add(new Dog("金毛","金色的",2)) ; // 增加宠物,成功
ps.add(new Dog("黄狗","黑色的",2)) ; // 增加宠物,失败
print(ps.search("黑")) ;
}
public static void print(Pet p[]){
for(int i=0;i<p.length;i++){
if(p[i]!=null){
System.out.println(p[i].getName() + "," + p[i].getColor()
+"," + p[i].getAge()) ;
}
}
}
};

解决方案 »

  1.   

    private Pet[] pets ;这句的意思是:定义元素类型全部是Pet的数组。数组名是pets。
    你要明白,接口,类在Java中都是自定义类型。
      

  2.   

    也就是说:private Pet[] pets ;
    pets这个数组中,只能放Pet的实现类的对象。
      

  3.   

    private Pet[] pets ; // 保存一组宠物
    就表示定义了一个Pet的数组,里面可以放实现了Pet接口的所有类型。就这样。就像你定义一个int数组,这个数组里面就不能放double类型或者String类型
      

  4.   

    那如果不这样定义private Pet[] pets  
    换这样定义可以吗?private Pet pets[];这样可以吗,因为我记得定义普通数组那组括号[]可以放前面也可以放后面的
      

  5.   

    private Pet[] pets ; // 保存一组宠物
    就表示定义了一个Pet的数组,里面可以放实现了Pet接口的所有类型。就这样。就像你定义一个int数组,这个数组里面就不能放double类型或者String类型
      

  6.   

    可以,Java中的数组的[]的位置,只是习惯问题,都正确。