/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/csharp/snippets/methods/named1.cs
40 строк
1 KB
Bill Wagner
Add params collections (#40978)
21 май 2024, 00:01
Не верифицирован
21 май 2024, 00:01
57cbe8e
Код
Авторство
О чём код?
// <Snippet45> namespace NamedMotorCycle; class TestMotorcycle : Motorcycle { public override int Drive(int miles, int speed) => (int)Math.Round((double)miles / speed, 0); public override double GetTopSpeed() => 108.4; static void Main() { var moto = new TestMotorcycle(); moto.StartEngine(); moto.AddGas(15); int travelTime = moto.Drive(miles: 170, speed: 60); Console.WriteLine("Travel time: approx. {0} hours", travelTime); } } // The example displays the following output: // Travel time: approx. 3 hours // </Snippet45> abstract class Motorcycle { // Anyone can call this. public void StartEngine() {/* Method statements here */ } // Only derived classes can call this. protected void AddGas(int gallons) { /* Method statements here */ } // Derived classes can override the base class implementation. public virtual int Drive(int miles, int speed) { /* Method statements here */ return 1; } // Derived classes can override the base class implementation. public virtual int Drive(TimeSpan time, int speed) { /* Method statements here */ return 0; } // Derived classes must implement this. public abstract double GetTopSpeed(); }