4-15形式参数和返回值类型是类,抽象类,接口

形式参数

/**
 * 形式参数:
 * 		形式参数是基本类型时,对实际参数没有影响
 * 		形式参数是引用类型时:
 * 						类(普通类)
 * 						抽象类
 * 						接口
 * */

形式参数是普通类

//形式参数是普通类时
class Student{
	public void study() {
		System.out.println("学生上课");
	}
}
class StudentDemo{
	public void method(Student s) {//此时形式参数是一个普通类
		s.study();
	}
}
public class Text1 {
	public static void main(String[] args) {
		StudentDemo sd = new StudentDemo();
		Student s = new Student();
		sd.method(s);
		new StudentDemo().method(new Student());//链式编程
	}
}

形式参数是抽象类

//形式参数是抽象类时
//创建一个具体类,通过具体类继承抽象类,来将抽象类实例化,即抽象类的子类,抽象类多态
abstract class Person2{//抽象类
	public abstract void work();
}
class Driver extends Person2{//继承抽象类的具体类

	@Override
	public void work() {
		System.out.println("司机会开车");
	}
	
}
class Person2Demo{
	public  void perso2Demo(Person2 p2) {//将抽象类作为形式参数
		p2.work();
	}
}
public class Text2 {
	public static void main(String[] args) {
		Person2 p2 = new Driver();
		Person2Demo pd = new Person2Demo();//抽象类多态
		pd.perso2Demo(p2);
	}
}

形式参数是接口时

//形式参数是接口时
//因为接口不能直接用来创建对象,所以我们要创建一个具体类去实现接口,通过接口多态创建对象
//这是接口作为形式参数才可以被使用
interface Smoking{//接口
	public abstract void smoking();
}
class Student3 implements Smoking{//实现接口的具体类

	@Override
	public void smoking() {
		System.out.println("学生抽烟");
	}
	
}
class Student3Demo{
	public void student3Demo(Smoking s) {//形式参数是接口
		s.smoking();
	}
}
public class Text3 {
	public static void main(String[] args) {
		Smoking s = new Student3();//接口多态
		Student3Demo sd = new Student3Demo();
		sd.student3Demo(s);
		Smoking sm = new Smoking() {//创建匿名对象,这种方式在实际开发中用的比较少
			@Override
			public void smoking() {
				System.out.println("学生抽烟2");
			}
			
		};
		sd.student3Demo(sm);
	}
}

返回值

/**
 * 返回值:
 * 		如果返回值基本类型:用对应的基本类型去接收数据即可!
 * 		引用类型:
 * 			类(具体类):	需要的是该类的对象
 * 			抽象类
 * 			接口
 */

返回值类型是普通类

/**
 * 返回值是普通类类型时,return返回的是一个对象
 * */
class Person4{
	public Student4 method() {
		Student4 s4 = new Student4();
		return s4;//返回一个对象
	}
}
class Student4{
	public void study() {
		System.out.println("学生爱学习");
	}
}
public class Text4 {
	public static void main(String[] args) {
		Person4 p4 = new Person4();
		Student4 ss = p4.method();
		ss.study();
		Student4 s4 = new Person4().method();
		s4.study();
	}
}

返回值类型是抽象类

//形式参数是抽象类类型时,需要创建一个子类继承抽象类,然后通过借接口多态使用
//中间方法虽然返回值类型时抽象类类型,但返回值是一个继承了该抽象类的具体类的对象
 abstract class Person5{
	public abstract void show5();
}
class Student5 extends Person5{

	@Override
	public void show5() {
		System.out.println("一个学生");
	}
	
}
class Student5Demo{
	public Person5 demo() {
		return new Student5();
	}
}
public class Text5 {
	public static void main(String[] args) {
		Student5Demo sd5 = new Student5Demo();
		Person5 p5 = sd5.demo();
		p5.show5();
		Person5 pp5 = new Student5Demo().demo();
		pp5.show5();
	}

返回值类型是接口

//接口作为返回值类型时,需要创建一个具体类去实现它,然后通过接口多态使用
//中间方法虽然返回值类型时接口类型,但返回值是一个实现该接口的对象
interface Inner{
	public void show6();
}
class Driver6 implements Inner{
	public void show6() {
		System.out.println("司机开车了");
	}
}
class DriverDemo{
	public Inner demo6() {
		return new Driver6();
	}
}
public class Text6 {
	public static void main(String[] args) {
		Inner i = new DriverDemo().demo6();
		i.show6();
		DriverDemo dd = new DriverDemo();
		Inner d6 = dd.demo6();
		d6.show6();
	}
}

猜你喜欢

转载自blog.csdn.net/ws1995_java/article/details/79991648
今日推荐