方法的返回值
基本数据类型:byte,short,int ,long,float,double,char,boolean
引用数据类型:
类:当类作为方法的返回值得的时候,实际上需要的是该类的对象
class Student3{
public void study(){
System.out.println("好好学习天天向上");
}
}
class StudentDemo3{
//今后你看到一个方法的返回值是一个类的类型的时候,
//实际上返回的是该类对象的地址值。
public Student3 fun(){
Student3 s = new Student3();
return s;
}
}
public class StudentTest2 {
public static void main(String[] args) {
StudentDemo3 studentDemo3 = new StudentDemo3();
Student3 s = studentDemo3.fun();
s.study();
}
}
抽象类:当抽象类作为方法的返回值得时候,实际上需要的是该抽象类的子类对象的
地址值,
abstract class Person3{
public abstract void study();
}
class Doctor extends Person3{
@Override
public void study() {
System.out.println("好好学习医术");
}
}
class PersonDemo3{
//今后当你看到一个方法的返回值是一个抽象类类型的时候
//需要返回的是该抽象类的子类对象
public Person3 fun(){
// Person3 person3 = new Person3();
// Person3 p = new Doctor();
// return p;
return new Doctor();
}
}
public class PersonTest2 {
public static void main(String[] args) {
PersonDemo3 personDemo3 = new PersonDemo3();
Person3 p = personDemo3.fun(); //Person3 p = new Doctor();
p.study();
}
}
接口:当接口作为方法的返回值的时候,实际上需要的是实现该接口的类对象的地址值;
interface PlayGame3{
public abstract void playLol();
}
class Teacher3 implements PlayGame3{
@Override
public void playLol() {
System.out.println("打英雄联盟");
}
}
class TeacherDemo3{
//当接口作为返回值类型的时候,需要的是实现该接口的类的对象
public PlayGame3 fun(){
return new Teacher3();
}
}
public class TeacherTest2 {
public static void main(String[] args) {
// TeacherDemo3 teacherDemo3 = new TeacherDemo3();
// PlayGame3 pg3 = teacherDemo3.fun();
// pg3.playLol();
//链式编程(今后你们所学的spark,flink都是这样的写代码方式)
//scala
new TeacherDemo3().fun().playLol();
}
}
数组:当数组作为方法的返回值得时候,实际上需要的是数组的地址值;形式参数
方法的形式参数:
基本数据类型:byte,short,int ,long,float,double,char,boolean
引用数据类型:
类:当类作为方法的形式参数的时候,实际上需要的是该类的对象
class Student{
public void study(){
System.out.println("好好学习天天向上");
}
}
class StudentDemo{
//但凡今后看到一个方法的参数是一个类的类型进行传参的时候
//实际上穿传的是该类的对象的地址值
public void fun(Student student){
student.study();
}
}
public class StudentTest {
public static void main(String[] args) {
StudentDemo studentDemo = new StudentDemo();
Student student = new Student();
studentDemo.fun(student);
}
}
抽象类:当抽象类作为方法的形式参数的时候,实际上需要的是该抽象类的子类实现
对象的地 址值,
abstract class Person {
public abstract void study();
}
class Student2 extends Person{
@Override
public void study() {
System.out.println("好好学习天天向上");
}
}
class PersonDemo {
//将来你看到当一个抽象类作为参数的类型的时候
//实际上需要的时候该类子类实现对象的地址值,利用多态的形式创建
public void fun(Person person) { //Person person = new Student2()
person.study();
}
}
public class PersonTest {
public static void main(String[] args) {
PersonDemo personDemo = new PersonDemo();
//抽象类不能被实例化
// Person person = new Person();
// Person p = new Student2();
// personDemo.fun(p);
personDemo.fun(new Student2());
}
}
接口:当接口作为方法的形式参数的时候,实际上需要的是该接口的实现类对象的
地址值,利用接口多态的方式创建;
interface Person2 {
public abstract void study();
}
class Teacher implements Person2{
@Override
public void study() {
System.out.println("好好学习天天赚钱");
}
}
class TeacherDemo{
//今后你看到一个方法的参数类型是一个接口的时候,
//实际上需要的是该接口的实现类对象的地址值,利用接口多态的方式创建
public void fun(Person2 person2){
person2.study();
}
}
public class TeacherTest {
public static void main(String[] args) {
TeacherDemo teacherDemo = new TeacherDemo();
// Person2 person2 = new Person2();
Person2 p = new Teacher();
teacherDemo.fun(p);
}
}
数组:当数组作为方法的形式参数的时候,实际上需要的是数组的地址值;