有人可以使用这个编程任务我可以使用抽象类作为基类 [英] Can Someone With This Programming Task Should I Use An Abstract Class As A Base Class

查看:85
本文介绍了有人可以使用这个编程任务我可以使用抽象类作为基类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写一个C#控制台应用程序,它可以从任何适当的来源构建单个车辆列表,然后

然后将列表中所有车辆的详细信息输出到控制台。

车辆可以是以下三种类型之一:

汽车,

摩托车,



所有3类型具有以下常见属性:

发动机,

制造商,

型号

每辆车还有物业每种车型都是独一无二的。

例如,一辆车有4个轮子,一辆摩托车有2个,一艘船有一个螺旋桨。

展示一种合适的方法来创造并显示每辆车的这些数据,然后将每辆车的

品牌,型号(以及任何其他相关细节)输出到控制台。

请拉上并返回完成的解决方案一旦完成。

解决方案

是的,在我看来,车辆应该是抽象的(可能是界面)。这就是现实世界的建议。


我会比我尊敬的同事CPallini和George Jonsson的回复更远一点,并且肯定会选择使用界面...因为:



如果您的List 必须包含不同子类型的项目,那么您可以这样做的唯一方法是使用接口,或者使用延迟像System.Dynamic中的Expando对象一样绑定数据结构...甚至不考虑旧的'ArrayList,请:)



代码草图这里不是工作代码,因为它还没有经过全面测试(我做了测试,以确保它将编译,如果完成)。使用它为您自己的工作获取想法

  public   enum  VehicleType 
{
Car,
Motorbike,
Boat
}

public interface IVehicle
{
VehicleType VType { get < /跨度>; set ; }
// 你写下剩余的
无效打印(IVehicle vehicle);
}

是的,我使用的是Enum:不是因为它是绝对必需的,而是因为它使代码更清楚地表达了意图,这有助于代码维护和随着时间的推移重新使用。



我可能会选择将IVehicles集合定义为单独的类:

  public   class  VehicleCollection:List< IVehicle> 
{
public void PrintToConsole()
{
// 调用'每个实例中的Print方法
foreach (IVehicle iv in this )iv.Print();
}
}

假设您定义了一个继承自IVehicle的Class'Flora,以及一个继承自Vehicle的Class'MotorBike,其中您将属性唯一到摩托车......然后'VehicleCollection的打印命令(如图所示)可以非常简单。



在Vehicle Class的Print命令中首先打印各种车辆类型的所有常见属性,然后处理如何打印每种类型车辆的独特属性的问题:

  public   void  Print()
{
// 写下所有类型车辆共享的所有常见属性
// Console.WriteLine(?????);
// 你写剩下的

// 在此处写出每种车型的独特属性
< span class =code-keyword> switch ( this .VType)
{
case VehicleType.Motorbike:
// print motorbike data

// 此处不需要测试null
// 并且是防御性编程的一个例子

// MotorBike类中的打印方法留给您编写
var motorBike = as 摩托车;
if (motorBike!= null )motorBike.Print();
break ;

// 你写下剩余的
}
}


为什么我们不使用抽象类作为基类和派生类来继承虚拟属性。





  abstract  车辆
{

// 方法
public virtual void PrintVehicleType()
{


}
// 字段
内部 int _EngineCapacity;
internal string _VehicleType;
internal int _nOfWheels;
internal string _Manifacturer;


// 属性
内部 虚拟 int _NoOfWheels
{
get { return _NoOfWheels; }
set {_NoOfWheels = value ; }

}

内部 虚拟 string VehicleType
{
get { return _VehicleType; }
set {_VehicleType = VehicleType; }

}


}
class CAR
{



}

public class MotorCycle:Vehicle
{
public override void PrintVehicleType()
{
base .PrintVehicleType();
}

}
class
{
int _EngineCapacity;
public bool HasPropeller = true ;

}



}



}


Write a C# console application which builds a single list of vehicles from any appropriate source and
then prints the details of all the vehicles in the list out to the console.
A vehicle can be one of 3 types:
Car,
Motorbike,
Boat
All 3 types have the following, common properties:
Engine,
Manufacturer,
Model
Each vehicle also has properties which are unique to each vehicle type.
For example, a car has 4 wheels, a motorbike has 2 and a boat has a propeller.
Demonstrate a suitable approach to create and display this data for each vehicle then output the
make, model (and any other relevant details) for each vehicle to the console.
Please zip up and return the finished solution once you are finished.

解决方案

Yes, Vehicle should be abstract, in my opinion (could be an interface). That's what real world suggests.


I would go a little farther than the replies by my esteemed colleagues CPallini, and George Jonsson, and definitely choose to use an Interface ... because:

If your List must include items of different sub-types, the only way you can do that is to use an Interface, or use a late-binding data-structure like the Expando object in System.Dynamic ... don't even think about the old 'ArrayList, please :)

The code "sketch" here is not working code in the sense it has not been fully tested (I did test to make sure it will compile if "completed"). Use it to get ideas for your own work:

public enum VehicleType
{
    Car,
    Motorbike,
    Boat
}

public interface IVehicle
{
    VehicleType VType { get; set; }
    // you write the rest
    void Print(IVehicle vehicle);
}

Yes, I'd use an Enum: not because it's absolutely required, but because it makes the code express the intent more clearly, which contributes to code maintenance and re-usability over time.

I would probably choose to define a collection of IVehicles as a separate Class:

public class VehicleCollection : List<IVehicle>
{
    public void PrintToConsole()
    {
        // invoke the 'Print method in each instance
        foreach (IVehicle iv in this) iv.Print();
    }
}

Assuming you define a Class 'Vehicle that inherits from IVehicle, and a Class 'MotorBike which inherits from Vehicle in which you put the properties unique to a Motorbike ... then the Print command for the 'VehicleCollection (as shown) can be very simple.

In the Print command in the Vehicle Class is where you first print all the common properties of the various vehicle types, and then you deal with the issue of how to print the unique properties of each type of Vehicle:

public void Print()
{ 
    // write all the common Properties shared by all types of vehicles here
    // Console.WriteLine(?????);
    // you write the rest
    
    // write out each vehicle type's unique properties here
    switch (this.VType)
    {
        case VehicleType.Motorbike:
            // print motorbike data
    
            // the test for null here is not required
            // and is an example of "defensive" programming

            // Print method in MotorBike Class left for you to write
            var motorBike = this as MotorBike;
            if (motorBike != null) motorBike.Print();
            break;
    
        // you write the rest
    }
}


Why didnt we Use An abstract Class as a Base Class and derived class to inherit Virtual Properties.


abstract class Vehicle
   {

       //method
       public virtual void PrintVehicleType()
       {


       }
       //fields
       internal int _EngineCapacity;
       internal string _VehicleType;
       internal int _nOfWheels;
      internal string _Manifacturer;


       // Properties
       internal virtual int _NoOfWheels
       {
           get { return _NoOfWheels; }
           set { _NoOfWheels = value; }

       }

       internal virtual string VehicleType
       {
           get { return _VehicleType; }
           set { _VehicleType = VehicleType; }

       }


   }
   class CAR
   {



   }

     public  class MotorCycle : Vehicle
       {
           public override void PrintVehicleType()
           {
               base.PrintVehicleType();
           }

       }
       class Boat
       {
           int _EngineCapacity;
           public bool HasPropeller = true;
        
       }



   }



}


这篇关于有人可以使用这个编程任务我可以使用抽象类作为基类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆