类类型作为方法返回值
抽象类类型作为方法返回值
接口类类型作为方法返回值
类类型作为方法返回值
返回值:返回的是该类的对象
1.创建该类对象
2.使用return语句返回该对象
抽象类类型作为方法返回值
返回值:该抽象类的子类对象
1.创建一个抽象类
2.新建一个类,继承抽象类,实现该抽象类的抽象方法
3.创建子类对象
4.使用return语句返回子类对象
接口类类型作为方法返回值
返回值:该接口的实现类对象
1.创建接口
2.新建类,实现接口,实现接口的抽象方法
3.创建实现类对象
4.使用return语句返回实现类对象
package DEMO2.DAY9_demo08_方法的返回值;
public class Demo {
public static void main(String[] args) {
//ctrl+alt+v:生成返回值
Inter inter = m3();
}
/**
* 类类型作为方法返回值
* 返回值:返回的是该类的对象
* 1.创建该类对象
* 2.使用return语句返回该对象
*/
public static Student m1(){
Student s = new Student();
return s;
//返回匿名对象
// return new Student();
}
/**
* 抽象类类型作为方法返回值
* 返回值:该抽象类的子类对象
* 1.创建一个抽象类
* 2.新建一个类,继承抽象类,实现该抽象类的抽象方法
* 3.创建子类对象
* 4.使用return语句返回子类对象
*/
public static Abstractclass m2(){
Zi z = new Zi();
return z;
//匿名内部类
// return new Abstractclass() {
// @Override
// public int hashCode() {
// return super.hashCode();
// }
// }
}
/**
* 接口类类型作为方法返回值
* 返回值:该接口的实现类对象
* 1.创建接口
* 2.新建类,实现接口,实现接口的抽象方法
* 3.创建实现类对象
* 4.使用return语句返回实现类对象
*/
public static Inter m3(){
ZiImpl i = new ZiImpl();
return i;
//匿名内部类
// return new Inter() {
// @Override
// public int hashCode() {
// return super.hashCode();
// }
// }
}
}
class Student{
String name = "王五";
}
abstract class Abstractclass{
String name = "赵六";
}
class Zi extends Abstractclass{
}
interface Inter{
String name = "田七";
}
class ZiImpl implements Inter{
}