设计一个摄影师的类,该类中只有一个方法,photograph方法,该方法有2个参数,第一个参数表示拍照的工具,第2个参数表示拍摄的对象。
拍照的工具,可以是手机,也可以是相机,也可以是平板,可以是数码相机,也可以是傻瓜式胶卷相机,(设置一个接口Photographable),
拍摄的对象可以是桌子,椅子,风景,人,花草等,(设置一个接口Target)
。
//class Target Photographable
//概念上的接口,系统对外提供的所有服务,类的所有能被外部使用者访问
//的方法构成了类的接口
//interface定义的接口,明确地描述了系统对外提供的所以服务,能够
//更清晰地把系统的实现细节和接口分离
public interface Photographable
{
// public void takePhoto();
public void takePhoto(Target target);
}
//class CellPhone
/**
手机父类,普通手机
*/
class CellPhone
{
protected String brand;
public CellPhone()
{
}
public CellPhone(String brand)
{
this.brand=brand;
}
public void call() //打电话
{
}
public void sendMessage() //发短信
{
}
public void receiveMessage() //收短信
{
}
}
//class Commonphone
class CommonPhone extends CellPhone
{
}
class SuperPhone extends CellPhone implements Photographable
{
public SuperPhone()
{
}
public SuperPhone(String brand)
{
super(brand);
}
public void takePhoto(Target target)
{
System.out.println("SuperPhone "+brand+" take photos of "+target.getOutlook());
}
}
//class Camera
public abstract class Camera implements Photographable
{
protected String brand;
public Camera()
{
}
public Camera(String brand)
{
this.brand=brand;
}
}
//class DigitalCamera
public class DigitalCamera extends Camera
{
public DigitalCamera()
{
}
public DigitalCamera(String brand)
{
super(brand);
}
public void takePhoto(Target target)
{
System.out.println("Digital Camera "+brand+" take photos of "+target.getOutlook());
}
public void ViewPhoto()
{
}
public void deletePhoto()
{
}
}
//class OpticalCamera
pubic class OpticalCamera extends Camera
{
public OpticalCamera()
{
}
public OpticalCamera(String brand)
{
super(brand);
}
public void takePhoto(Target target)
{
System.out.println("Optical Camera "+brand+"take photos of "+target.getOutlook());
}
}
//interface Target
public interface Target
{
public String getOutlook();
}
//class Person
class Person implements Target
{
public String getOutlook()
{
return "a very beautiful or handsome person";
}
}
//class Flower
class Flower implements Target
{
public String getOutlook()
{
return "a very beautiful flower";
}
}
//class Photographer
class Photographer
{
public void photograph(Photographable tool,Target target)
{
tool.takePhoto(target);
}
}
//class Demo04
class Demo04
{
public static void main(String args[])
{
Photographable ph01=new SuperPhone("HTC");
Photographable ph02=new DigitalCamera("Cannon");
Target target01=new Flower();
Target target02=new Person();
Photographer pher=new Photographer();
pher.photograph(ph01,target01);
pher.photograph(ph02,target02);
}
}