简体   繁体   中英

how to Implement this Abstract Class/properties logic by Interface in C#?

I have a abstract Class figures which contains Abstract properties and I am Overriding them in Derived class Rectangle and Square. Now, iI want to implement this with Interface. But I can't use constructor and neither I can't Declare the Variable inside the Interface . So, how to implement this using Interface where Figures Should be Interface and Square and Rectangle should be class ?

abstract class Figures
{
    int Width;
    int _cs;
    public Figures(int Width)
    {
        CS = Width;
    }

    public abstract int getarea
    {
        get;
    }
    public abstract int getperm
    {
        get;
    }
    public abstract int CS
    {
        set;
    }

    public abstract void display();
}

class Square : Figures
{
    int _CsS;
    public Square(int c) : base(c)
    {
    }

    public override int getarea
    {
        get
        {
            return (_CsS * _CsS);
        }
    }
    public override int getperm
    {
        get
        {
            return (2 * _CsS * _CsS);
        }
    }
    public override int CS
    {
        set
        {
            _CsS = value;
        }
    }

    public override void display()
    {
        Console.WriteLine("area={0} and perimeter={1}", getarea, getperm);
    }
}

class Rectangle : Figures
{
    int H;
    int _csr;
    public Rectangle(int H, int W) : base(W)
    {
        this.H = H;
    }

    public override int getarea
    {
        get
        {
            return H * _csr;
        }

    }
    public override int getperm
    {
        get
        {
            return 2 * H * _csr;
        }

    }
    public override int CS
    {
        set
        {
            _csr = value;
        }
    }

    public override void display()
    {
        Console.WriteLine("area={0} and perimeter={1}", getarea, getperm);
    }
}

You can do something like this:

interface IFigures
{

    int getarea
    {
        get;
    }
    int getperm
    {
        get;
    }
    int CS
    {
        set;
    }

    void display();
}

Thenk you can implement this interface from your classes and do your logic inside the class itself. So instead of putting the properties logic inside of your abstract class you will have to write them in your child classes.

class Square : IFigures
{
    int _CsS;
    public Square(int c) 
    {
         CS = c;
    }

    public int getarea
    {
        get
        {
            return (_CsS * _CsS);
        }
    }
    public int getperm
    {
        get
        {
            return (2 * _CsS * _CsS);
        }
    }
    public int CS
    {
        set
        {
            _CsS = value;
        }
    }

    public void display()
    {
        Console.WriteLine("area={0} and perimeter={1}", getarea, getperm);
    }

    //here you have implemented properties
}

so how to implement this using Interface

By definition, an interface won't let you implement anything. You can only specify things.

So you will have to remove the ctor and the fields from the interface IFigures and re-implement them in every class. You could reuse an implementation with a abstract class FiguresBase: IFigures but that's not always the best design.

It all depends on why you want the interface and how you will use it.

Your abstract class is a good thing. It lets you re-use code.

Interfaces (ie contracts) are also good if you want to achieve a loosely coupled system.

You can use abstract classes and interfaces together, to achieve both code reusability and loosely coupled parts.

public interface IFigures
{
  int getarea();
}
public abstract class Figures : IFigures
{
  public abstract int getarea();

  //opportunity for code reuse
  protected int getarea_internal()
  {
    throw new NotimplementedExcpetion();
  }
}

public class Square : Figures
public class Rectangle: Figures

here is the answer with class Diagram

Class Diagram of the Program

 interface IFigures
    {
        int Getarea
        {
            get;

        }
        int GetPerm
        {
            get;
        }
        int CS
        {
            //get;
            set;
        }
    }
  abstract class Figures:IFigures
    {
        int _Cs;
        public Figures( int _Cs)
        {
            CS = _Cs;
        }

        public abstract int Getarea
        {
            get;

        }
        public abstract int GetPerm
        {
            get;

        }
        public abstract int CS
        {
            //get;
            set;
        }
        public abstract void display();

    }
class Circle:Figures
    {
        int _r, _csc;
        public Circle(int _r):base(_r)
        {
            CS = _r;
        }

        public override int Getarea
        {
            get
            {
                return (_r * _r);
            }


        }
        public override int GetPerm
        {
            get
            {
                return (2* _csc * _csc);
            }


        }
        public override void display()
        {
            Console.WriteLine("area of Circle={0}", (_r * _r));
            Console.WriteLine("perimeter of rectangle={0}", (2 * _r * _r));
        }
        public override int CS
        {
            //get
            //{
            //    return _csc;
            //}

            set
            {
                _csc = value;
            }
        }

    }

class Rectangle:Figures
    {
        int _L, _csr;
        public Rectangle(int _L,int _W):base(_W)
        {
         this._L = _L;
            CS = _W;
        }
        public override int Getarea
        {
            get
            {
                return _csr * _L;
            }


        }
        public override int GetPerm
        {
            get
            {
                return (2* _csr * _L);

            }



        }
        public override void display()
        {
            Console.WriteLine("area of rectangle={0}", (_csr * _L));
            Console.WriteLine("perimeter of rectangle={0}", (2* _csr * _L));
        }
        public override int CS
        {
            //get
            //{
            //    return _csr;
            //}

            set
            {
                _csr = value;
            }
        }
}
class Program
    {
        static void Main(string[] args)
        {
            Figures f = new Rectangle(3, 4);
            f.display();
            //f.CS = 5;
            f.display();

            Console.ReadKey();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM