首頁  >  文章  >  Java  >  java常用關鍵字

java常用關鍵字

巴扎黑
巴扎黑原創
2016-12-05 10:41:431271瀏覽

常用的關鍵字:

          一、存取限定詞:是用來修飾類別、屬性、方法、建構方法的

                                                  存取限定詞的範圍中

               可存取

                      2.protected     在同一類別中與同一個套件中以及不同包中的子類別  在同一類別中和同一個包裝中被存取

                      4. private         只能在同一類別中被存取

 

          二、this和super       

                                this.屬性      this.方法()            this(參數清單);     使用在構造方法符號第一行,表示某一類別的建構方法                                        哪一個建構方法取決於參數清單

                    super的用法:

            用來在子類別中表示父類別對象,呼叫父類別的方法

               super(參數清單); 用在子類別的建構方法中,用來呼叫父類別的某一個建構方法

的結構方法

 

This和super的使用代碼 

public class Student {  
      
    public String name;  
      
    public Student(){  
        // this用在构造方法的第一行  
        // 表示当前类的某一个构造方法  
        this("张三");  
    }  
    public Student(String name){  
        this.name = name;     
    }  
    public void study(){  
        System.out.println(name+"在休息");  
    }

Java 

public class UNStudent extends Student{  
      
    public UNStudent(){  
        //默认调用父类的无参构造方法  
        super();  
        System.out.println("UNStudent");  
    }  
    public void study(){  
        System.out.println(name+"在学习");  
    }  
}

主類代碼 

public class Main {  
    public static void main(String[] args) {  
          
                //创建Student类的对象  
        Student s = new Student();  
                //调用Student中的方法  
        s.study();  
          
                //创建UNStudent的对象  
        UNStudent u = new UNStudent();  
                //调用UNStudent中的方法  
        u.study();  
    }

         final的用法:用來修飾類別、變數、方法

               final修飾類別:表示這個類別不能繼承

               final修飾方法:表示這個方法不能被重寫[覆寫]

          inal   四、static   

             static的用法:可用來修飾類別、屬性、方法、程式碼區塊

                                     static屬性與成員屬性的差異:

                         static屬性[類別屬性]:

                                      類別中所有的物件共享         成員屬性 :

                                        static方法及成員方法的差異:                       

 

           

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

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

 

                                成员方法:

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

 

Java代码 

public class A {  
  
    public A() {  
        System.out.println("A");  
    }  
  
}

 

Java代码 

public class B {  
  
    public B() {  
        System.out.println("B");  
    }  
}

 

Static的使用代码 

public class Test {  
  
    //成员属性  
    public A a = new A();  
    //类属性  
    public static B b = new B();  
      
    //成员方法  
    public void change() {  
        System.out.println("change");  
    }  
      
    //类方法  
    public static void execute() {  
        System.out.println("execute");  
    }  
  
}

 

Java代码 

public class Demo {  
  
    public static void main(String[] args) {  
  
        //调用静态方法  
        Test.execute();  
  
        //调用成员方法需要对象  
        Test t = new Test();  
        t.change();  
          
          
    }  
  
}


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn