1. 程式人生 > >抽象方法用哪些修飾符修飾?

抽象方法用哪些修飾符修飾?

問:抽象的(abstract)方法是否可同時是靜態的(static),是否可同時是本地方法(native),是否可同時被synchronized修飾? 

答:

例項說明:

public abstract class Demo {

int a;
public  Demo(int a) {
this.a = a;
}

public abstract static void f1();  //報錯
public abstract native void f2();  //報錯

        public abstract synchronized void f3();  //報錯

        private abstract void f4();  //報錯

        abstract void f5();  //編譯通過

}

編譯報錯: 

The abstract method f1 in type Demo can only set a visibility modifier, one of public or protected

The abstract method f2 in type Demo can only set a visibility modifier, one of public or protected

The abstract method f3 in type Demo can only set a visibility modifier, one of public or protected

The abstract method f4 in type Demo can only set a visibility modifier, one of public or protected

abstract 方法只能用訪問修飾符 public 或者protected!

原文地址:https://blog.csdn.net/hz_lizx/article/details/54970797