Drink :
package Water;
public class Drink {
public void taste(){
}
}
Coco:
package Water;
public class Coco extends Drink {
@Override
public void taste() {
System.out.println("我是可口可乐,我是甜汽水");
}
}
SaltDrink:
package Water;
public class SaltDrink extends Drink {
@Override
public void taste() {
System.out.println("我是盐汽水,我是咸的");
}
}
Test:
package Water;
import java.util.Scanner;
import org.junit.Test;
public class jTest {
@Test
//方式3
public void test(){
sell("甜的");
}
private void sell(String good){
Drink drink=null;
if(good.equals("甜的")){
drink=new Coco();
}else if("咸的".equals(good)){
drink=new SaltDrink();
}
drink.taste();
}
//方式2
/* public void sell(){
Coco coco=new Coco();
SaltDrink st=new SaltDrink();
System.out.println("请输入:");
String name=new Scanner(System.in).next();
if(name.equals("甜的")){
coco.taste();
}else if(name.equals("咸的")){
st.taste();
}else{
System.out.println("输入错误!!!");
}
//方式1
Drink dk1=new Coco();
Drink dk2=new SaltDrink();
Sell sell1=new Sell();
sell1.sell(dk1);
Sell sell2=new Sell();
sell2.sell(dk2);
}*/
}