java常用关键字

常用的关键字:

          一、访问限定符:是用来对类、属性、方法、构造方法进行修饰的

                                                                   访问限定符的范围

                      1. public          在同一个程序中的任何地方都可以访问

                      2.protected     在同一个类中和同一个包中以及不同包中的子类中可以访问

                      3.默认不写     在同一个类中和同一个包中被访问

                      4.private         只能在同一个类中被访问

          二、this和super         

                   this的用法:

                                this.属性       this.方法();   用来表示当前对象

                                 this(参数列表);     用在构造方法符第一行,表示当前类的某一个构造方法                                                                       具体用在哪一个构造方法取决于参数列表

                    super的用法:

                               super.属性     super.方法();    用来在子类中表示父类对象,调用父类的方法

               super(参数列表); 用在子类的构造方法中,用来调用父类的某一个构造方法

                 具体的哪一个构造方法取决于参数列表

This和super的使用代码 
  1. public class Student {  
  2.       
  3.     public String name;  
  4.       
  5.     public Student(){  
  6.         // this用在构造方法的第一行  
  7.         // 表示当前类的某一个构造方法  
  8.         this("张三");  
  9.     }  
  10.     public Student(String name){  
  11.         this.name = name;     
  12.     }  
  13.     public void study(){  
  14.         System.out.println(name+"在休息");  
  15.     }  
Java代码 
  1. public class UNStudent extends Student{  
  2.       
  3.     public UNStudent(){  
  4.         //默认调用父类的无参构造方法  
  5.         super();  
  6.         System.out.println("UNStudent");  
  7.     }  
  8.     public void study(){  
  9.         System.out.println(name+"在学习");  
  10.     }  
  11. }
主类代码 
  1. public class Main {  
  2.     public static void main(String[] args) {  
  3.           
  4.                 //创建Student类的对象  
  5.         Student s = new Student();  
  6.                 //调用Student中的方法  
  7.         s.study();  
  8.           
  9.                 //创建UNStudent的对象  
  10.         UNStudent u = new UNStudent();  
  11.                 //调用UNStudent中的方法  
  12.         u.study();  
  13.     }  
  14.   

       三、final

               final的用法:用来修饰类、变量、方法

               final修饰类:     表示这个类不能被继承

               final修饰方法:表示这个方法不能被重写[覆盖]

               final修饰变量:表示这个变量不能被修改,且只能赋值一次

     四、static   

             static的用法:可以用来修饰类、属性、方法、代码块

                                       static的属性和方法是不需要创建对象来调用的

             static属性和成员属性的区别:

                         static属性[类属性]:

                                      该类的所有的对象共享的一个属性,只会占用一块内存空间

                           成员属性 :

                                      每一个对象都单独占用一块内存空间,必须通过对象才能调用

             static方法和成员方法的区别:                       

                               static方法[类方法]:

                                         类方法中不能再调用this和super表示对象

                                         类方法是调用父类的还是子类重写的只和类名有关

                                成员方法:

                                        成员方法是调用父类的还是子类重写的只和对象本身有关

Java代码 
  1. public class A {  
  2.   
  3.     public A() {  
  4.         System.out.println("A");  
  5.     }  
  6.   
  7. }  
Java代码 
  1. public class B {  
  2.   
  3.     public B() {  
  4.         System.out.println("B");  
  5.     }  
  6. }  
Static的使用代码 
  1. public class Test {  
  2.   
  3.     //成员属性  
  4.     public A a = new A();  
  5.     //类属性  
  6.     public static B b = new B();  
  7.       
  8.     //成员方法  
  9.     public void change() {  
  10.         System.out.println("change");  
  11.     }  
  12.       
  13.     //类方法  
  14.     public static void execute() {  
  15.         System.out.println("execute");  
  16.     }  
  17.   
  18. }  
Java代码 
  1. public class Demo {  
  2.   
  3.     public static void main(String[] args) {  
  4.   
  5.         //调用静态方法  
  6.         Test.execute();  
  7.   
  8.         //调用成员方法需要对象  
  9.         Test t = new Test();  
  10.         t.change();  
  11.           
  12.           
  13.     }  
  14.   
  15. }  

猜你喜欢

转载自731547255.iteye.com/blog/2342670