其他分享
首页 > 其他分享> > 三十一、引用数据类型使用

三十一、引用数据类型使用

作者:互联网

1、概述

2、代码体现

/*
    1 分别定义含有Play,Mp3Player , IPod参数的方法,并调用传入实参进行测试
    2 定义一个学生类,里面定义含有Player, Mp3Player,IPod类型的成员变量,创建学生对象并给成员变量赋值
 */
public class Demo1 {
    public static void main(String[] args) {

//        usePlay(new IPod());
//        useMp3Player(new IPod());
//        useIPod(new IPod());

        Student s = new Student();
        s.setPlay(new IPod());// 0x001
        s.setMp3Player(new IPod());// 0x002
        s.setiPod(new IPod());// 0x003

    }
    // 方法的参数是一个接口 , 调用此方法需要传入此接口的实现类对象
    public static void usePlay(Play play) {
    }

    // 方法的参数是一个抽象类 , 调用此方法需要传入此抽象类的子类对象
    public static void useMp3Player(Mp3Player mp3Player) {
    }

    // 方法的参数是一个具体的类 , 调用此方法需要传入此类对象 , 或者此类的子类对象
    public static void useIPod(IPod iPod) {
    }
}

interface Play {
    public abstract void play();// 播放
}

abstract class Mp3Player implements Play {

}

class IPod extends Mp3Player {
    @Override
    public void play() {
        System.out.println("IPod播放音乐...");
    }
}


【student】
public class Student {
    private Play play;// 0x001
    private Mp3Player mp3Player;// 0x002
    private IPod iPod;// 0x003

    public Student() {
    }

    public Student(Play play, Mp3Player mp3Player, IPod iPod) {
        this.play = play;
        this.mp3Player = mp3Player;
        this.iPod = iPod;
    }

    public Play getPlay() {
        return play;
    }

    public void setPlay(Play play) {
        this.play = play;
    }

    public Mp3Player getMp3Player() {
        return mp3Player;
    }

    public void setMp3Player(Mp3Player mp3Player) {
        this.mp3Player = mp3Player;
    }

    public IPod getiPod() {
        return iPod;
    }

    public void setiPod(IPod iPod) {
        this.iPod = iPod;
    }
}

标签:play,IPod,数据类型,三十一,Mp3Player,void,引用,iPod,public
来源: https://www.cnblogs.com/6ovo6/p/14933323.html