关键字总结
Java语法基础篇之关键字总结:
Java的关键字有很多,下面我们主要探讨
访问限定符:public protected private 和默认的访问限定。还有this,super ,final,static这几个比较神秘的关键字。
(一)首先我们来探讨访问限定符,我们分别从修饰类,属性,方法,构造方法这四点来讨论他们的用法。
修饰类时:
使用权限
Public: 在整个工程下面都可以使用使用被修饰的类
Protected: 再同一个包中或者在其子类中被修饰的类可以使用
默认的(即类前不加修饰符): 在一个包中可以被访问
Private: 外部类不能用private修饰,只有在内部类中才可以使用private修饰,只能在本类中使用
修饰属性和方法时:
Public: 在整个工程下面都可以使用
Protected: 再同一个包中或者在其子类中被修饰的方法或属性可以使用
默认的(即类前不加修饰符): 在一个包中可以被访问
Private: 外部类不能用private修饰,只有在内部类中才可以使用private修饰,只能在本类中使用
一下代码说明了修饰属性和方法时,再包内和包外访问权限的不同
代码:
以下为包内访问
package one; public class Father { public int numPublic; protected int numProtected; int numDefault; private int numPrivate; public Father() { numPublic=0; numProtected=1; numDefault=2; numPrivate=3; } public void putOut() { System.out.println("this is father public method!!"); } protected void putOut1() { System.out.println("this is father protected method!!"); } void putOut2() { System.out.println("this is father default method!!"); } private void putOut3() { System.out.println("this is father private method!!"); } } package one; public class Test { public static void main(String[] args) { Father father =new Father(); //访问public方法 father.putOut(); //访问protected方法 father.putOut1(); //访问默认方法 father.putOut2(); //访问public属性 System.out.println("numPublic : "+father.numPublic); //访问protected属性 System.out.println("numProtected : "+father.numProtected); //访问默认属性 System.out.println("numDefault : "+father.numDefault); //私有属性不能访问 System.out.println("numPrivate : "); } }
将Test类移到two这个包中时程序显示结果:
证明Protected: 再同一个包中或者在其子类中被修饰的方法或属性才可以使用
通过上下的对比, 说明了protected只能才包内访问(或其子类,子类中读者可以试一下),private只能在包中访问。
将包one中类Father的public去掉,会报错说明上述探究成立。
证明:默认的(即类前不加修饰符): 在一个包中才可以被访问
(二),下面我们讨论super和this。
This: 1.指本类当前正在被操作的的一个对象
2.在构造方法中调用本类的某一个构造方法(参数个数和顺序对应)
通常的用法为this.属性和this.方法
下面这样会输出什么结果呢?
结果为:
0
1
2
3
this is father public method!!
this is father protected method!!
this is father default method!!
this is father private method!!
因为this()调用了无参的构造函数,使得数据被初始化为0,1,2,3
假如这样呢?结果会是什么?
结果:
1
2
3
4
this is father public method!!
this is father protected method!!
this is father default method!!
this is father private method!!
因为这里我们把this()给屏蔽掉了,不能使用无参的构造函数,在有参的构造函数中把数据给赋值了
super:1.本类的父类对象(通常用在在子类中调用父类的方法)
2.在子类构造方法中调用父类的构造方法
3.在子类继承父类的方法时,也可以调用父类本方法
在使用super()时,必须在本函数的第一行
package one; public class Father { public int numPublic; protected int numProtected; int numDefault; private int numPrivate; public String name; public Father(int numPublic, int numProtected, int numDefault, String name) { // this(); // 本类默认对象调用属性 this.numPublic = numPublic; this.numProtected = numProtected; this.numDefault = numDefault; this.numPrivate = numPrivate; this.name = name; System.out.println(this.numPublic); System.out.println(this.numProtected); System.out.println(this.numDefault); System.out.println(this.name); // 本类默认对象调用方法 } public Father() { numPublic = 0; numProtected = 1; numDefault = 2; numPrivate = 3; } public void putOut() { System.out.println("this is father public method!!"); } protected void putOut1() { System.out.println("this is father protected method!!"); } void putOut2() { System.out.println("this is father default method!!"); } private void putOut3() { System.out.println("this is father private method!!"); } } package one; public class Son extends Father { private int num1; private int num2; private int num3; private int num4; private String name; public Son(int num1, int num2, int num3,String name ,int num4 ) { super(num1, num2, num3, name); this.num4 = num4; System.out.println("the num4 of son"+this.num4); } public static void main(String[] args) { new Son(1, 2, 3, "jake", 4); } } 输出的结果为: 1 2 3 jake the num4 of son4 因为
super(num1, num2, num3, name);这行代码调用了父类的构造方法,用父类的构造方法给变量赋值
<!--EndFragment-->
重点难点:static 静态修饰符
Static的作用:可修饰类 属性 方法 代码块(被修饰后的代码块我们又叫做静态块)。
下面我们讨论static的用法。
Static修饰类:一般情况下是不可以用static修饰类的。
如果一定要用static修饰类的话,通常static修饰的是内部类。被static修饰的内部类可以直接作为一个普通类来使用,而不需实例一个外部类。
请看代码:
public class One { public static void main(String[] args) { } public static class A { public A() { System.out.println("静态内部类的构造方法"); } public void putOut() { System.out.println("静态类的方法"); } } public class B { public B() { System.out.println("普通内部类"); } } } public class Test1 { public static void main(String[] args) { //static修饰内部静态类的用法 One.A testStatic = new A(); testStatic.putOut(); //普通内部类要用这种方法声明 One oneObject = new One(); B test2 = oneObject.new B(); } }
static 修饰属性 静态属性 在使用时不管在那里改变它的值,它的值都会被改变,这里涉及到静态装载的问题。在该类使用时,静态的方法,属性,代码块都会被放在一块静态空间。在对象创建之前,这些静态的属性方法代码块就已经在内存中存在。此属性都在同一个地址上因此无论是通过类名.属性,或者是对象名.属性,改变静态属性的值,都会使它的值改变。因此在使用静态变量时要谨慎。
Static 修饰方法:用static修饰的方法在调用时既可以使用对象+方法名,也可以使用类名+方法名。
代码:
public class One { //用static修饰属性 public static String name = "静态的变量"; // 用static修饰方法 public static void staticMethod() { System.out.println("静态的方法"); } } public class Test1 { public static void main(String[] args) { One oneObject = new One(); //static修饰属性和方法 System.out.println(One.name); //两种方式调用静态方法 oneObject.staticMethod(); One.staticMethod(); } }
static 修饰代码块 代码在进行类的装载的时候就会运行。我们在声明一个对象时静态块就会执行。而单纯的代码块,只有在为对象new出内存空间的时候(对象创建)才会执行。
public class One { static{ System.out.println("这是静态块"); } { System.out.println("这是默认的代码块"); } public static void main(String[] args) { } } public class Test1 { public static void main(String[] args) { One oneObject; } } 运行后出现的结果为: 这是默认的代码块 public class Test1 { public static void main(String[] args) { One oneObject = new One(); } } 运行后出现的结果为: 这是静态块
static总结:static之所以会有这样的特性,我们要从内存上和类的装载上进行理解。假如一个类中有被修饰的属性,方法,代码块,类会创建一个静态内存空间,将这些被静态的修饰的放进这个内存空间。而写该类的所有对象相应的属性,方法,代码块都会指向这片内存空间。
为了更好的理解static静态装载,下面我们来看两个程序
猜一猜输出的结果
package two; public class Frist { public Frist() { System.out.println("Frist"); } } package two; public class Second extends Frist{ public Second() { super(); System.out.println("Second"); } } package two; public class Third { public static Frist frist=new Frist(); public Second second=new Second(); static{ System.out.println("静态块of Third"); } public static void main(String[] args) { Second t=new Second(); } }结果: