package com.gqc.service;
public abstract class Shopping {
//模板方法
public void buyGoods(){
userLogin();
buy();
pay();
}
//子类必须实现 (抽象方法)
public abstract void buy();
//子类不能重写的方法 (最终方法)
public final void userLogin() {
System.out.println("用户登录");
}
//钩子方法
public void pay() {
System.out.println("使用银联卡支付");
}
}
package com.gqc.service;
public class ShoesShopping extends Shopping {
@Override
public void buy() {
System.out.println("购买皮鞋");
}
}
package com.gqc.service;
public class ClothesShopping extends Shopping {
@Override
public void buy() {
System.out.println("购买男装");
}
@Override
public void pay() {
System.out.println("使用支付宝支付");
}
}
package com.gqc.test;
import com.gqc.service.ClothesShopping;
import com.gqc.service.ShoesShopping;
import com.gqc.service.Shopping;
public class MyTest {
/**
* @param args
*/
public static void main(String[] args) {
ShoesShopping ss=new ShoesShopping();
ss.buyGoods();
System.out.println("---------------");
Shopping cloShopping=new ClothesShopping();
cloShopping.buyGoods();
}
}
/*
用户登录
购买皮鞋
使用银联卡支付
---------------
用户登录
购买男装
使用支付宝支付
*/