老王学java之一个让我困惑的问题

首先上代码:

import java.util.*;
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 ArrayList<Pet> search(String keyWord){
        ArrayList<Pet> pet=new ArrayList<Pet>();
        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){
                    pet.add(this.pets[i]);
                }
            }
        }
        return pet ;
    }
};
public class PetShopDemo_1{
    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(ArrayList<Pet> pet){
        for(int i=0;i<pet.size();i++)
        {
            System.out.println(pet.get(i).getName());
        }
    }
};

问题出在后面搜索结果的遍历上面:

这种方法正确,可以取得结果

 public static void print(ArrayList<Pet> pet){
        for(int i=0;i<pet.size();i++)
        {
            System.out.println(pet.get(i).getName());
        }
    }

而是用迭代器的这种方法却不管用

 public static void print(ArrayList<Pet> pet){
       Iterator it1=pet.iterator();
       while(it1.hasNext())
           { 
                System.out.println(it1.next().getName());
            }
    
    }    

报错,表示找不到getName(),但是直接输出it1.next()的结果和上面直接输出p[i]的结果是一样的,说明下面一种方法也是可以取得对象的地址的,至于为什么找不到getName()

方法,则就不明白了。我的想法是,It1已经转化成迭代器了,迭代器只能看到对象,看不到里面的东西。

因为it1.next()获得的是迭代器里面的内容。

原文地址:https://www.cnblogs.com/wanglei-134/p/3188807.html