简体   繁体   中英

C# inheritance classes

I am trying to solve a task, but I don't get how to create Add class so that the program works correctly.

I have checked many posts, but haven't found a solution. I try to own public for Number, Text, .... but in Visual Studio tips always ask to create public int new Number in class Class : Add .

I know I should create a list where I can add it text and number then if number then parse...or should I use indexers?

class Add
{
    //...............
}

class Class : Add
{
    public int Number { get; set; }
    public string Text { get; set; }
    public int NumberPlusOne()
    {
        return Number + 1;
    }
    public void Print()
    {
        Console.WriteLine("Number is " + Number);
        Console.WriteLine("Text is " + Text);
    }
}

class Program
{
    static void Main()
    {
        Add add = new Class();
        add.Number = 123;
        add.Text = "Hello!";
        add.Print();
        Console.WriteLine("Number + 1 = " + add.NumberPlusOne());
    }
}

Something like this: a base abstract class ( Add ) and derived class ( Class ) that overrides abstract methods

  // note "abstract"
  public abstract class Add {
    public abstract int Number { get; set; }
    public abstract string Text { get; set; }
    public abstract int NumberPlusOne(); 
    public abstract void Print();
  }

And Class should be changed into

  // note "override"
  class Class : Add {
     public override int Number { get; set; }
     public override string Text { get; set; }

     public override int NumberPlusOne()
     {
         return Number + 1;
     }

     public override void Print()
     {
         Console.WriteLine("Number is " + Number);
         Console.WriteLine("Text is " + Text);
     }
  }

//This is my solution

interface Add
        {
            string text { get; set; }
            int number { get; set; }
            void Print();
            int NumberPlusOne();

        }

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