类名作为形参和返回值
类名作为形参
其实要的是该类的对象
public class Cat {
public void eat(){
System.out.println("cat eat fish");
}
}
/*
测试类
*/
public class CatDemo {
public static void main(String[] args) {
CatOperator co = new CatOperator();
Cat c = new Cat();
co.useCat(c);
}
}
public class CatOperator {
public void useCat(Cat c){ //Cat c = new Cat();
c.eat();
}
}
类名作为返回值
类名作为形参和类名作为返回值
public class Cat {
public void eat(){
System.out.println("cat eat fish");
}
}
/*
测试类
*/
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();
}
}
public class CatOperator {
public void useCat(Cat c){ //Cat c = new Cat();
c.eat();
}
public Cat getCat(){
Cat c = new Cat();
return (c);
}
}
抽象类名作为形参和返回值
public abstract class Animal {
public abstract void eat();
}
public class AnimalDemo {
public static void main(String[] args){
AnimalOperator ao = new AnimalOperator();
Animal a= new Cat();
ao.useAnimal(a);
Animal a2 = ao.getAnimal(); //new Cat();
a2.eat();
}
}
public class Cat extends Animal{
public void eat(){
System.out.println("猫吃鱼");
}
}
public class AnimalOperator {
public void useAnimal(Animal a){ //Animal a= new Cat();
a.eat();
}
public Animal getAnimal() {
Animal a = new Cat();
return a;
}
}