187-1 类名作为形参和返回值
类名作为形参和返回值 | |
类名作形参 | 其实需要的是该类的对象 |
类名作返回值 | 其实返回的是该类的对象 |
myArgs
itheima01
Cat
CatOperator,useCat(Cat c){…}
CatDemo,main,
--------------------------------------------------------------
package itheima01;
public class▄ Cat {
public void eat(){
System.out.println("187/猫吃鱼");
}
}
--------------------------------------------------------------
package itheima01;
public class CatDemo {
public static void main(String[] args) {
//创建操作类对象,调用方法
CatOperator co = new CatOperator();
Cat c = new Cat();
co.useCat(c);
Cat c2 = co.getCat();//new Cat()
c2.eat();
}
}
--------------------------------------------------------------
package itheima01;
public class CatOperator {
public void useCat(Cat c){//类名作形参,其实需要的是该类的对象
c.eat();
}
//返回类型不是void了,是Cat
public Cat getCat(){//类名作返回值,其实返回的是该类的对象
Cat c = new Cat();
return c;
}
}