1.继承+重写+多态(用java写的例子)
苹果手机类:
功能: 发短信-(速度一般)
三星手机类:
功能:发短信-(速度快)
小米手机类:
功能:发短信-(速度慢)
...
人类:
数据:名字
功能:
用手机发短信
发短信过程(1)人拿着手机编写短信 2)手机发短信 3)人放下手机 )
手机:可以是(三星,苹果,小米)--》尝试多态解决
需求:
a)测试类中提供一个方法,根据参数“三星”,“苹果”,“小米”--》返回对应的手机对象。
b)熊大用三星手机发短信,熊二用苹果手机发短信
public class Test1 {
public static void main(String[] args) {
cellPhone p1=getcellphone("三星");
Person c1=new Person("熊大");
c1.usecellPhoneMessage(p1);
cellPhone p2=getcellphone("苹果");
Person c2=new Person("熊二");
c2.usecellPhoneMessage(p2);
}
public static cellPhone getcellphone(String type){
if("三星".equals(type)){
return new SanXingPhone();
}
if("苹果".equals(type)){
return new ApplePhone();
}
if("小米".equals(type)){
return new XiaoMiPhone();
}
return null;
}
}
class cellPhone{
public void sendMessgage(){
System.out.println("手机可以发短信,速度不知道如何");
}
}
class ApplePhone extends cellPhone{
public void sendMessgage(){
System.out.println("苹果手机发短信,速度一般");
}
}
class SanXingPhone extends cellPhone{
public void sendMessgage(){
System.out.println("三星手机发短信,速度快");
}
}
class XiaoMiPhone extends cellPhone{
public void sendMessgage(){
System.out.println("小米手机发短信,速度慢");
}
}
class Person{
String name;
public Person(String name){
super();
this.name=name;
}
public void usecellPhoneMessage(cellPhone ce){
System.out.println(this.name+"拿着手机发短信");
ce.sendMessgage();
System.out.println(this.name+"放下手机");
}
}