简体   繁体   English

使用Static类或Abstract类进行对象创建

[英]Use Static class or Abstract class for object creation

So I have a general question. 所以我有一个普遍的问题。 Lets say I want to create car objects in this program I am writing and I have to create hundreds of this car object. 让我们说我想在这个程序中创建汽车对象我正在编写,我必须创建数百个这个汽车对象。 Is it better to create a car abstract class and extend it when I make my car object(ie Toyota, Nissan, etc.) or could I use a bunch of static classes that hold the details of the specific object and use that in a general car object to make the Toyota or Nissan? 创建汽车抽象类并在制作我的汽车对象(即丰田汽车,日产汽车等)时扩展它是否更好?或者我可以使用一堆静态类来保存特定对象的细节并在一般情况下使用它汽车对象要制造丰田还是日产? I think the using the abstract method should be pretty self explanatory but here is an example of the second method: 我认为使用抽象方法应该是非常自我解释的,但这是第二种方法的示例:

public class CarNames {

public static String getCarName(int pCarIndex){

    switch (pCarIndex) {
    case 0:         
        return "Toyota";
    default:
        throw new Error("pCarIndex does not exist!");
    }
}

}

Which is used in this class to create the object: 在此类中使用哪个来创建对象:

public class Car{

    private int mCar_ID;//This indicates which Car to load
    private String mCarName;

public Car(int pCar_ID){
    mCar_ID = pCar_ID;
            mCarName = CarNames.getCarName(pCar_ID);
            //Do Stuff with collected parameters 
}

}

Let me know if something doesn't make sense. 如果事情没有意义,请告诉我。 Thanks. 谢谢。

EDIT: The cars may have different parameters such as size, engine, etc. The reason I chose an abstract class is because I think it offers more flexibility than an interface, since I could add methods down the line. 编辑:汽车可能有不同的参数,如大小,引擎等。我选择抽象类的原因是因为我认为它提供了比界面更多的灵活性,因为我可以添加方法。

Is it better to create a car abstract class and extend it when I make my car object(ie Toyota, Nissan, etc.) or could I use a bunch of static classes that hold the details of the specific object and use that in a general car object to make the Toyota or Nissan? 创建汽车抽象类并在制作我的汽车对象(即丰田汽车,日产汽车等)时扩展它是否更好?或者我可以使用一堆静态类来保存特定对象的细节并在一般情况下使用它汽车对象要制造丰田还是日产?

Neither. 都不是。 You have precisely one Car class and a file (eg, database) containing the details of a particular vehicle. 您只有一个Car类和一个包含特定车辆详细信息的文件(例如,数据库)。 You then create instances of that one Car class based upon the file contents. 然后,您可以根据文件内容创建该Car类的实例。

The cars may have different parameters such as size, engine, etc. 汽车可能有不同的参数,如尺寸,发动机等。

Programmers for the past half-century have referred to this as "data". 过去半个世纪的程序员将此称为“数据”。 The less data you bake into your source code, the better. 您在源代码中烘焙的数据越少越好。

I am going to take a stab at this... I am not sure if your goal in the end is ease of maintenance or memory or... I am going to guess that because this is an Android question your concern is with memory. 我准备好了...我不确定你的目标最终是否易于维护或记忆或......我猜这是因为这是一个Android问题,你关心的是记忆。 Especially when you talk about 100's of objects. 特别是当你谈论100个物体时。

My first thought the flyweight pattern it will allow those objects to share memory and have smaller footprints. 我首先想到的是轻量级模式 ,它将允许这些对象共享内存并具有更小的足迹。 But I am with the @CommonsWare answer... Part of what your are talking about is data. 但我正在使用@CommonsWare答案......你所谈论的部分内容是数据。 If I understand your question, your plan is to define all the the different parts of your car in code based on the cardId that doesn't sound very efficient. 如果我理解你的问题,你的计划是根据看起来效率不高的cardId在代码中定义汽车的所有不同部分。 That sounds like data, whether it be xml or sqllite database. 这听起来像数据,无论是xml还是sqllite数据库。

You might be talking about Nissans which could end up as a type (class) extending the abstract class car, but the engine size, height, width, and gas mileage is all data. 你可能会谈论日产汽车,它可能最终成为扩展抽象级汽车的类型(类),但发动机的尺寸,高度,宽度和汽油里程都是数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM