zoukankan      html  css  js  c++  java
  • 5-1 继承 多态

    public class ValueTest9 extends ValueTest8 {
        
        public ValueTest9() {
        }
        
        public ValueTest9(int x) {
            super(x);
        }
    
    
        private int bonus = 2;
        
        
    
        public static void main(String[] args) {
            /**
             * 1.覆盖方法 不能访问父类的私有域,调用父类的方法 super 关键字
             * 
             * super和this不一样,super不是一个对象的引用,只是编译器调用超父类的关键字
             */
    //        ValueTest9 v1 = new ValueTest9();
    //        v1.getSalary();
    
        }
    
        public int getSalary() {
            System.out.println(this);
            System.out.println(super.getClass());
            int salary = super.getSalary();
            return salary + bonus;
        }
        
        
        /*
         * 3.子类构造器
         * 
         * 1)子类可以通过构造器访问父类的私有域对其初始化
         * 2)super关键字访问父类构造器,必须放在第一行
         * 3)子类构造器没有显示调用父类的构造器,默认调用无参构造器,父类没有无参构造器Java编译器会报错
         */
        
        
        /*
         * this 
         * 1)引用隐式参数
         * 2)调用本类其他的构造器
         * 
         * super
         * 1)调用父类的方法
         * 2)调用父类的构造器
         * 
         * 构造参数既可以传递给本类的构造器,也可以传递给父类
         * 
         * 调用构造器的语句必须在第一行
         * 
         */
        
        /*
         * 继承层次:
         * 一个公共的超类派生出所有类的集合
         * 继承链:路径
         * 
         */
        
        /*
         * 多态
         * 子类的对象赋值给父类
         * 
         */
        public void method1(){
            ValueTest8 v1 = null;
            v1 = new ValueTest9();
            v1 = new ValueTest8();
        }
        
        /*
         * 方法调用:
         * 1)找到子类和超类同方法名的方法
         * 2)匹配参数
         */
        
        
        /*
         * final:不允许扩展
         */
        //public final class ValueTest8 {
    }
  • 相关阅读:
    CNN comprehension
    Gradient Descent
    Various Optimization Algorithms For Training Neural Network
    gerrit workflow
    jenkins job配置脚本化
    Jenkins pipeline jobs隐式传参
    make words counter for image with the help of paddlehub model
    make words counter for image with the help of paddlehub model
    git push and gerrit code review
    image similarity
  • 原文地址:https://www.cnblogs.com/lxh520/p/8145870.html
Copyright © 2011-2022 走看看