xiao_Ai江桥

浅析构造函数,及public、private、protected、final、this、super关键字

 初学JAVA,感觉很多知识点不熟悉,看了好多遍教材,最终还是决定把它写下来,加深印象以便忘了的时候再过来复习一下。看上去字数可能比较多,其实内容很简明。

 首先看this的用法:

 1 package test;
 2 
 3 public class father {
 4 
 5     private String student1 = "xiaoAi ";
 6     private String student2 = "jiangQiao ";
 7     private int age1 = 25;
 8     private int age2 = 26;
 9     private int grade1 = 98;
10     private int grade2 = 96;
11 
12     protected father(String name, int age, int grade) {
13         String student1 = "occur error1 ";
14         String student2 = "occur error2 ";
15 
16         name = this.student1;
17         age = this.age1;
18         grade = this.grade1;
19         System.out.println(name + "is" + " " + age + " years old and get" + " " + grade);
20 
21         name = student2;
22         age = age2;
23         grade = grade2;
24         System.out.println(name + "is" + " " + age + " years old and get" + " " + grade);
25     }
26 
27     public static void main(String[] args) {
28         new father("cc", 25, 100);
29 
30     }
31 
32 }

 

 在father这个构造函数里,对已经初始化的变量再次赋值,student1用的是this.的方式,它的作用是在这个函数里面去引用外部的成员变量。student2则不是这样,因为在father函数中,student2被重新String并赋值,此时作为局部变量,在优先级上高于成员变量,它没有使用this关键字,所以输出的结果如图:

 super:

 1 package test;
 2 
 3 public class father {
 4 
 5     public String student = "xiaoXiao";
 6 
 7     protected father() {
 8 
 9     }
10 
11     // 测试this、以及private限定的范围、给出protected
12     protected father(String name, int age, int grade) {
13 
14     }
15 
16 }
 1 package test;
 2 
 3 public class remote extends father {
 4 
 5     String student = "occur error";
 6 
 7     protected remote(String name, int age, int grade) {
 8 
 9         name = super.student;
10         System.out.println("Here is " + name + " *** " + age + " *** " + grade);
11 
12     }
13 
14     public static void main(String[] args) {
15 
16         new remote("xiXi", 25, 100);
17 
18     }
19 }

 这里,remote类中初始化了student,但实际上它有从father继承的属性。此时如果我们通过构造函数16行new一个新的出来,那么它会执行第10行,打印信息为??

 

 Right anwser is "xiaoXiao" rather than "occur error"。因为super关键字可以从父类中获取到开放的成员变量,也就是说该变量不能被private所修饰。

 如果把使用this和super比作上楼梯台阶,this能做到一下一格,super则一下两格。

 protected:

 1 package test;
 2 
 3 public class father {
 4 
 5     public static void main(String[] args) {
 6         Remote rc = new Remote();
 7         rc.println();
 8         
 9     }
10 
11 }
 1 package test;
 2 
 3 public class Remote {
 4 
 5     private String exam = "you only have 30 minutes";
 6 
 7     protected void println() {
 8         System.out.println(exam);
 9     }
10 
11 }

 同一个package下面,可以成功调用。不同package就不可以了,系统会提示让你把protected换成public,即使是继承关系,也不可以。这样看来,protected确实起到了保护的作用,但不如private彻底。

 final:

 顾名思义有点“最后的”意思,因此这个理解起来好像更容易点,就是当final关键字修饰变量的时候,该变量必须初始化并且不能再被赋值,也就是怎么出现怎么离开。当它修饰类的时候,这个类就不能在被继承,感觉像是shutdown。

 最后关于构造函数,其实也已经涉及在前面的代码里面,我个人理解构造函数的使用看你的需求怎样。比如:

 

 

这里的构造函数是father()和father(String ,int , int );也就是当你father ff = new father();这么干的时候,可以决定是否在括号里输入参数,或输入怎样的参数来达到某种目的。当你任何构造函数都不写的时候,系统会默认一个无参构造函数。

写的有不对的地方烦请大家指正,共同进步。

posted on 2017-09-19 13:24  xiao_Ai江桥  阅读(820)  评论(0编辑  收藏  举报

导航