JAVA重写不需要@override
Posted 叶丶梓轩
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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,编译也是通过的,那个是注解,是给编译器看的,告诉编译器我下面写的代码是重写方法,让编译器检查,执行的时候不会执行注解代码
以上是关于JAVA重写不需要@override的主要内容,如果未能解决你的问题,请参考以下文章
Java 重写(Override)与重载(Overload)
Java 重写(Override)与重载(Overload)
Java 重写(Override)与重载(Overload)
Java 重写(Override)与重载(Overload)