Java关键字总结

来源:互联网 发布:手机上的编程软件 编辑:程序博客网 时间:2024/05/22 00:11

关键字的定义:编程语言里事先定义的,有特别意义的标识符,Java语言规定关键字不能作为标识符

目前共有50个Java关键字,其中,”const”和”goto”这两个关键字在Java语言中并没有具体含义

跟类相关的关键字:public private protected class package import
abstract interface implements extends
跟函数相关:void return break continue final
跟属性相关:int float double short long byte char boolean static
跟流程相关:if else for while do switch case default
其他:this super new try catch finally synchronized

1、 访问权限

public:当前工程的任何一个类可以随意访问
protected:同一个包下的所有类可以随意访问、不同包的子类可以继承到
默认的:同一个包下的所有类可以随意访问
private:当前自己类可以使用

类只能用:public以及默认的修饰

下面我们做一些测试:
首先,我们新建一个工程,在这个工程中,我们新建一个包,
然后,在这个包里,我们创建一个类为Student类

    public class Student{        public int num;        private int age;        int class;        protected int date;        public void Test(){        }    }

在同一个包中,我们在创建一个类为ProjectTest类:

    public class ProjectTest{        public void Test(){            Student student = new Student();            student.num = 10;            student.age = 20;            student.class = 30;            student.date = 40;        }    }

在编译上面这个程序的时候,我们不难发现,在date这个单词下面有个红浪线,说明,编译报错,原因就在,date是被定义为private的,所以不好被其他类访问。

下面我们在同一个工程下,在不同一个包中,我们再创建一个类为UniStudent类继承Student类:

    public class UniStudent extends Student{        public void Test(){            Student student = new Student();            student.num = 10;            student.age = 20;            student.class = 30;            student.date = 40;        }    }

在编译上面这个程序的时候,我们不难发现在num,age,class,date这几个单词下面都被标记了红色,说明这是编译不过去的,原因就在于age,class,date的第一个前提是在同一个包里,Student类和Teacher类都不在同一个包里,故而会报错。

我们再新建一个包,新建一个测试类:Test

    public class Test{        public static void main(String[] args){            Student student = new Student();            student.num = 10;            student.age = 20;            student.class = 30;            student.date = 40;        }    }

在编译上面这个程序的时候,我们不难发现在class和date这二个单词下面出现了红色标记,说明,这里编译报错,通过上面分析,我们也不能得出。

2、 this、super
this:当前类的对象(调用当前类的方法或者属性)
super:父类的对象(调用父类的方法或者属性)

调用属性:this.属性 super.属性
调用方法:普通方法:this.方法名() super.方法名()
构造方法:this() super() 注意:一定要写在第一行

下面我们通过实例来体现一下this和super的用法:
我们先创建二个类,学生类和大学生类:

    public class Student{        public String name;        pubic int age;        public void study(){            System.out.println(“学生在学习!”);        }       }
    public class UniStudent extends Student{        public void study(){            System.out.println(“大学生在学习!”);        }        public void Test(){            super.study();            this.study();        }    }

我们编译上面的程序,我们会发现在屏幕上打印了两句话:
学生在学习!
大学生在学习!
所以我们不难发现this和super的用法。

3、 final(最终的)

修饰的类型:类名、属性、方法、参数、局部变量
修饰类:不能被继承
修饰方法:不能被重写
修饰参数/属性/局部变量:在生命周期内不能被重新赋值
如果是引用的类型的变量,则地址不能更改,但是对象内部属性可以修改

4、 static(静态的)

修饰:方法、属性、内部类、静态块

静态块格式:
static{}

注意:静态方法不能直接有非静态属性,也不能直接调用非静态方法
程序在内存中加载的顺序:
1、静态属性 2、静态块 3、静态方法
4、非静态属性 5、构造方法 6、非静态方法

我们也可以用上面的例子来说明:
我们在一个包中定义两个类:

    public class Student{        public static int age = 1;        static{            System.out.println(1);        }        {        System.out.println(2);        }        public Student(){            System.out.println(3);        }        public static void play(){            System.out.println(4);        }    }   
    public class Test{        public static void main(String[] args){            Student stu = new Student();            Student stu1 = new Student();            stu.play();            stu1.play();            System.out.println(stu.age);            System.out.println(stu1.age);            System.out.println(Student.age);        }    }
0 0
原创粉丝点击