(面向对象) 形式参数和返回值的问题深入研究

目录

 

形式参数:

 引用类型为抽象类:

Test1:

Test2:

引用类型为类:

Test1:

Test2:

链式编程:每次调用完毕方法后,返回的是一个对象。

 引用类型为接口:

Test1:

Test2:


当一个对象被当作参数传递到一个方法后,此方法可改变这个对象的属性,并可返回变化后的结果,那么这里到底是值传递还是引用传递?
    答案:是值传递。Java 编程语言只由值传递参数。当一个对象实例作为一个参数被传递到方法中时,参数的值就是对该对象的引用。对象的内容可以在被调用的方法中改变,但对象的引用是永远不会改变的。

形式参数:

        基本类型(太简单,不是我今天要讲解的)
        引用类型
            类名:(匿名对象的时候其实我们已经讲过了)需要的是该类的对象
            抽象类:需要的是该抽象的类子类对象
            接口:需要的是该接口的实现类对象

 引用类型为抽象类:

Test1:

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

class PersonDemo {
	public void method(Person p) {//p; p = new Student();  Person p = new Student(); //多态
		p.study();
	}
}

//定义一个具体的学生类
class Student extends Person {
	public void study() {
		System.out.println("Good Good Study,Day Day Up");
	}
}

class PersonTest {
	public static void main(String[] args) {
		//目前是没有办法的使用的
		//因为抽象类没有对应的具体类
		//那么,我们就应该先定义一个具体类
		//需求:我要使用PersonDemo类中的method()方法
		PersonDemo pd = new PersonDemo();
		Person p = new Student();
		pd.method(p);
	}
}

Test2:

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

class PersonDemo {
	public Person getPerson() {
		//Person p = new Student();
		//return p;
		
		return new 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();
	}
}

引用类型为类:

Test1:

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

class StudentDemo {
	public void method(Student s) { //ss; ss = new Student();  Student s = new Student();
		s.study();
	}
}

class StudentTest {
	public static void main(String[] args) {
		//需求:我要测试Student类的study()方法
		Student s = new Student();
		s.study();
		System.out.println("----------------");
		
		//需求2:我要测试StudentDemo类中的method()方法
		StudentDemo sd = new StudentDemo();
		Student ss = new Student();
		sd.method(ss);
		System.out.println("----------------");
		
		//匿名对象用法
		new StudentDemo().method(new Student());
	}
}

Test2:

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();
	}
}

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();
	}
}

链式编程:每次调用完毕方法后,返回的是一个对象。

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

class StudentDemo {
	public Student getStudent() {
		return new Student();
	}
}

class StudentTest3 {
	public static void main(String[] args) {
		//如何调用的呢?
		StudentDemo sd = new StudentDemo();
		//Student s = sd.getStudent();
		//s.study();
		
		//大家注意了
		sd.getStudent().study();
	}
}

 引用类型为接口:

Test1:

//定义一个爱好的接口
interface Love {
	public abstract void love();
}

class LoveDemo {
	public void method(Love l) { //l; l = new Teacher();  Love l = new Teacher(); 多态
		l.love();
	}
}

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

class TeacherTest {
	public static void main(String[] args) {
		//需求:我要测试LoveDemo类中的love()方法
		LoveDemo ld = new LoveDemo();
		Love l = new Teacher();
		ld.method(l);
	}
}

Test2:

//定义一个爱好的接口
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();
	}
}

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值