package com.dava;

public class TesThread extends Thread implements Runnable {
public void run() {
System.out.println("运行成功!");
} /**
* @param args
*/
public static void main(String[] args) {
Thread t=new Thread(new TesThread());
t.start(); } }

运行结果:

运行成功!

程序继承了Thread 实现Runnable,接口的方法是需要全部实现的,而Runnable接口源码如下:

public
interface Runnable {
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see java.lang.Thread#run()
*/
public abstract void run();
}

所以有run();方法程序会正常执行.

单独继承Runnable方法:

public class ImpInterface implements Runnable {

    public void run() {
// TODO Auto-generated method stub }
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("123465");
} }

如果部实现run方法会出现异常。但是在上面的ji集成了Thread又实现了Runnable,可以部实现Run方法,程序会正常运行。

package com.dava;

public class TesThread extends Thread implements Runnable {
// public void run() {
// System.out.println("运行成功!");
// }
public void print() {
System.out.println("1231456"); }
/**
* @param args
*/
public static void main(String[] args) { // Thread t=new Thread(new TesThread());
// t.start();
new TesThread().print(); } }

运行结果:1231456

为什么不实现接口也行呢?看下图

Thread也实现了Runnable接口,实现了Run方法,子类默认继承了父类的方法,所以也实现了Runnable的run方法。

最新文章

  1. maven项目报:An error occurred while filtering resources
  2. prior knowledge
  3. tomcat错误:@HandlesTypes annotation of one or more ServletContentInitializers
  4. HDU1412
  5. WinForm(C#)CheckedlistBox绑定数据,并获得选中的值(ValueMember)和显示文本(DisplayMember)
  6. ASP.NET MVC学习之路由篇
  7. Linux系统环境变量的四个配置文件的关系
  8. WeChatAPI 开源系统架构详解
  9. Laravel 5.2 教程 - 数据填充
  10. CloseableHttpClient获取https请求不验证证书
  11. webpack 非严格模式设置 npm i babel-plugin-transform-remove-strict-mode
  12. 【转】python之配置日志的几种方式
  13. 弃 Java 而使用 Kotlin 的你后悔了吗?| kotlin将会是最好的开发语言
  14. shell 运算符
  15. poj1733(并查集+离散化)
  16. Effective Java 第三版——50. 必要时进行防御性拷贝
  17. shell 变量的数值计算实践 expr
  18. JVM 详解
  19. 2.5 Oracle之存储过程和MERGE INTO语句
  20. SQL数据是否存在(是否有数据)判断,表,存储过程是否存在

热门文章

  1. C++ traits技术浅谈
  2. screen printing
  3. C语言学习心得
  4. Jquery的学习:基础核心!
  5. tp框架 :操作数据库
  6. 算法模板——平衡树Treap 2
  7. 3891: [Usaco2014 Dec]Piggy Back
  8. 1724: [Usaco2006 Nov]Fence Repair 切割木板
  9. Node.js + React + MongoDB 实现 TodoList 单页应用
  10. ASP.NET MVC 创建控制器类过程