重载,一个java类里有多个同名的方法,但参数列表不同,见下面代码:
public class Caller {
/**
* 喊叫
*
* @param callMan
* 呼喊者
* @param callContent
* 呼喊内容
*/
public void call(String callMan, String callContent) {
System.out.println("I am " + callMan + ", I will call " + callContent);
}
/**
* 喊叫
*
* @param callContent
* 呼喊内容
*/
public void call(String callContent) {
System.out.println("I am hellokitty, I will call " + callContent);
}
}2. 重写和覆盖是一个概念,子类和父类有同名的方法名,且参数列表完全一致,在运行中子类的方法完全覆盖父类方法.
使用场景,系统要支持多种缓存机制,memcache,ehcache各实现一套方法
(1)定义接口 CacheService
public interface CacheService {
/**
* 缓存
*
* @param cacheKey
* 缓存key
* @param cacheContent
* 待缓存内容
*/
public void cache(String cacheKey, Object cacheContent);
}(2) EhcacheCacheImpl实现:
public class EhcacheCacheServiceImpl implements CacheService {
@Override
public void cache(String cacheKey, Object cacheContent) {
System.out.println("Cached by ehcache!");
}
}(3) MemcacheServiceImpl实现:
public class MemcacheServiceImpl implements CacheService {
@Override
public void cache(String cacheKey, Object cacheContent) {
System.out.println("Cached by memcache!");
}
}(4)测试
public class CacheTest {
public static void main(String[] args) {
CacheService cacheService1 = new EhcacheCacheServiceImpl();
cacheService1.cache("", "");
CacheService cacheService2 = new MemcacheServiceImpl();
cacheService2.cache("", "");
}
}
执行结果:
Cached by ehcache!
Cached by memcache!(5)如果结合Springs使用,可以通过修改配置文件的方式,灵活修改实现。
import org.springframework.beans.factory.annotation.Autowired;
public class CacheTest1 {
@Autowired
private CacheService cacheService;
private void cache(String cacheKey, Object cacheContent) {
cacheService.cache(cacheKey, cacheContent);
}
public static void main(String[] args) {
CacheTest1 newCacheTest = new CacheTest1();
newCacheTest.cache("", "");
}
}本文出自 “12314480” 博客,请务必保留此出处http://12324480.blog.51cto.com/12314480/1875421
原文:http://12324480.blog.51cto.com/12314480/1875421