美文网首页java基础
java中的形式参数

java中的形式参数

作者: 小漫画ing | 来源:发表于2019-02-27 22:05 被阅读0次

    引用类型

    A:类
    需要的是该类的对象;
    B:抽象类
    需要的是该抽象的类子类对象;

    package cn.manman.com;
    abstract class Person1{
        public abstract void study();
    }
    
    class PersonDemo{
        public void method(Person1 person){
            person.study();
        }
    }
    //一个具体的学生类
    class Student extends Person1{
        public void study(){
            System.out.println("学习中!");
        }
    }
    public class xingcan {
        public static void main(String[] args) {
            //使用PersonDemo类中的method()方法
            //创建对象
            PersonDemo pDemo=new PersonDemo();
            //又因为抽象类不能实例化,但是Student继承了Person
            //所以可以间接实例化(多态的方法)
            Person1 p=new Student();
            pDemo.method(p);
        }
    }
    
    

    C:接口
    需要的是该接口的实现类对象

    package cn.manman.com;
    //定义一个爱好的接口
    interface  habbit{
        public abstract void love() ;
    }
    //
    class LoveDemo{
        public void method(habbit h){
            h.love();
        }
    }
    //定义具体类,实现接口
    class Teacher implements habbit{
        public void love(){
            System.out.println("爱学习!");
        }
    }
    public class xingcan1 {
        public static void main(String[] args) {
            LoveDemo loveDemo=new LoveDemo();
            habbit h=new Teacher();
            loveDemo.method(h);
        }
    }
    
    

    返回值类型

    A:基本类型
    B:引用类型:
    类:返回的是该类的对象;

    package cn.manman.com;
    class Student2{
        public void study(){
            System.out.println("好好学习!");
        }
    }
    class StudentDemo2{
        //看这里的返回值类型不对
        public Student2 getStudent(){//返回的是一个学生对象
            //Student s=new Student();
            //return s;
            //简化版就是如下:
            return new Student2();
        }
    }
    public class fanhuizhi_lei {
        public static void main(String[] args) {
            //使用student类中的study()
            //但是,要求是不能直接创建student的对象
            //使用studentDemo帮忙创建对象
            StudentDemo2 sDemo2=new StudentDemo2();
            Student2 s2=sDemo2.getStudent();
            s2.study();
        }
    }
    
    

    抽象类:返回的是抽象类的子类对象

    package cn.manman.com;
    //定义一个抽象类
    abstract class Person3{
        public abstract void study();
    }
    class Student3 extends Person3{
        public void study(){
            System.out.println("好好学习");
        }
    }
    class TestDemo{
        public Person3 getPerson3(){
            return new Student3();
        }
    }
    public class fanhuizhi_chouxianglei {
        public static void main(String[] args) {
            TestDemo tDemo=new TestDemo();
            Person3 person3=tDemo.getPerson3();
            person3.study();
            
        }
    }
    
    

    接口:返回的是该接口的实现类对象

    package cn.manman.com;
    interface Person4{
        public abstract void study();
    }
    class PersonDemo4{
        public Person4 getPerson4(){
             return new Teacher4();
        }
    }
    class Teacher4 implements Person4{
        public void study(){
            System.out.println("好好学习!!!");
        }
    }
    public class fanhuizhi_jiekou {
        public static void main(String[] args) {
            PersonDemo4 pDemo4=new PersonDemo4();
            Person4 person4=pDemo4.getPerson4();
            person4.study();
        }
    }
    
    

    相关文章

      网友评论

        本文标题:java中的形式参数

        本文链接:https://www.haomeiwen.com/subject/dzzfuqtx.html