面向对象的类建模 C#

3
我有两个不同的类,分别是CarRadio。 我该如何确保只有类Car可以使用类Radio? 或者这里有更好的实现方法吗? 我在用C#编写代码。
class Car
{
    // car has a radio
}  


class Radio
{
    // some properties

        // some methods
        void TurnOn(bool onOff) { }        
}

Thanks in advance.


3
你的意思是仅能被使用?你可以将Radio作为Car类中的私有类。 - Bob Vale
哈哈,所以你把指令写成了注释,另一个人把实现写成了答案。你得到了我的投票 :) - Shawn Mclean
@Shawn:完全可以接受的... - IAbstract
可能有一种“更好”的方法来做这件事,但我们需要了解更多信息。汽车只能使用原装收音机吗?新的收音机不能安装在您的汽车上吗?根据您的概念化方式,将确定最佳路线。 - Jamie Dixon
@Jamie - 两种方法都可以适用。我很乐意知道你如何实现你所说的后者。 - yonan2236
我不会将我的收音机类设为私有的。我会允许单独创建收音机,而不是依赖汽车。你有什么好理由不希望任何人创建一个收音机吗? - Jamie Dixon
7个回答

6
class Car {

  public Car() {
    myRadio = new Radio();
  }

  private class Radio {
    void TurnOn(bool onOff) {}
  }
}

4

一种方法是通过构造函数强制一个Radio必须属于一个Car的依赖关系:

class Radio
{
    Car owner;

    // constructor
    public Radio(Car car)
    {
        owner = car;
    }

    // some properties

    // some methods
    void TurnOn(bool onOff) { }        
}

另一种方式是将Radio对象嵌套在Car对象中。通常,这意味着不应该直接访问任何Car外的对象。通过嵌套,代码如下:

class Car 
{

    private class Radio 
    {
    }

    // add methods to affect the Radio object
}

1
class Car
{
    class Radio
    {
        // some properties

        // some methods
        void TurnOn(bool onOff) { }        
    }        // car has a radio
}  

这样就可以让除了Car类以外的任何类都看不到Radio类。现在只有在Car内部,才能控制它的开关。

0

如果你想在其他地方使用收音机,那么将其建模为逻辑模型可能是合适的。但如果你只想在汽车中使用收音机,那么它可能还不值得成为一个类,你可能过度设计了。如果你确实想限制收音机仅在汽车中可用,你可以在汽车类中声明收音机。这并不是最好的解决方案,但至少是一个答案 :P

public class Car
{
    class Radio
    {
        public void TurnOn()
        {
            // do stuff
        }
    }

    public Car()
    {
                    r = new Radio();
        r.TurnOn();
    }
}

public class NotCar
{
    // i cant use radio
}

0

鉴于汽车和收音机的概念(为什么其他人不能使用收音机),这是一个奇怪的问题,但答案是你可以将收音机作为嵌套类型使用,并设置为私有访问。

class Car
{ 
  Radio m_Radio = new Radio();
  private class Radio {...}
}

请注意,通过这种方式,您无法通过汽车公共接口将收音机暴露给外部世界,因此所有与Car.Radio的工作都必须通过Car类立即完成,这听起来对我来说不是很好的设计。

0

试试这个

class Car 
{
   class Radio 
   {
      void TurnOn(bool onOff) { } 
   }
}

类Radio将是私有的,只能由类Car访问


0

我已经阅读了上面所有的答案,都很好,但是我想尝试另一种方法来实现具有收音机的汽车,考虑到汽车可以安装通用收音机或老板收音机。我使用了接口依赖注入原则、多态性继承概念来实现这一点。在这种方法中,我们从IRadio接口实现了通用收音机和老板收音机。请参考以下代码片段。

namespace CarAndRadioProgramExample
{
    class Program
    {
        static void Main(string[] args)
        {
            GeneralRadio gRadio = new GeneralRadio();
            gRadio.MaximumVolume = 100;
            gRadio.MinimumVolume = 0;
            Car carWithGRadio = new Car(gRadio);
            carWithGRadio.SwitchOnCarRadio();

            BossRadio bRadio = new BossRadio();
            bRadio.MaximumVolume = 200;
            bRadio.MinimumVolume = 0;
            Car carWithBRadio = new Car(bRadio);
            carWithBRadio.SwitchOnCarRadio();

            Console.ReadLine();
        }
    }

    class Car
    {
        IRadio carRadio;
        public Car(IRadio radioObject)
        {
            carRadio = radioObject;
        }

        internal void SwitchOnCarRadio()
        {
            carRadio.RadioOn();
        }
    }

    interface IRadio
    {
        int MaximumVolume { get; set; }
        int MinimumVolume { get; set; }
        void RadioOn();
    }
   class GeneralRadio :IRadio
    {
        public int MaximumVolume
        {
            get;
            set;
        }
       public int MinimumVolume
        {
            get;
            set;
        }

        public void RadioOn()
        {
            Console.WriteLine("Switching On Generic Radio with maximum volume" + MaximumVolume + "and minimum volume" + MinimumVolume);
        }
    }

    class BossRadio : IRadio
    {
        public int MaximumVolume
        {
            get;
            set;
        }
        public int MinimumVolume
        {
            get;
            set;
        }

        public void RadioOn()
        {
            Console.WriteLine("Switching On Boss Radio with maximum volume" + MaximumVolume + "and minimum volume" + MinimumVolume);
        }
    }

}

运行上述代码时,输出将会是enter image description here

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接