qq_15290009
热呛
2015-11-30 02:23
采纳率: 100%
浏览 1.7k
已采纳

这种装修设计模式怎么写

图片说明
就像这样的

这种的设计模式,先写什么后写什么,
求大神帮我罗列书详细步骤图,我是个新手,
所以比较蒙,感谢各位大神了,或者有类似的代码也可以。
------------十分感谢你们的帮助

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • goawaycow1
    Longyhx 2015-11-30 09:52
    已采纳

    public abstract class Home
    {
    public string description = "空房子";
    public abstract string getDescription();

        public Double Area;
        public abstract Double getArea();
    }
    
    //房子
    public class House : Home
    {
        public House()
        {
            description = "MyHouse:";
        }
    
        public override string getDescription()
        {
            return description;
        }
    
        public override Double getArea()
        {
            return 0.0;
        }
    }
    
    //主卧
    public class MainCell : Home 
    {
        Home home;
    
        public MainCell(Home home,Double area)
        {
            this.home = home;
            this.Area = area;
        }
    
        public override string getDescription()
        {
            return this.home.getDescription() + " MainCell";
        }
    
        public override Double getArea()
        {
            return this.Area + home.getArea(); 
        }
    }
    
    //阳台
    public class Balcony : Home
    {
        Home home;
    
        public Balcony(Home home, Double area)
        {
            this.home = home;
            this.Area = area;
        }
    
        public override string getDescription()
        {
            return this.home.getDescription() + " Balcony";
        }
    
        public override Double getArea()
        {
            return this.Area + home.getArea();
        }
    }
    
    
    
    点赞 评论
  • goawaycow1
    Longyhx 2015-11-30 03:04

    采用装饰者模式,添加主卧装饰类,次卧装饰类,客厅装饰类,厨房装饰类,卫生间装饰类,阳台装饰类,根据需要进行装饰即可。

    点赞 评论
  • qq_15290009
    热呛 2015-11-30 06:48

    有没有人啊 这回答的具体一下子

    点赞 评论
  • u012814689
    乌溜_乌溜 2015-11-30 09:32

    ----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

    点赞 评论
  • goawaycow1
    Longyhx 2015-12-02 05:28
     public abstract class Home
        {
            public string description = "空房子";
            public abstract string getDescription();
    
            public Double Area;
            public abstract Double getArea();
        }
    
        //房子
        public class House : Home
        {
            public House()
            {
                description = "MyHouse:";
            }
    
            public override string getDescription()
            {
                return description;
            }
    
            public override Double getArea()
            {
                return 0.0;
            }
        }
    
        //主卧
        public class MainCell : Home 
        {
            Home home;
    
            public MainCell(Home home,Double area)
            {
                this.home = home;
                this.Area = area;
            }
    
            public override string getDescription()
            {
                return this.home.getDescription() + " MainCell";
            }
    
            public override Double getArea()
            {
                return this.Area + home.getArea(); 
            }
        }
    
        //阳台
        public class Balcony : Home
        {
            Home home;
    
            public Balcony(Home home, Double area)
            {
                this.home = home;
                this.Area = area;
            }
    
            public override string getDescription()
            {
                return this.home.getDescription() + " Balcony";
            }
    
            public override Double getArea()
            {
                return this.Area + home.getArea();
            }
        }
    
    //测试代码
    
    //创建一个空房子,初期面积0
    Home myHouse = new House();
    
    //在房子上装饰一个主卧
    myHouse = new MainCell(myHouse, 20);
    //在房子上装饰一个主卧
    myHouse = new MainCell(myHouse, 20);
    //在房子上装饰一个阳台
    myHouse = new Balcony(myHouse, 15);
    
    Console.WriteLine(myHouse.getDescription());
    Console.WriteLine("Area:" + myHouse.getArea());
    
    //输入结果
    MyHouse: MainCell MainCell Balcony
    Area:55
    
    点赞 评论

相关推荐