package cn.itcast.day20120727;
public class OopDemo1 {
/**
* @param args
*
* 面试题:用面向对象的 方式设计如下情景 。
* 两块石头磨成一把石刀,石刀可以砍树,看砍成木材,木材做成椅子
* 思路:
* 1 抽象加工场:方法:加工原材料
* 2 石刀:
* 元素:石头
* 方法1:砍树(石刀,树)
* 方法2:砍成木材 (石刀,木材)
* 3 木材:
* 元素:木材
* 方法:做成椅子
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str1 = "石头1";
String str2 = "石头2";
RockKnife rockKnife = new RockKnife(str1,str2);
Tree tree = rockKnife.cutTree(new Tree());
Wood wood = rockKnife.cutWood(new Wood());
Chair chair = new Chair(wood);
}
* @param args
*
* 面试题:用面向对象的 方式设计如下情景 。
* 两块石头磨成一把石刀,石刀可以砍树,看砍成木材,木材做成椅子
* 思路:
* 1 抽象加工场:方法:加工原材料
* 2 石刀:
* 元素:石头
* 方法1:砍树(石刀,树)
* 方法2:砍成木材 (石刀,木材)
* 3 木材:
* 元素:木材
* 方法:做成椅子
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String str1 = "石头1";
String str2 = "石头2";
RockKnife rockKnife = new RockKnife(str1,str2);
Tree tree = rockKnife.cutTree(new Tree());
Wood wood = rockKnife.cutWood(new Wood());
Chair chair = new Chair(wood);
}
}
//1 抽象加工场:方法:加工原材料
abstract class Machining
{
public abstract Object machining(String[] str);
}
//2 石刀:
class RockKnife extends Machining
{
// 元素:石头1,石头2,树
public String str1 ;
public String str2 ;
private Tree tree = new Tree();
private Wood wood = new Wood();
public String[] str ={str1,str2};//两块石头连在一起
public RockKnife(){}
public RockKnife(String str1, String str2) {
this.str1 = str1;
this.str2 = str2; //构造方法
machining(str);
}
public String getStr1() {
return str1;
}
public void setStr1(String str1) {
this.str1 = str1;
}
public String getStr2() {
return str2;
}
public void setStr2(String str2) {
this.str2 = str2;
}
public RockKnife machining(String[] str2) {
System.out.println("用两块石头从加工厂获得了一个石刀");
return new RockKnife();
}
//1 抽象加工场:方法:加工原材料
abstract class Machining
{
public abstract Object machining(String[] str);
}
//2 石刀:
class RockKnife extends Machining
{
// 元素:石头1,石头2,树
public String str1 ;
public String str2 ;
private Tree tree = new Tree();
private Wood wood = new Wood();
public String[] str ={str1,str2};//两块石头连在一起
public RockKnife(){}
public RockKnife(String str1, String str2) {
this.str1 = str1;
this.str2 = str2; //构造方法
machining(str);
}
public String getStr1() {
return str1;
}
public void setStr1(String str1) {
this.str1 = str1;
}
public String getStr2() {
return str2;
}
public void setStr2(String str2) {
this.str2 = str2;
}
public RockKnife machining(String[] str2) {
System.out.println("用两块石头从加工厂获得了一个石刀");
return new RockKnife();
}
// 方法1:砍树(石刀,树)
public Tree cutTree( Tree tree)
{
System.out.println("石刀砍树");
return tree;
}
// 方法2:砍成木材 (石刀,木材)
public Wood cutWood( Wood wood)
{
System.out.println("石刀砍木材");
return wood;
}
public Tree cutTree( Tree tree)
{
System.out.println("石刀砍树");
return tree;
}
// 方法2:砍成木材 (石刀,木材)
public Wood cutWood( Wood wood)
{
System.out.println("石刀砍木材");
return wood;
}
}
//3 木材:
class Chair
{
// 元素:木材
private Wood wood = new Wood();
public Chair(){}
public Chair(Wood wood){
this.wood = wood;
compose(wood);
}
public Chair compose(Wood wood)
{
// 方法:做成椅子
System.out.println("把木材做成椅子");
return new Chair();
}
//3 木材:
class Chair
{
// 元素:木材
private Wood wood = new Wood();
public Chair(){}
public Chair(Wood wood){
this.wood = wood;
compose(wood);
}
public Chair compose(Wood wood)
{
// 方法:做成椅子
System.out.println("把木材做成椅子");
return new Chair();
}
}
package cn.itcast.day20120727;
public class Wood {
public Wood(){}
}
public Wood(){}
}
package cn.itcast.day20120727;
public class Tree {
public Tree(){}
}
public Tree(){}
}