final 关键字

xiaoxiao2021-02-28  7

class

public class ClassA { //指定成员的默认值 String name="defualt"; int n=100; //无参 但有默认值 的构造方法 //  使得创建出的实例有一些默认值// public ClassA() {// super();// this.name = "defualt";// this.n = 100;// }

}

finalstudy

public class FinalStudy { public static void main(String[] args) { /*---final 关键字---*/ //final 类型关键字     常变量 //  只能被初始化    后续只读// final double g = 9.8;// final double pi;// pi = 3.1415926; //final 和引用类型 //  引用本身 为final arr只能引用恒定的一个数组// final int[] arr = {1,2,3,4,5};// int[] arr2 = {2,3,4,5,6};// // arr[0] = -99;// System.out.println( arr[0]);//  // // Person obj = new Person();// Person obj2 = new Person();// System.out.println(obj.name +"   "+obj2.name);// System.out.println(obj.id +"   "+obj2.id);// System.out.println(obj.id2 +"   "+obj2.id2); Student stu = new Student(); stu.disp(); }

}

person

// final  写在class之前 表示当前类不允许被继承public  class Person { String name = "abcd";  int age; final String id;  //final 写在成员变量的前面表示  改属性只读 final String id2 = "002"; //缺点  所有id2 都一样逻辑不合适 public Person() { super(); id = Math.random()+" "; } //final *** 现在函数定义前  //  让该名字  只能代指当前函数 final void disp(){ System.out.println("disp!!!!"); }

}

student

转载请注明原文地址: https://www.6miu.com/read-2800265.html

最新回复(0)