类名、抽象类名、接口作为返回值类型

它们都是引用类型。
要知道,抽象类、接口都不能实例化。

  • 类名作返回值类型:返回的是该类的对象
  • 抽象类名作返回值类型:返回的是该抽象类的子类对象
  • 接口名作返回值类型:返回的是该接口的实现类的对象

1、类名作返回值

class Student {
    public void study() {
        System.out.println("Good Good Study,Day Day Up");
    }
}

class StudentDemo {
    public Student getStudent() {
        //Student s = new Student();
        //Student ss = s;

        //Student s = new Student();
        //return s;
        return new Student();//返回该返回值类型Student类的对象
    }
}

class StudentTest2 {
    public static void main(String[] args) {
        //需求:我要使用Student类中的study()方法
        //但是,这一次我的要求是,不要直接创建Student的对象
        //让你使用StudentDemo帮你创建对象
        StudentDemo sd = new StudentDemo();
        Student s = sd.getStudent(); //new Student(); Student s = new Student();
        s.study();
    }
}

2、抽象类名作返回值类型

abstract class Person {
    public abstract void study();
}

class PersonDemo {
    public Person getPerson() {
        //Person p = new Student();
        //return p;

        return new Student(); //返回该抽象类Person的子类Student对象
    }
}

class Student extends Person {
    public void study() {
        System.out.println("Good Good Study,Day Day Up");
    }
}

class PersonTest2 {
    public static void main(String[] args) {
        //需求:我要测试Person类中的study()方法
        PersonDemo pd = new PersonDemo();
        Person p = pd.getPerson(); //new Student();  Person p = new Student(); 多态
        p.study();
    }
}

3、接口名作返回值类型

interface Love {
    public abstract void love();
}

class LoveDemo {
    public Love getLove() {
        //Love l = new Teacher();
        //return l;

        return new Teacher();//返回实现该接口的类的对象
    }
}

//定义具体类实现接口
class Teacher implements Love {
    public void love() {
        System.out.println("老师爱学生,爱Java,爱林青霞");
    }
}

class TeacherTest2 {
    public static void main(String[] args) {
        //如何测试呢?
        LoveDemo ld = new LoveDemo();
        Love l = ld.getLove(); //new Teacher(); Love l = new Teacher(); 多态
        l.love();
    }
}

猜你喜欢

转载自blog.csdn.net/u013317445/article/details/81782093
今日推荐