【问题标题】:How to structure the code to return certain members only如何构造代码以仅返回某些成员
【发布时间】:2021-01-13 20:49:00
【问题描述】:

我想问我如何构造代码,以便当用户(主程序)指示没有苹果(hasApple = false)时,用户将无法访问fruitMenu.ApplefruitMenu.ApplePrice

我想让程序在没有苹果的情况下运行,那么就不会有苹果和苹果属性的价格供用户选择。

using System;

namespace ConsoleApplication
{
    public class Menu
    {
        public class Fruit
        {
            public string Apple { get; set; }
            public double ApplePrice { get; set; }
            public string Orange { get; set; }
            public double OrangePrice { get; set; }

            // Constructor
            public Fruit(string apple, double applePrice, string orange, double orangePrice)
            {
                this.Apple = apple;
                this.ApplePrice = applePrice;
                this.Orange = orange;
                this.OrangePrice = orangePrice;
            }
        }

        public bool HasApple { get; set; }
        public bool HasOrange { get; set; }

        public Fruit GetMenu()
        {
            string Apple;
            double ApplePrice;
            if (HasApple)
            {
                Apple = "Apple";
                ApplePrice = 5;
            }
            else
            {
                Apple = "No Apple";
                ApplePrice = double.NaN;
            }

            string Orange;
            double OrangePrice;
            if (HasOrange)
            {
                Orange = "Orange";
                OrangePrice = 5;
            }
            else
            {
                Orange = "No Orange";
                OrangePrice = double.NaN;
            }

            Fruit allFruits = new Fruit(Apple, ApplePrice, Orange, OrangePrice);
            return allFruits;
        }

        // Constructor
        public Menu(bool hasApple, bool hasOrange)
        {
            this.HasApple = hasApple;
            this.HasOrange = hasOrange;
        }
    }

    public class Program
    {
        static void Main(string[] args)
        {
            Menu menu = new Menu(true, false);            
            Menu.Fruit fruitMenu = menu.GetMenu();
            Console.Write(fruitMenu.Apple);
            Console.Write("\t");
            Console.WriteLine(fruitMenu.ApplePrice);
            Console.Write(fruitMenu.Orange);
            Console.Write("\t");
            Console.WriteLine(fruitMenu.OrangePrice);
            Console.ReadKey();
        }
    }
}

【问题讨论】:

  • 设计缺陷。应该有两个 Fruit 具有 Name 和 Price 属性的实例,而不是一个 Fruit 具有 Apple 和 Orange 属性的实例。
  • 目前程序会输出Apple 5 No Orange NaN。第二行是因为您为hasOrange 指定了false。所以它基本上可以按您的意愿工作 - 除非您只是希望它在 hasOrange 为假时为空白?请说清楚。 (但是是的,我同意上述观点,您的设计并不是对 OOP 的最佳使用。)。
  • 我实际上想设计成这样,如果我将 hasOrange 指定为 false,GetMenu 只会为我返回与 Apple 相关的内容。
  • 因此,您可以先将Orange = "No Orange"; 更改为Orange = "";。或者,如果您愿意,请在决定向控制台输出什么之前查询 HasApple 和 HasOrange 属性。例如if (fruitMenu.HasOrange) { Console.Write(fruitMenu.Orange); Console.Write("\t"); Console.WriteLine(fruitMenu.OrangePrice); }.
  • 我明白了,非常感谢@ADyson 的建议!

标签: c# member


【解决方案1】:

一些组合起来的问题可以用下面的代码来表示。

我们应该尽可能让Fruit 独立于它的实现OrangeApple。由于它们没有额外的逻辑,我添加了字段Type

另外Menu 不应该真正关心里面放了什么。通过从中删除HasAppleHasOrange,代码变得更易于维护——当我们想要引入新类型的水果时,我们唯一需要在代码中更改的只是为水果名称添加新类型。

如果没有添加到菜单中,水果为空会限制对价格字段的访问

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication
{
    public class FruitNames
    {
        public const string Apple = "Apple";
        public const string Orange = "Orange";
    }

    public class Fruit
    {
        public string Type { get; set; }
        public double Price { get; set; }

        public Fruit(string type, double price)
        {
            Type = type;
            Price = price;
        }
    }
    public class Menu
    {
        private List<Fruit> items = new List<Fruit>();

        public IEnumerable<Fruit> GetMenu()
        {
            return items;
        }

        public void Add(Fruit fruit)
        {
            items.Add(fruit);
        }
    }

    public static class MenuExtensions
    {
        public static Menu WithApple(this Menu menu)
        {
            menu.Add(new Fruit(FruitNames.Apple, 5));

            return menu;
        }

        public static Menu WithOrange(this Menu menu)
        {
            menu.Add(new Fruit(FruitNames.Orange, 10));

            return menu;
        }
        public static Fruit GetFruitOrDefault(this Menu menu, string type)
        {
            return menu.GetMenu().FirstOrDefault(x => x.Type == type);
        }

        public static Fruit GetAppleOrDefault(this Menu menu)
        {
            return menu.GetFruitOrDefault(FruitNames.Apple);
        }

        public static Fruit GetOrangeOrDefault(this Menu menu)
        {
            return menu.GetFruitOrDefault(FruitNames.Orange);
        }
    }

    public class Program
    {
        static void PrintFruit(Fruit fruit)
        {
            Console.Write(fruit.Type);
            Console.Write("\t");
            Console.WriteLine(fruit.Price);
        }

        static void Main(string[] args)
        {
            Menu menu = new Menu();
            Console.WriteLine("Menu without fruits:");

            Fruit apple = menu.GetAppleOrDefault();
            if (apple != null)
            {
                PrintFruit(apple); // won't be printed
            }

            Fruit orange = menu.GetOrangeOrDefault();
            if (orange != null)
            {
                PrintFruit(orange); // won't be printed
            }

            Console.WriteLine("Menu with fruits:");
            menu = menu.WithApple().WithOrange(); // fill menu with items

            apple = menu.GetAppleOrDefault();
            if (apple != null)
            {
                PrintFruit(apple); // will be printed
            }

            orange = menu.GetOrangeOrDefault();
            if (orange != null)
            {
                PrintFruit(orange); // will be printed
            }

            Console.ReadKey();
        }
    }
}

【讨论】:

  • 不错!感谢@Yegor 提供详细的解决方案。
猜你喜欢
  • 1970-01-01
  • 2018-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多