zoukankan      html  css  js  c++  java
  • JavaSE学习笔记(九)—— 形式参数和返回值的问题深入研究

    一、形式参数

      形式参数分为基本类型和引用类型。其中基本类型太简单,此处不做讲解。  

      引用类型的形式参数分为:类名、抽象类和接口三大类

    1.1 形式参数是类名

      当形式参数是类名(匿名对象的时候其实介绍过了),需要的是该类的对象

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

    1.2 形式参数是抽象类

      形式参数是抽象类时,需要的是该抽象类的子类对象

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

    1.3 形式参数是接口

      形式参数是接口时,需要的是该接口的实现类对象

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

    二、返回值类型

      返回值类型分为基本类型和引用类型。其中基本类型太简单,这里不做讲解。

      引用类型的返回值分为:类、抽象类和接口。

    2.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();
        }
    }
    
    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.2 返回值是抽象类

      返回值是抽象类时,返回的是该抽象类的子类对象。

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

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

    三、链式编程

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

    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();
        }
    }
  • 相关阅读:
    IE8及其以下浏览器边框圆角兼容问题
    关于git的一些指令及遇到的问题和解决方法
    vue项目环境搭建及运行
    webpack中的重要功能
    webpack 的重要功能
    c++ stl sort 自定义排序函数cmp要遵循 strict weak ordering
    spring boot 包jar运行
    windows上传文件到linux云服务器上
    最少硬币数目的问题
    leetcode 415 两个字符串相加
  • 原文地址:https://www.cnblogs.com/yft-javaNotes/p/10832942.html
Copyright © 2011-2022 走看看