2015-02-05 3 views
0

데코레이터 패턴을 구현하려고하는데 프로그램을 컴파일하려고 할 때 오류가 계속 발생합니다. 나는 이유를 알 수 없다. 나는 그것이 인터페이스가 아닌 것으로 무언가를 가지고 있다는 것을 알고 있지만, 나는 많은 변화를 시도했지만 아무 것도 효과가 없다. 어떤 도움을 주셔서 감사합니다! 당신은 메소드 선언의 기본 클래스를 지정할 필요가 없습니다데코레이터 패턴 : 실행되지 않음

#region ShirtsComponent Members 

    string ShirtsComponent.GetBrand() 
    { 
     return string.Format("{0}, {1}", fashion_Base.GetBrand(), _brand); 
    } 

    double ShirtsComponent.GetPrice() 
    { 
     return _price + fashion_Base.GetPrice(); 
    } 
    #endregion 

:

  using System; 
      using System.Collections.Generic; 
      using System.Linq; 
      using System.Text; 
      using System.Threading.Tasks; 

      namespace OODAssignment3_ZackDavidson 
      { 
      class Program 
      { 
      static void Main(string[] args) 
      { 
      //Creates a new Calvin Klein Shirt fordecoration 
      CalvinKlein ckShirt = new CalvinKlein(); 
      Console.WriteLine(Convert.ToString(ckShirt.GetBrand())); 

      //Puts a solid color on the Calvin Klein Shirt 
      solidColorDecorator ckSCD = new solidColorDecorator(ckShirt); 

      //Puts stripes on the Calvin Klein Shirt 
      stripedShirtDecorator ckSSD = new stripedShirtDecorator(ckShirt); 

      //Puts a pocket on the Clavin Klein Shirt 
      pocketShirtDecorator ckPSD = new pocketShirtDecorator(ckShirt); 

      //Creates a new Tommy Hilfiger Shirt 
      TommyHilfiger thShirt = new TommyHilfiger(); 

      //Puts stripes on the Tommy Hilfiger Shirt 
      stripedShirtDecorator thSSD = new stripedShirtDecorator(thShirt); 

      //Puts a pocket on the Tommy Hilfiger Shirt 
      pocketShirtDecorator thPSD = new pocketShirtDecorator(thShirt); 
     }//EndOfMain 
    }//EndOfClassProgram 

    public abstract class ShirtsComponent 
    { 
     public abstract string GetBrand(); 
     public abstract double GetPrice(); 
    } 

    class CalvinKlein : ShirtsComponent 
    { 
     private string ck_Brand = "Calvin Klein"; 
     private double ck_Price = 75.0; 

     public override string GetBrand() 
     { 
      return ck_Brand; 
     } 

     public override double GetPrice() 
     { 
      return ck_Price; 
     } 
    } 

    class TommyHilfiger : ShirtsComponent 
    { 
     private string th_Brand = "Tommy Hilfiger"; 
     private double th_price = 85.0; 

     public override string GetBrand() 
     { 
      return th_Brand; 
     } 

     public override double GetPrice() 
     { 
      return th_price; 
     } 
    } 

    public abstract class Decorator : ShirtsComponent 
    { 
     ShirtsComponent fashion_Base = null; 

     protected string _brand = "Undefined Decorator"; 
     protected double _price = 0.0; 

     protected Decorator(ShirtsComponent fashionBase) 
     { 
      fashion_Base = fashionBase; 
     } 

     #region ShirtsComponent Members 

     string ShirtsComponent.GetBrand() 
     { 
      return string.Format("{0}, {1}", fashion_Base.GetBrand(), _brand); 
     } 

     double ShirtsComponent.GetPrice() 
     { 
      return _price + fashion_Base.GetPrice(); 
     } 
     #endregion 
    } 

    class solidColorDecorator : Decorator 
    { 
     public solidColorDecorator(ShirtsComponent fashionBase) 
      : base(fashionBase) 
     { 
      this._brand = "Solid Color Shirt"; 
      this._price = 25.0; 
     } 
    } 

    class stripedShirtDecorator : Decorator 
    { 
     public stripedShirtDecorator(ShirtsComponent fashionBase) 
      : base(fashionBase) 
     { 
      this._brand = "Striped Shirt"; 
      this._price = 50.0; 
     } 
    } 

    class pocketShirtDecorator : Decorator 
    { 
     public pocketShirtDecorator(ShirtsComponent fashionBase) 
      : base(fashionBase) 
     { 
      this._brand = "Dotted Shirt"; 
      this._price = 90.0; 
     } 
    } 

}//EndOfNamespace 
+0

항상 적절한 언어 태그로 질문에 태그를 답니다. (예 : '날카로운') –

+0

감사합니다. 미안합니다. 오류는 다음과 같습니다 : 오류 명시 적 인터페이스 선언의 'IShirtsComponent'가 인터페이스가 아닙니다. ' –

답변

1

이 코드는 문제입니다. 올바른 코드는 다음과 같습니다.

#region ShirtsComponent Members 

    public override string GetBrand() 
    { 
     return string.Format("{0}, {1}", fashion_Base.GetBrand(), _brand); 
    } 

    public override double GetPrice() 
    { 
     return _price + fashion_Base.GetPrice(); 
    } 
    #endregion 
+0

#region 영역의 코드가 변경되었지만 여전히 오류가 발생합니다. 여전히 ShirtsComponent가 인터페이스가 아니라고 말합니다. –

+0

오류가 발생한 줄은 무엇입니까? –

+0

line 110..exactly 어디에서 기본 클래스를 제거 했습니까? –

관련 문제