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 }  
  • 相关阅读:
    clear:both其实是有瑕疵的
    CSS3不遥远,几个特性你要知道
    JavaScript使用数组拼接字符串性能如何?
    CSS网页宽度怎么定比较合适
    浅析JavaScript的垃圾回收机制
    淡入淡出效果的js原生实现
    非阻塞式JavaScript脚本及延伸知识
    HTML5 Canvas圣诞树
    Ubuntu查看和自动挂载硬盘
    正则表达式批量重命名
  • 原文地址:https://www.cnblogs.com/GoldenEllipsis/p/10359286.html
Copyright © 2011-2022 走看看