Dark Horse day02 Static final interface keyword use in some of the considerations

1.Static key
(1). Static methods can not use this keyword, because the static object exists in preference to, when the static method to load may not have this subject

(2) static method can not directly call a non-static, you can call non-static way by creating an object
(3) subclasses can not override the parent class static method, but can inherit

class Fu{
    static int a=10;
    public static  void sleep(){
        System.out.println("睡觉");
    }
}

class Zi extends Fu{
    public void sleep(){
        //会报错
    }
}

2. Interface
(1) has a default public static final variable before the interface with the front public abstract method.
(2) multiple inheritance between the interface and the interface, can not be achieved only by inheritance.
Deflut modification method (3) may have an interface method thereof, can directly call subclass
(4) a subclass inherits the parent Fu Zi while achieving interfaces A, Zi without rewriting the interface class may be in the A All abstract methods, but the method does not override must be overridden in Fu class.
Added: Interface classes can not be inherited, it can only be achieved class.
3.final keywords
* (1) the final member variables can be modified assignment in the constructor method, but each constructor must change value assignment.
(2) a modified final classes can not have sub-classes, methods, and therefore can not be overwritten in the final

final class Fu{
    static int a=10;
    public static  void sleep(){
        System.out.println("睡觉");
    }
}

class Zi extends Fu{//会报错,无法继承被final修饰的类

}
Released eight original articles · won praise 3 · Views 113

Guess you like

Origin blog.csdn.net/weixin_43814245/article/details/105080043