第4节内部类

形参和返回值

  类名作为形参和返回值

  • 方法的形参是类名,其实需要的是该类的对象
  • 方法的返回值是类名,其实返回的是该类的对象
package itheima_18;

public class Cat {
    public void eat(){
        System.out.println("猫吃鱼");
    }
}
Cat
package itheima_18;
//猫操作类
public class CatOperator {
    public void useCat(Cat c){ //使用这个猫 Cat c=new Cat();
        c.eat();
    }

    public Cat getCat(){
        Cat c=new Cat();
        return c;
    }
}
package itheima_18;

/*
 * 测试类
 * */
public class CatDemo {
    public static void main(String[] args) {
        //创建操作类对象,并调用方法
        CatOperator co = new CatOperator();
        Cat c = new Cat();//创建Cat对象
        co.useCat(c);//把c传递给方法

        Cat c2 = co.getCat();//new Cat
        c2.eat();
    }
}

 

posted @ 2021-03-03 22:58  Testtext  阅读(37)  评论(0)    收藏  举报