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 }  
  • 相关阅读:
    TCP/IP协议栈之QEMU
    FreeRTOS-Plus-CLI中添加一个自己的命令行
    FreeRTOS A57
    log日志库
    函数解读:ioremap / ioremap_nocache / ioremap_wc / ioremap_wt
    Makefile 使用小结
    41. 缺失的第一个正数(First Missing Positive)
    42. 接雨水(Trapping Rain Water)
    关于C++内联和静态成员函数的问题
    C++11 线程并发问题
  • 原文地址:https://www.cnblogs.com/GoldenEllipsis/p/10359286.html
Copyright © 2011-2022 走看看