Java Road --- Day13

2019-10-28-22:40:14

table of Contents

  1. instanceof keyword

  2. Final Keyword

    2.1 Final Keyword modified class

    2.2 Final keyword modifications member method

    2.3 Final Keyword modified local variables

    2.4 Final Keyword modified member variable

  3. permission modifier


Instanceof keyword

  Role: Object judge cited a parent class is a subclass of what

  format:

    Instanceof class name object name   

. 1  Package demosummary.instanceoftest;
 2  
. 3  public  class the Test {
 . 4      public  static  void main (String [] args) {
 . 5          // Create a Dog object 
. 6          Animal Animal = new new Dog ();
 . 7          // subclass specific method if you want to call requires downcast
 8          // determine what parent class reference animal not originally Dog 
. 9          IF (animal the instanceof Dog) {
 10              animal.setName ( "fu" );
 . 11              Dog Dog =   (Dog) animal;
 12 is              dog.skill ();
 13         }
14 
15     }
16 }

Final Keyword

  Meaning: final keyword represents the final, irrevocable

  Common four kinds of usage:   

    1 can be used to modify a class
    2 can be used to modify a method,
    3 can also be used to modify a local variable
    4 can also be used to modify a member variable

  Final Keyword modified class

    format:

      public final class class name {

        Statement body;

      }

    Meaning: This class can not have any subclasses, and members of the methods can not be overwritten (because there is no sub-categories)

1 public final c1ass MyClass /*extends object*/ {
2     public void method() {
3         System.out.print1n( "方法执行!");
4     }
5 }

  Final keyword modifications member method

    当final关键宇用来修饰一个方法的时候,这个方法就是最终方法,也就是不能被覆盖重写。
    格式:
      修饰符 final 返回值类型 方法名称(参数列表) {
        // 方法体

      }

    注意事项:1、对于类、方法来说,abstract关键字和final关键字不能同时使用,因为矛盾。

public abstract class Fu {
    public final void method() {
        System.out.printIn("父类方法执行!");
    }
    public abstract /*final*/ void methodAbs() ; 
}

  Final关键字修饰局部变量

    作用:一旦使用final来修饰局部变量,那么这个变量就不能进行修改(一次赋值,终生不变)

    注意事项:

      1.对于基本类型来说,不可变说的是变量当中的数据不可改变

      2.对于引用类型来说,不可变说的是变量当中的地址值不可改变

  Final关键字修饰成员变量   

    作用:对于成员变量来说,如果使用final关键字修饰,那么这个变量也照样是不可变。
      1.由于成员变量具有默认值,所以用了final之后必须手动赋值,不会再给默认值了。
      2.对于final的成员变量,要么使用直接赋值,要么通过构造方法赋值。二者选其一。
      3.必须保证类当中所有重载的构造方法,都最终会对final的成员变量进行赋值。

权限修饰符

  Java中有四种权限修饰符:

          public > protected > ( default) > private

同一个类(我自己)  YES  YES    YES   YES

同一个包(我邻居)  YES  YES    YES   NO

不同包子类(我儿子) YES  YES    NO     NO

不同包非子类(陌生人 YES  NO      NO     NO

  注意事项: (default)并不是关键字“defoult",而是根本不写。

 

Guess you like

Origin www.cnblogs.com/hpcz190911/p/11756461.html