Можно ли получить доступ к реквизиту производных классов в списке базового класса? - PullRequest
0 голосов
/ 17 апреля 2020

Я новичок в c# и пытаюсь выучить его.

Я сделал следующий код:

БАЗОВЫЙ КЛАСС:

namespace Computer
{
    class Component
    {
        public string Name { get; set; }
        public string Brand { get; set; }
        public string Model { get; set; }
        public string Category { get; set; }
        public decimal Price { get; set; }

        public Component()
        {

        }

        public Component(string name, string brand, string model, string category, decimal price)
        {
            Name = name;
            Brand = brand;
            Model = model;
            Category = category;
            Price = price;
        }
    }
}

ПРОИЗВОД. КЛАСС

namespace Computer
{
    class Processor : Component
    {
        public double Frequency { get; set; }

        public Processor(string name, string brand, string model, string category, decimal price, double frequency) 
            : base(name, brand, model, category, price)
        {
            Frequency = frequency;
        }
    }
}

ОСНОВНАЯ ПРОГРАММА:

using System;
using System.Collections.Generic;

namespace Computer
{
    class Program
    {
        static void Main(string[] args)
        {
            var computer = new Computer();

            computer.ItemsList = new List<Component>
            {
                new Component("ASUS ROG Maximus XI Hero Z390", "Asus", "Maximus XI Hero (Wi-Fi)", "Motherboards", 399.99m),
                new Processor("i9-9900k", "Intel", "BX80684I99900K", "Processors", 649.99m, 5.50),
            };

            foreach (var part in computer.ItemsList)
            {
                Console.WriteLine("Component Description : ");
                Console.WriteLine("------------------------");
                Console.WriteLine($"Name : {part.Name}");
                Console.WriteLine($"Brand : {part.Brand}");
                Console.WriteLine($"Model : {part.Model}");
                Console.WriteLine($"Category : {part.Category}");
                Console.WriteLine($"Price : ${part.Price}");
                ***Console.WriteLine(part.Frequency);***
                Console.WriteLine("---\n");               
            }
        }
    }
}

Я знаю, что базовый класс не имеет пропеллер частоты, но есть ли способ получить доступ к этому опору для моего нового процессора пример? А может я плохо это делаю?

Спасибо за советы !!

...