简体   繁体   English

Java是否为类成员提供动态变量?

[英]Does Java have dynamic variables for class members?

I am wondering whether it is possible to make dynamic variables in Java. 我想知道是否可以在Java中创建动态变量。 In other words, variables that change depending on my instructions. 换句话说,变量会根据我的指示而变化。

EDIT Rephrasing my question, I mean variables for a class that change type depending on a given variable (stockType, for those who read on). 编辑重新解释我的问题,我的意思是一个类的变量,它根据给定的变量更改类型(stockType,对于那些阅读的人)。

FYI, I am making a trading program. 仅供参考,我正在制作交易计划。 A given merchant will have an array of items for sale for various prices. 给定的商家将有各种价格的待售物品。

The dynamism I am calling for comes in because each category of items for sale has its own properties. 我要求的活力是因为每种待售物品都有自己的特性。 For example, a book item has two properties: int pages, and boolean hardCover. 例如,book项有两个属性:int pages和boolean hardCover。 In contrast, a bookmark item has one property, String pattern. 相反,书签项具有一个属性String pattern。

Here are skeleton snippets of code so you can see what I am trying to do: 这是代码的骨架片段,因此您可以看到我想要做的事情:

public class Merchants extends /* certain parent class */ {
        // only 10 items for sale to begin with
        Stock[] itemsForSale = new Stock[10]; 

        // Array holding Merchants
        public static Merchants[] merchantsArray = new Merchants[maxArrayLength];

        // method to fill array of stock goes here
}

and

public class Stock {
    int stockPrice;
    int stockQuantity;
    String stockType; // e.g. book and bookmark
    // Dynamic variables here, but they should only be invoked depending on stockType
    int pages;
    boolean hardCover;
    String pattern;
}

Subclass your Stock for each stock type: 子类的Stock每只股票类型:

public class Stock {
    private int price;
    private int quantity;
}

public class StockBook extends Stock {
    private int pages;
    private boolean hardCover;
}

public class StockBookmark extends Stock {
    private String pattern;
}

Or use Map for the different types of attributes: 或者使用Map表示不同类型的属性:

public class Stock {
    private int price;
    private int quantity;
    private String classification; // e.g. book and bookmark
    private Map properties = new HashMap();
}

You might want to consider using polymorphism. 您可能想要考虑使用多态。

public class Stock {
    int stockPrice;
    int stockQuantity;
    int stockType;
}

public class Book extends Stock{
    int pages;
    boolean hardcover;
}

public class Bookmark extends Stock {
    String pattern;
}

Java does not allow dynamic variables. Java不允许动态变量。 Instead, you should apply object oriented design techniques. 相反,您应该应用面向对象的设计技术。

In this case, you should define an abstract class Stock with common methods and members and extend that class for the types: Book , Bookmark , etc. See below for an example. 在这种情况下,您应该使用常用方法和成员定义抽象类Stock ,并为类型扩展该类: BookBookmark等。请参阅下面的示例。

The advantages to using an abstract class for Stock (which none of the other answers have yet shown) is that a "Stock" item can't really exist on its own. 使用Stock的抽象类(其他答案都没有显示)的优点是“Stock”项目本身并不存在。 It doesn't make sense to have 5 Stocks on a Merchant's shelf in the same way that it makes sense to have 5 Books on a Merchant's shelf. 在商家的货架上放置5个股票是没有意义的,就像在商家货架上放置5本书一样。

public abstract class Stock {
    private int stockPrice;
    private int stockQuantity;

    // Implement getters and setters for stockPrice and stockQuantity.
}

public class Book extends Stock {
    // Since I'm extending Stock, I don't have to redefine price or quantity
    private int pages;
    private boolean hardCover;

    // Implement getters and setters for pages and hardCover.
}

public class Bookmark extends Stock {
    private String pattern;

    // Implement getters and setters for pattern.
}

Note that in order to determine which type of object you are dealing with later, if you absolutely have to , you'll use checks like: 请注意,为了确定您以后要处理的对象类型, 如果您必须这样做 ,您将使用以下检查:

if (myStock instanceof Book) { ... }

NO and for good reason. ,有充分的理由。

Anyways to do what you want instead of having a stockType, Subclass Stock into the different types you want. 无论如何要做你想要的而不是有一个stockType,Subclass Stock到你想要的不同类型。

Give us examples of what you want to do with the dynamic variables and we can show you how. 给我们举例说明您想要对动态变量做什么,我们可以向您展示如何做。

Also don't use arrays. 也不要使用数组。 They are fixed length and only really there to be C++ like. 它们是固定的长度,只有像C ++一样。 Use List instead. 改用List

Replace Stock[] itemsForSale = new Stock[10]; 替换Stock[] itemsForSale = new Stock[10]; with List<Stock> itemsForSale = new ArrayList<Stock>(); with List<Stock> itemsForSale = new ArrayList<Stock>(); and read up about List 并阅读有关List

我不确定你想要什么,但听起来像State设计模式可能是你要检查的东西。

You can use a factory design pattern here. 您可以在此处使用工厂设计模式 The reason being, based on the stockType you want an object that is specific/dynamic, which a factory would provide for you. 原因是,基于stockType,您需要一个特定/动态的对象,工厂将为您提供。

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

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