对Java中关键字的认知

一、关键字定义:一些已经指定了含义的词语,即信息的集中化和概括化,方便用户的使用。
 下面我们就列举一些常见的关键字:
 (1)根类相关:public private protected  class interface implements extends import package
abstract

        (2)根方法函数/属性相关:void return static final


        (3)八大基本数据类型:int short byte long char boolean double float


       (4) 循环判断:for switch do while break continue if case 


        (5)异常:try catch throw throws finally


        (6)其他:this super


        (7)goto(保留字)
    二、关键字命名
      例如: swith(){//int/short/ String(JDK1.7版本以前不能用) 枚举类型
case **:break;
       }
       (1)在这里顺便说一下标识符的命名规则:
       1.首字母只能以字母,下划线,$开头,后面可以跟字母,下划线、美元符$、数字。
       2.标识符不能是关键字。
       3.标识符区分大小写
       4.标识符没有长度限制,但是不宜过长
       5.如果标识符有多个单词构成,那么从第二个单词开始,首字母大写
       6.标识符尽量命名有意义,让人望文生义
       7.尽量少用带$符号的标识符,不太习惯,还有内部类中,$具有特殊含义
       8.Java语言使用16-bit双字节字符编码标准(unicode字符集),最多可以识别65535个字符。建议标识符中最好使用ASCII字母,虽然中文标识符也能够正常的编译和执行,却不建议使用。
                   注意:true、false、null不能作为java标识符,因为编译器会会按照字面意思去解释它。
保留字可以做为标识符,但是最好不要用。
        (2)下面通过一个比较抽象的表格来说明访问权限修饰符的作用范围:     
                                  当前类               当前包           包外子类        包外类
public        可以                    可以                   可以              可以
protected     可以                    可以                    可以                
  默认                          可以                     可以
private       可以
          (3)下面我们用具体的实例来说明每一个访问权限修饰符的作用:
 1、    package 测试;                                                         
     public class Student{
public String name;
int age;
protected String dress;
private String sex;


}
package 测试;


public class Test {
public static void main(String[] args) {
Student st=new Student();
st.name="张三";
st.age=14;
st.dress="衡阳";
//报错,错误为The field Student.sex is not visible
    st.sex="男";
}
}           
通过观察这段代码我们发现   st.sex="男"语句会报错,报错的原因是 (The field Student.sex is not visible)即主函数
调用不到sex因为它是Student的私有属性,只能被它的当前类调用,主函数调用不了。
 2、package 测试;


public class UnStudent  extends Student {
public static void main(String[] args) {
UnStudent  un=new UnStudent();
un.age=12;
   un.dress="长沙";
   un.name="李四";
   //报错The field Student.sex is not visible
   un.sex="女";
   System.out.println(un.age);
   System.out.println(un.dress);
   System.out.println(un.name);
  
}


}
上面是我创建的Student的一个子类   UnStudent,正如大家所遇见的sex又报错(The field Student.sex is not visible),而其它三个
属性仍然没有错,并且当把un.sex="女";这条语句删除后程序的运行结果就是12 长沙 李四,这就充分的说明了public、protected、默认
这三个访问符号的访问范围都是作用于当前类和当前包。
3、下面在创建一个新包,进一步判断public、protected、默认的区别
package 测试1;


import 测试.Student;


public class UnStudent extends Student {
public void study(){
this.name="张三";
//报错,错误是The field Student.age is not visible
this.age=16;
this.dress="北京";
 //报错,错误是The field Student.sex is not visible
this.sex="男";


}
    
}
上述代码中sex与age均报错了分别是The field Student.sex is not visible和The field Student.age is not visible,这就表明
默认的访问范围不包括包外子类,而public与protect均可以。
4、从上述几个例子可知public与protect的作用范围大致相同,那么这是不是就意味着他们的作用范围一定是错的呢,答案显然是错的,下面
我们再通过一个例子证明我的推断。
package 测试1;


import 测试.Student;


public class Text {
public static void main(String[] args) {
Student sd=new Student();
sd.name = "张三";
//报错The field Student.age is not visible
sd.age = 20;
//报错The field Student.sex is not visible
sd.sex = "男";
//报错The field Student.dress is not visible
sd.dress = "上海";
}


}
根据上述程序可知age,sex,dress均出现报错,这就证明虽然public与protect的作用范围大致相同,但他们最大的不同就是public
能访问所有的包外类,而protect只能访问包外子类,访问范围相对于public来说更小。
(4)this\super
this:当前类的对象
super:父类的对象


普通方法和属性调用:this.方法名(),this.属性名
 this();
 super.方法名(),super.属性
 super();
1、this
package 测试;


public class Student {
public String name;
int age;
protected String dress;
private String sex;
public Student(){
this("n");
System.out.println("执行了第一个");
}
public Student(String n){
System.out.println("执行了第二个");
}
}
this是指当前对象自己,上述程序中的this就是对Student(String n)方法的调用,故执行的结果就是执行了第二个、
执行了第一个,因为this("n");先调用的Student(String n),故先输出执行了第二个。那么问题来了,如果我们把
this("n");放在Student(String n)构造方法的第一行又会发生什么呢,答案是此程序无法运行故我们要将this("n");
改为this();这时程序运行的结果就是执行了第一个,那这又会有一个问题,为什么执行了第二个没有输出了,因为它压根儿
就没有被this()调用,因为this()没有调用Student(String n)所需的形参。故事发展到这里,我想信大家的心理都有了一个
结论,那就是无论是什么构造函数,this()都要放在该构造函数的第一行。
2、super
package 测试1;


public class Student {
  public Student(String n){
//调用Object的构造方法
super();
}
       public void study(){
System.out.println("学生学习");
}
}
package 测试1;


public class UnStudent extends Student {
public UnStudent() {
//调用父类的对象
super("");
}
public void study() {
super.study();
System.out.println("大学生学习");
}
    
}
package 测试;


import 测试1.Student;


public class Test {
public static void main(String[] args) {
Student stu = new Student("");
        UnStudent us = new UnStudent("");
us.study();
}
}
super指的是在继承关系中,父类相对于子类就是super。和this()一样super也要放在每一个构造函数
的第一行,下面我们就来具体说一下super()在上述各个代码中所起到的作用。在Student类中,super()
起到的是调用当前类中方法的作用,而在UnStudent中super()起到的是调用分类中的方法的作用,故程序
最后的运行结果就是学生学习    大学生学习。
(5)final(最终的,最后的)
可以用来修饰:类、方法、属性、参数、局部变量
1、修饰类:当前类不能被继承
package 测试2;


public  final   class Student {
           public String name;
           protected int age;
           String dress;
           private String sex;
}
package 测试2;
//报错The type UnStudent cannot subclass the final class Student
public class UnStudent extends Student{


}
在这里我又用了一个新包来诠释final的作用,当上述程序中的Student前加上了final的关键词后,Student
就不能被UnStudent继承了。
2、方法:当前方法不能被重写
   package 测试2;


public    class Student {
           public String name;
           protected int age;
           String dress;
           private String sex;
public final void study(){
                }
}
package 测试2;


public class UnStudent extends Student{
//报错Cannot override the final method from Student
public  void study(){

}
}
显然UnStudent中的study()方法不能覆盖父类中的study()方法
3、属性:代表当前属性只能被赋值一次(常量)
      package 测试2;


public    class Student {
           //name报错The blank final field name may not have been initialized
           public  final String name;
           protected int age;
           String dress;
           private String sex;
public final void study(){
               
}

}
上述程序中name报错,说明name的定义方式又问题,当我们在定义name之前加上final时,我们应该
把name值变成一个常量,故正确的定义方式就是public  final String name="";这才是正确的定义方式。
4、参数/局部变量:代表当前参数在生命周期范围内不能重新赋值
        package 测试2;


public   class Student {
           public  String name;
           protected int age;
           String dress;
           private String sex;
           public Student(final String n){
        //调用Object的构造方法
        super();
        }




        public void study(){
        System.out.println("");
        }



}
package 测试2;


public class UnStudent extends Student{
public UnStudent(String n) {
super("");
// TODO Auto-generated constructor stub
}


public  void study(){
System.out.println("小学生学习");
}


}
package 测试2;


public class Tess {
public static void main(String[] args) {
Student st=new Student("");
UnStudent un=new UnStudent("");
un.study();

}
}
如上面的程序 public Student(final String n)构造方法参数前加了final,故当前参数在生命周期范围内不能重新赋值。


三、static(静态的):可以用来修饰:方法、属性、静态块(注意一点:静态方法中一定不能直接使用非静态属性或者方法)
package 测试3;


public class Student {
        public String name;
        public static String dress;
    //加载类的时候自动执行
        static{
        System.out.println( "执行静态块");
        }
        //创建类的对象的时候自动执行
        {
        System.out.println( "执行程序块");
        }
//        public void study(){
//        
//        }
//        public static void play(){
//        
//        }
}
package 测试3;


public class Tett {
public static void main(String[] args) {
Student stu1 = new Student();
Student stu2 = new Student();


// stu1.dress= "衡阳";
// stu2.dress = "株洲";
// Student.dress = "广州";
// System.out.println(stu1.dress);
// System.out.println(stu2.dress);
// System.out.println(Student.dress);
上述程序的运行结果是执行静态块 执行程序块 执行程序块,其中System.out.println( "执行静态块");一定会运行。
而System.out.println( "执行程序块");因为是要在创建类对象的时候执行,而主函数中创建了两次对象,故其执行
两次,假如主函数创建了三次或更多的对象,那么System.out.println( "执行程序块")也将执行相应的次数,下满让我们
将注释摘掉看会发生什么样的神奇的效果。


package 测试3;


public class Tett {
public static void main(String[] args) {
Student stu1 = new Student();
Student stu2 = new Student();


stu1.dress= "衡阳";
stu2.dress = "株洲";
Student.dress = "广州";
System.out.println(stu1.dress);
System.out.println(stu2.dress);
System.out.println(Student.dress);
现在大家看到的是摘除注释后的主函数,那么现在执行的话又会发生什么呢,答案就是程序的运行结果是执行静态块
执行程序块 执行程序块 广州  广州  广州,那么问题来了为甚摸会出现三个广州呢,主要是因为主函数的默认对象就是
Student,无论Student.dress = "广州";前面有多少个对象,只要最后一句是Student.dress = "广州";那么就执行
为“广州”,因为它将它前面的所有值都覆盖了。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值