作业
使用eclipse关联 maven 创建 java项目 。
完成 单例模式的开发与测试
package com.ujiuye.homework;
public class A { private static A a = new A(); // 直接创建对象——饿汉式 private A(){ // 私有构造方法 }
public static A getInstance(){ return a; } public void run(){ System.out.println("running"); } }
|
package com.ujiuye.homework;
import org.junit.Test;
public class TestA {
@Test public void test1(){ A.getInstance().run(); }
} 注意:测试方法不可以写主方法,必须写一个测试方法,加上@Test,测试方法需要在pom.xml中导入jar包 |
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> |
输出结果