public class UserDTO{} class EmployeeService{} class StudentDAO{} class OrderItemEntity{} public class UserServiceImpl{} public class OrderItemController{}
public interface Closeable{} public interface Cloneable{} public interface Runnable{} public interface Comparable<T>{} public interface CompletionService<V>{} public interface Iterable<T>{} public interface EventListener{}
public class FileNotFoundException{} public class UserAlreadyExistException{} public class TransactionException{} public class ClassNotFoundException{} public class IllegalArgumentException{} public class IndexOutOfBoundsException{}
public abstract class VirtualMachineError{} public class StackOverflowError{} public class OutOfMemoryError{} public class IllegalAccessError{} public class NoClassDefFoundError{} public class NoSuchFieldError{} public class NoSuchMethodError{}
方法命名规范
表述获取
如果一个方法用于获取某个值,通常使用“get”作为其前缀
1 2 3
public String getUserName(){} public List<Integer> getUserIds(){} public User getOne(){}
表述查询
如果方法需要通过查询或筛选的方式获取某个数据,通常使用“find”/“query”作为其前缀
1 2 3
public List<Person> findOne(Integer id){} public List<Integer> findAll(){} public List<String> queryOrders(){}
表述条件
如果一个方法需要一些条件参数,则可以使用“by”/“with”等字符作为方法名中条件的连接符
1 2 3
public User findByUsername(String username){} public List<Integer> getUserIdsWithState(boolean state){} public List<User> findAllByUsernameOrderByIdDesc(String username){}
public static final String LOGIN_USER_SESSION_KEY = "current_login_user"; public static final int MAX_AGE_VALUE = 120; public static final int DEFAULT_PAGE_NO = 1; public static final long MAX_PAGE_SIZE = 1000; public static final boolean HAS_LICENSE = false; public static final boolean IS_CHECKED = false;
其它命名规范
数组命名
1 2 3 4 5
int[] array = new int[10]; int[] idArray ={1,2,3,4,5}; String[] nameArray = {"First","Yellow","Big"} public List<String> getNameById(Integer[] ids){} public List<String> getNameById(Integer...ids){}
public class HashSet<E> extends AbstractSet<E>{} public class HashMap<K,V> extends AbstractMap<K,V>{} public class ThreadLocal<T>{} public interface Functor<T,X extends Throwable>{ T val() throws X; } public class Container<K,V>{ private K key; private V value; Container(K key,V value){ this.key = key; this.value = value; } } public interface BaseRepository<T,ID>{ T findById(ID id); void update(T t); List<T> findByIds(ID...ids); } public static <T> List<T> methodName(Class<T> clz){ List<T> dataList = getByClz(clz); return dataList; }
接口实现类
在通常情况下,建议接口实现类使用“Impl作为后缀”,不建议使用大写的“I”作为接口前缀
1 2
public interface OrderService{} public class OrderServiceImpl implements OrderService{}
public class UserServiceTest{ public void testFindByUsernameAndPassword(){} public void testUsernameExist_notExist(){} public void testDeleteById_isOk(){} }