`
wanghailiang333
  • 浏览: 195296 次
  • 性别: Icon_minigender_1
  • 来自: 广西
社区版块
存档分类
最新评论

Java Import 中的 static

 
阅读更多

以前比没怎么接触过Junit,项目需要所以现学一下

 

在用Junit时,发现一个调用本地方法的语句

 

代码如下

import static org.junit.Assert.*;
import org.junit.Test;

public class junitTest {
	@Test
	public void testCount() {
		fail(“...”);  //调用“本地”方法
	}
}
 奇怪我的junitTest类并没有继承Junit的类,但却可以使用fail方法

 

很快就会发现原来问题出在import语句中,

 

可以看到第一个import后边跟随着static

 

自然联想到后边import进来到类

 

源码:

public class Assert {
...
	/**
	 * Fails a test with the given message.
	 * 
	 * @param message
	 *            the identifying message for the {@link AssertionError} (<code>null</code>
	 *            okay)
	 * @see AssertionError
	 */
	static public void fail(String message) {
		throw new AssertionError(message == null ? "" : message);
	}

	/**
	 * Fails a test with no message.
	 */
	static public void fail() {
		fail(null);
	}
...
}
 现在明白原来是这么回事...你懂的...
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics