0

Пытаюсь построить иерархию классов(из Main вызывается базовый конструктор,который в зависимости от некоторых параметров вызывает один из трех подклассов.) Компилятор выдает ошибку " "***" не содержит конструктор,который принимает аргументы " и "имена членов не могут совпадать с именами типов,в которых они содержаться". Как можно это исправить?

Вызов базового класса

var Price=new Edition(EditionType, PagesNumber, PrintType); 

Сам базовый класс

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

namespace PriceCalculator
{
    public class Edition
    {
        private readonly int EditionType;
        private readonly int PagesNumber;
        private readonly int PrintType;
        public Edition(int EditionType, int PagesNumber, int PrintType)
        {

            this.EditionType = EditionType;
            this.PagesNumber = PagesNumber;
            this.PrintType = PrintType;
            int ApproxPrice = PagesNumber * PrintType;

            Calculate(ApproxPrice);


        }

        public int Calculate (int Price)
        {

            if (EditionType == 1)
            {
                int value = Convert.ToInt32(new Journal(Price));
                return value;
            }

            else { return 0; }




        }
    }
}

Класс Journal

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

namespace PriceCalculator

{
   public class Journal : Edition
    {
        private readonly int ApproxPrice;

        public Journal(int ApproxPrice)
        {
            this.ApproxPrice = ApproxPrice;
            ControlPrice(ApproxPrice);
        }

        public int ControlPrice(int Price)
        {
            Console.WriteLine("Test");
            var mult = Convert.ToInt32(Console.Read());
            return Convert.ToInt32(ApproxPrice * mult);

        }
    }
}
2
  • 1
    Что это такое? public int Journal(int ApproxPrice) вы пытаетесь в конструкторе вернуть значение?
    – user206435
    19 мар 2019 в 16:27
  • 1
    конструктор возвращает объект класса, в данном случае в качестве конструктора идет попытка вызвать метод.
    – Grundy
    19 мар 2019 в 16:28

1 ответ 1

3

Конструктор не может что-то возвращать.

public /*int*/ Edition(int EditionType, int PagesNumber, int PrintType)
{
  this.EditionType = EditionType;
  this.PagesNumber = PagesNumber;
  this.PrintType = PrintType;
  ApproxPrice = PagesNumber * PrintType;
}

Я поменял код,но осталась одна ошибка в классе Journal "Отсутствует аргумент, соответствующий требуемому формальному параметру "EditionType" из "Edition.Edition(int, int, int)"

У родительского класса Edition нет конструктора без параметров, вызов которого компилятор пытается вставить в Ваш код. Вот он и жалуется на отсутствие первого параметра EditionType в этом вызове.

    public Journal(int ApproxPrice) : base(?, ?, ?)
    {
        this.ApproxPrice = ApproxPrice;
        ControlPrice(ApproxPrice);
    }
17
  • 2
    еще public int Journal(int ApproxPrice).
    – user206435
    19 мар 2019 в 16:28
  • 1
    @LLENN Естественно.
    – user176262
    19 мар 2019 в 16:28
  • 1
    @Alexey Вы пытаетесь обратиться к PrintType используя тип Edition. А нужно использовать экземпляр класса - объект типа Edition.
    – user176262
    19 мар 2019 в 18:47
  • 1
    @Alexey Покажите Ваш код, в котором есть Edition.PrintType.
    – user176262
    19 мар 2019 в 18:56
  • 1
    @Alexey Перечитайте мой комментарий выше. Не торопитесь. Подумайте.
    – user176262
    19 мар 2019 в 18:58

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.