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

问:抽象的(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

扫描二维码关注公众号,回复: 5586975 查看本文章

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

猜你喜欢

转载自blog.csdn.net/u012273935/article/details/79820647