zoukankan      html  css  js  c++  java
  • 继承

    当子类继承了某个类之后,便可以使用父类中的成员变量,但是并不是完全继承父类的所有成员变量。具体的原则如下:

    1)能够继承父类的public和protected成员变量;不能够继承父类的private成员变量;

    2)对于父类的包访问权限成员变量,如果子类和父类在同一个包下,则子类能够继承;否则,子类不能够继承;

    3)对于子类可以继承的父类成员变量,如果在子类中出现了同名称的成员变量,则会发生隐藏现象,即子类的成员变量会屏蔽掉父类的同名成员变量。如果要在子类中访问父类中同名成员变量,需要使用super关键字来进行引用

    static 方法和final方法不能被子类重写

    public class Father {
    
        int x = 50;
    
        public void hello() {
            System.out.println("父类  hello() ");
        }
    
        //子类也定义了a,和hi
        int a = 200;
    
        public  void hi() {
            //这里可以说明的是,方法是运行时绑定,属性是编译时绑定
            System.out.println("###"+this.getClass()); //实际类型是子类
            this.a =  300;
            System.out.println("父类   hi() "+ " , a = "+ this.a);  //编译时就绑定的类型是Father
        }
        
        
        public static void test() {
            
        }
        
        
    }
    public class Child extends Father {
    
        int a = 100;
    
        public  void hi() {
            System.out.println("子类 : a =  "+a);
        }
        
        public void foo() {
            super.hi();
            System.out.println("父类  : a = "+super.a);
        }
        
        
        public static void test() {
            
        }
        
        public static void main(String[] args) {
            //自身的属性,方法
            Child  child =  new Child();
            child.hi();                  
            System.out.println(child.a); 
            
            //继承的属性,方法, 
            child.hello();
            System.out.println(child.x); 
            
            //调用父类的属性和方法(super)
            child.foo();
            
        }
    }
  • 相关阅读:
    line
    同步fifo的verilogHDL设计实例
    在DE1-SOC上运行Linux
    DE1-SOC连接设定
    Tcl语言笔记之二
    Tcl语言笔记之一
    关于复位赋初值的问题
    Altera FPGA中的pin进一步说明
    Altera FPGA中的pin简介
    笔记之Cyclone IV第一卷第四章Cyclone IV器件中的嵌入式乘法器
  • 原文地址:https://www.cnblogs.com/moris5013/p/10921739.html
Copyright © 2011-2022 走看看