JAVA重写不需要@override
一,如下代码,
package com.boot.enable.bootenable; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Component; import java.util.concurrent.TimeUnit; @Component("RunnableDemo") public class RunnableDemo implements Runnable { @Override @Async // 异步方式执行方法 public void run() { for(int i=0;i<5;i++) { System.out.println(i); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } } } }
package com.boot.enable.bootenable; import org.springframework.stereotype.Component; @Component("RunnableDemo2") public class RunnableDemo2 implements Runnable { public void run() { for (int i=6;i<10;i++) { System.out.println(i); } } }
package com.boot.enable.bootenable; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication public class BootEnableApplication { public static void main(String[] arg) { ConfigurableApplicationContext context = SpringApplication.run(BootEnableApplication.class); Runnable bean = context.getBean("RunnableDemo", Runnable.class); System.out.println("start"); bean.run(); System.out.println("end"); } }
二,可以运行,不写JAVA重写不需要@override,编译也是通过的,那个是注解,是给编译器看的,告诉编译器我下面写的代码是重写方法,让编译器检查,执行的时候不会执行注解代码