zoukankan      html  css  js  c++  java
  • Example of Abstract Class

    The Vehicle class has abstract members that must be implemented by the Car class or any other class that is inherited from the Vehicle class.
     
    The Vehicle class has three abstract members, two properties, Distance and Time and a method, Speed. 
     
     1 using System;  
     2 namespace AbstractExample  
     3 {  
     4     abstract class Vehicle  
     5     {  
     6         public abstract double Distance { get; set; }  
     7         public abstract double Time { get; set; }  
     8         public abstract double Speed();  
     9     }   
    10     class Car : Vehicle  
    11     {  
    12         double mDistance, mTime = 0.0;  
    13         public override double Distance  
    14         {  
    15             get  
    16             {  
    17                 return mDistance;  
    18             }  
    19             set  
    20             {  
    21                 if (value <= 0)  
    22                 {  
    23                     mDistance = 1;  
    24                 }  
    25                 else  
    26                 {  
    27                     mDistance = value;  
    28                 }  
    29             }  
    30         }  
    31         public override double Time  
    32         {  
    33             get  
    34             {  
    35                 return mTime;  
    36             }  
    37             set  
    38             {  
    39                 if (value <= 0)  
    40                 {  
    41                     mTime = 1;  
    42                 }  
    43                 else  
    44                 {  
    45                     mTime = value;  
    46                 }  
    47             }  
    48         }   
    49         public override double Speed()  
    50         {  
    51             double speed = 0.0;  
    52             double hours = mTime / 60;  
    53             speed = mDistance / hours;  
    54             return speed;  
    55         }  
    56     }   
    57     class Program  
    58     {  
    59         static void Main(string[] args)  
    60         {  
    61             double speed = 0.0;  
    62             Car objCar = new Car();  
    63             Console.WriteLine("Enter the Distance");  
    64             objCar.Distance = Double.Parse(Console.ReadLine());  
    65             Console.WriteLine("Enter the time in minutes");  
    66             objCar.Time = Double.Parse(Console.ReadLine());  
    67             speed = objCar.Speed();  
    68             Console.WriteLine("Car speed is {0:0.00}", speed);  
    69             Console.Read();  
    70         }  
    71     }  
    72 }  
  • 相关阅读:
    项目
    Cache Code
    是什么限制了我们面向对象
    程序设计语言本质
    不要迷失在技术的海洋中
    程序是给自己看的还是给别人看的
    程序员的春天
    新手如何学习一门新的语言
    无废话C#设计模式之二十:Mediator
    (原创)代码分析-DataGrid实现增删(带提示)改和分页
  • 原文地址:https://www.cnblogs.com/GoldenEllipsis/p/10359286.html
Copyright © 2011-2022 走看看