Реализация интерфейсов класса - C#
Формулировка задачи:
Есть класс интерфейс:
Нужно реализовать его функции в классе наследнике:
Но VisualStudio 2015 пишет, что в классе не реализованы методы интерфейса (хз правильно ли я сформулировал).
Далее, нужно унаследовать класс Car и переопределить все три функции в классах:
Помогите разобраться, плс.
п.с. весь код:
Листинг программы
- interface Transport
- {
- void SetName(string name);
- void SetTonnage(int tonnage);
- void SetCost(double cost);
- }
Листинг программы
- class Car : Transport // ERROR!
- {
- private string name;
- private int tonnage;
- private double cost;
- protected virtual void SetName(string name)
- {
- this.name = name;
- }
- protected virtual void SetTonnage(int tonnage)
- {
- this.tonnage = tonnage;
- }
- protected virtual void SetCost(double cost)
- {
- this.cost = cost;
- }
- }
Листинг программы
- class Sedan : Car
- {
- }
- class Truck : Car
- {
- }
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Lab_2_option_2
- {
- interface Transport
- {
- void SetName(string name);
- void SetTonnage(int tonnage);
- void SetCost(double cost);
- }
- class Car : Transport
- {
- private string name;
- private int tonnage;
- private double cost;
- protected virtual void SetName(string name)
- {
- this.name = name;
- }
- protected virtual void SetTonnage(int tonnage)
- {
- this.tonnage = tonnage;
- }
- protected virtual void SetCost(double cost)
- {
- this.cost = cost;
- }
- }
- class Sedan : Car
- {
- }
- class Truck : Car
- {
- }
- }
Решение задачи: «Реализация интерфейсов класса»
textual
Листинг программы
- static void Main(string[] args)
- {
- Console.Write(" Enter the brand name of the car: ");
- string name = Console.ReadLine();
- Console.Write(" Enter the tonnage of the car: ");
- int tonnage;
- int.TryParse(Console.ReadLine(), out tonnage);
- Console.Write(" Enter the cost of the car: ");
- double cost;
- double.TryParse(Console.ReadLine(), out cost);
- Sedan car1 = new Sedan(name, tonnage, cost); // ERROR!!!
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д