zoukankan      html  css  js  c++  java
  • 继承一个类或者是实现一个接口注意点

    当子类继承父类时,

                             new一个子类赋值给父类变量时,若父类中的方法,子类中也有时,调用的方法是子类的,否则,就是父类的。

                             同理, new一个子类赋值给父类接口变量时,若子类含有与接口父类重名的方法,则调用子类的方法。

                             父类变量不可以调用子类有,父类没有的子类方法,只能调用子类重写父类的方法。

    package com.wh.d3inherited;
    /**
     * @author 王恒
     * @datetime 2017年4月14日 下午3:07:23
     * @description
     *
     */
    public class TestStudent {
    
    	public static void main(String[] args) {
    		Person p=new Student();
    		System.out.println("---------------------以下 是Person声明,Student类new出来的对象");
    		p.info();
    		p.display();//调用的Persond的方法                   student重写了person的display方法
    		
    		
    		Person p2 = new Person();
    		System.out.println("---------------------以下 是Person声明,Person类new出来的对象");
    		p2.info();
    		p2.display();
    		
    		/**
    		 * 运行结果:
    		 *      
    		 *      ---------------------以下 是Person声明,Student类new出来的对象
    		 *      Person的info
    		 *      这是Student的常规display方法!!!
    		 *      ---------------------以下 是Person声明,Person类new出来的对象
    		 *      Person的info
    		 *      Person的display
    		 * 
    		 * 
    		 * 结论:创建一个对象时,若是由子类new出来的对象赋给父类变量
    		 */
    	}
    
    }
    

      

  • 相关阅读:
    ES6 随记(1)-- let 与 const
    this 机制的四种规则
    BEM(一种 CSS 命名规则)
    WebSocket 的后记
    WebSocket 初体验
    “空”的艺术-当数据为空时显示什么
    前端路由以及浏览器回退,hash & history & location
    体验 WebFont,网页上的艺术字
    dedecms安装全过程(集成环境)
    面向对象(五)
  • 原文地址:https://www.cnblogs.com/1020182600HENG/p/6709486.html
Copyright © 2011-2022 走看看