The use of keywords in abstract classes and interfaces

Static method: If you want to use static modification methods in an interface, you must have a method body (after java8); abstract methods of abstract classes cannot use static. In summary, in this case, can you use static in the method? Just think about whether the method can be accessed through the class name.

Static variable: The static variable used in the interface must be initialized, because the variable declared by the interface is public static final by default. The static variable of the abstract class can be initialized without being initialized (as a member variable by default)

abstract  class TestAbstract {
    static int i;
    static abstract void run();//报错,无法使用static
}
interface  TestInterface{
    static int i;//报错,需要初始化
    static void run();//报错,无方法体
}
class TASon extends  TestAbstract{
    public static void main(String[] args) {
        System.out.println(i);//结果为0
    }
}

Native method: Abstract methods cannot be modified by native methods. Native methods are methods implemented by native code (non-java code), which implies that these methods actually have method bodies, and abstract methods have not been implemented yet, so abstract methods cannot be both Realized and not realized...

public native int hashCode();//Object中的hashCode方法,虽然没法显式看到方法体,但是native却暗示了方法体的存在
abstract void run();//显然的指明了这个方法无实现体

Synchronized method: An abstract method cannot be explicitly declared as synchronized, but this does not mean that it cannot use synchronized (or it does not mean that it cannot achieve synchronization). As shown in the following method, you can see the abstraction The method can completely synchronize the implementation method of its own subclass (if synchronized can be explicitly declared as well), but this does not mean that the abstract method "should" be able to synchronize. It actually calls "unknown" code in the lock, which is very dangerous in itself. This is the secret of deadlock and so on. (In a word, it is unsafe? You have to let the writer see the method body to write synchronized?)

abstract class TestAbstract {
    public synchronized void m() {
        print();//如果abstract可以显式声明synchronized的话应该就是这种感觉吧
    }

    abstract void print();
    
    synchronized abstract void print2();//报错
}

Force the synchronization of abstract methods in abstract classes

abstract class TestAbstract {
    public synchronized void m() {
        print();
    }

    public abstract void print();
}

public class TASon extends TestAbstract {

    @Override
    public void print() {
        System.out.print("aaaaaaaaaa");
        System.out.print("bbbbb");
    }

    public void test() {
        new Thread() {
            @Override
            public void run() {
                while (true) {
                    m();
                }
            }
        }.start();
        new Thread() {
            @Override
            public void run() {
                while (true) {
                    m();
                }
            }
        }.start();
    }

    public static void main(String[] args) {
        new TASon().test();
    }
}

After calling the m method, you can try to call the print method again, and you can find that the latter is more a and less b (ignoring the results of the first and last parts of the console, the front is that the buffer is not enough, and the back is Is the result of forcibly stopping the operation)

I originally wanted to add @Test annotations to the test method. There are two problems. One is that the Test cannot be started if the class is not public, and the other is that the execution will stop after a while (even if I use an infinite loop, it is not resolved)

Guess you like

Origin blog.csdn.net/weixin_44463178/article/details/108785847