继承练习题

  1. package com.hspedu.extend_.exercise;
    
    public class ExtendsExercise01 {
        public static void main(String[] args) {
            B b = new B();
    
        }
    }
    class A{
        A(){
            System.out.println("a");
        }
        A(String name){
            System.out.println("a name");
        }
    
    
    }
    class B extends A{
        B(){
            this("abc");//这里没有因为有this了
            //this或super都是在第一行才行。
            System.out.println("b");
        }
        B(String name){
            //注意这里有一个隐藏的super();会调用父类的无参构造器
            System.out.println("b name");
        }
    
    }
    /*
    输出
    a
    b name
    b
     */
    

    考点:有隐藏的super(),构造器中如果第一句没有this那么每个构造器第一句都有一个隐藏的super(),或者是自定义的一个有参的super(参数列表)。

  2. package com.hspedu.extend_.exercise;
    
    public class ExtendsExercise03 {
    
    }
    class Computer{
        private String CPU;
        private String storage;
        private String disc;
        //set
        //get
        public String getCPU() {
            return CPU;
        }
    
        public void setCPU(String CPU) {
            this.CPU = CPU;
        }
    
        public String getStorage() {
            return storage;
        }
    
        public void setStorage(String storage) {
            this.storage = storage;
        }
    
        public String getDisc() {
            return disc;
        }
    
        public void setDisc(String disc) {
            this.disc = disc;
        }
        //无参构造器
        public Computer() {
        }
        //有参构造器
        public Computer(String CPU, String storage, String disc) {
            this.CPU = CPU;
            this.storage = storage;
            this.disc = disc;
        }
    
        //先不用get和set方法,用直接修改的方法
        public String getDetails(){
            return "CPU:"+CPU+",内存:"+storage+",硬盘:"+disc;
        }
    
    }
    class PC extends Computer{
    
        private String brand;
        //get和set方法
        public String getBrand() {
            return brand;
        }
    
        public void setBrand(String brand) {
            this.brand = brand;
        }
    
        public PC() {
        }
    
        public PC(String brand) {
            this.brand = brand;
        }
    
        public PC(String CPU, String storage, String disc, String brand) {
            super(CPU, storage, disc);
            this.brand = brand;
        }
        public void printInfo(){
            System.out.println("PC信息:");
            System.out.println(getDetails()+",品牌:"+brand);
        }
    }
    class NotePad extends Computer{
    
        String color;
    
        public NotePad() {
        }
    
        public NotePad(String color) {
            this.color = color;
        }
    
        public NotePad(String CPU, String storage, String disc, String color) {
            super(CPU, storage, disc);
            this.color = color;
        }
        public void printInfo(){
            System.out.println("NotePad信息:");
            System.out.println(getDetails()+",颜色:"+color);
        }
    }
    class Test{
        public static void main(String[] args) {
            //String CPU, String storage, String disc, String brand
            PC pc = new PC("麒麟","8G","100G","苹果");
            //System.out.println(CPU);//不能直接访问private
            pc.printInfo();
            //String CPU, String storage, String disc, String color
            NotePad notePad = new NotePad("高通","16G","200G","黑色");
    
            notePad.printInfo();
    
    
        }
    }
    
    

    注意层级关系,无参构造器必须要有,因为子类的构造器中会有隐藏的super()调用父类的无参构造器,所以父类在编写有参的构造器的同时,也要显示的无参构造器以免报错

  3. super代表父类的引用,用于访问父类的属性,方法,构造器
    1. package com.hspedu.super_;
      
      public class A {
          public int n1 = 100;
          protected int n2 = 200;
          int n3 = 300;
          private int n4 = 400;
          //构造器
          public A() {
          }
          public A(String name){
              
          }
          public A(String name,int age){
              
          }
          //方法
          public void test100(){
              
          }
          protected void test200(){
              
          }
          void test300(){
              
          }
          private void test400(){
              
          }
      }
      
    2. package com.hspedu.super_;
      
      public class B extends A{
          //访问父类的属性,但不能访问父类的private属性
          public void hi(){
              System.out.println(super.n1+" "+super.n2+" "+super.n3);//n4不能被访问
          }
          //访问父类的方法,不能访问父类的private方法
          public void ok(){
              super.test100();
              super.test200();
              super.test300();
              //test400()是private不能被访问
          }
          //访问父类构造器:super(参数列表),只能放在构造器的第一句,只能出现一句
          public B(){
              //super();
             // super("jias");
              super("zhang",20);
          }
      }
      

      注意super调用属性,方法,构造器的语法。

    3. super使用细节,方法的查找

      1. 方法调用,cal(),这个会先从本类中查找,在去父类中查找,和this.cal()等价

      2. super.cal(),这个找cal()方法的步骤直接查找父类

      3. 当子类中有和父类中的成员(属性和方法)重名时,为了访问父类的成员,必须通过super。如果没有重名,使用super,this,直接访问是一样的

      4. 如果查找属性过程中,找到了但不能访问则报错。

      5. super的访问不限于直接父类,如果爷爷类和本类中有同名的成员,也可以使用super去访问爷爷类的成员;如果多个基类(上级类)中都有同名的成员,使用super访问遵循就近原则。A->B->C,当然也要遵循权限规则

    4. this和super区别

      1. 调用构造器:this调用本类构造器,必须放在构造器的首行。super调用父类构造器,必须放在子类构造器的首行

你可能感兴趣的:(java,jvm,servlet)