• 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 }  
  • 相关阅读:
    浅谈工业无线技术之天线
    防护等级
    PROFINET如何实现实时性
    2020,我又回来了
    关于ReentrantLock和Condition的用法
    当你在试衣间试衣服,请你务必想起wait()与notify()
    用生活例子来解释Java synchronized块
    关于textview显示特殊符号居中的问题
    扯一扯我的2016
    国庆的这6天
  • 原文地址:https://www.cnblogs.com/GoldenEllipsis/p/10359286.html
Copyright © 2020-2023  润新知