• java super关键字


     子类用super操作被隐藏的成员变量和方法

    Example5_7.java

    class Sum { 
        int n;
        float f() { 
           float sum=0;
           for(int i=1;i<=n;i++)
              sum=sum+i;
           return sum;  
        }
    }
    class Average extends Sum {
        int n;  
        float f() { 
          float c;
          super.n=n;
          c=super.f();
          return c/n; 
        }
       float g() {
           float c;
           c=super.f();
           return c/2; 
       }
    }
    public class Example5_7 {
       public static void main(String args[]) {
           Average aver=new Average();
           aver.n=100;
          
           float resultTwo=aver.g(); float resultOne=aver.f();
           System.out.println("resultOne="+resultOne);
           System.out.println("resultTwo="+resultTwo);
       }
    }

    使用super调用父类的构造方法

    Example5_8.java

    class Student {
       int number;String name;
       Student() {
       }
       Student(int number,String name) {
          this.number=number;
          this.name=name;
          System.out.println("我的名字是:"+name+ "学号是:"+number);
       }
     }
    class UniverStudent extends Student {
       boolean 婚否;
       UniverStudent(int number,String name,boolean b) {
          super(number,name);
          婚否=b;
          System.out.println("婚否="+婚否);
       }
    }
    public class Example5_8 {
       public static void main(String args[]) {
          UniverStudent zhang=new UniverStudent(9901,"何晓林",false);
       }
    }
  • 相关阅读:
    总结!!!总结!!!
    Beta 总结
    BETA-7
    BETA-6
    BETA-5
    BETA-4
    BETA-3
    华为云-软件产品案例分析
    BETA-2
    BETA-1
  • 原文地址:https://www.cnblogs.com/yihujiu/p/5990589.html
Copyright © 2020-2023  润新知