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 }  
  • 相关阅读:
    235. Lowest Common Ancestor of a Binary Search Tree
    234. Palindrome Linked List
    233. Number of Digit One
    232. Implement Queue using Stacks
    231. Power of Two
    230.Kth Smallest Element in a BST
    229. Majority Element II
    228. Summary Ranges
    postgres 数组中获取最后一个元素的值
    excel 拆分单元格并填充上一行的数据
  • 原文地址:https://www.cnblogs.com/GoldenEllipsis/p/10359286.html
Copyright © 2011-2022 走看看