Maison  >  Article  >  Java  >  java常用关键字

java常用关键字

巴扎黑
巴扎黑original
2016-12-05 10:41:431271parcourir

常用的关键字:

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

                                                                   访问限定符的范围

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

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

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

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

 

          二、this和super         

                   this的用法:

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

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

                    super的用法:

                               super.属性     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修饰类:     表示这个类不能被继承

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

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

 

     四、static   

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

                                       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();  
          
          
    }  
  
}


Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn