• 关于java基础中,接口里面父类的对象指向子类的引用



    父类的引用指向子类的对象,它只能看的到父类的那些方法~ 子类自身的方法看不到~~


    ·······························
    如:
    interface Singer { //定义了一个接口,它属于特殊的抽象类,
    方法不用去实现,叫它的子类去实现它的方法
    Public void sing();
    public void sleep();


    }


    class Student implements Singer {
    private String name;
    Student(String name) {
    this.name = name;
    }
    public void study {
    System.out.println("studying ,,,,");
    }
    public String getName() {
    return name;
    }


    public void sing() {
    System.out.println("student is sing");
    }
    public void sleep() {
    System.out.println("student is sleep");
    }
    }


    public class Test {
    public static void main(String[] args) {
    Singer s1 = new Student("le"); //父类的引用指向子类的对象
    s1.sing(); s1.sleep();
    }
    }


    ······························
    打印结果为:
    student is singing (分析:因为new的s1这个对象只能看到子类中 父类的方法,子类的方法是看不到的)
    即父类的引用指向子类的对象
    student is sleeping

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    php 数组
    条件语句if else ,switch ,while ,do.while
    if..else 判断中的 Boolean()转换
    wampserver 集成环境
    sublime text 安装及使用
    vue tab切换
    SVG 基础
    gitosis管理员的密钥丢失解决办法
    源码安装MySQL
    Xshell远程登录
  • 原文地址:https://www.cnblogs.com/mrcharles/p/4731769.html
Copyright © 2020-2023  润新知