从令牌确定子对象并初始化它
所以我有一个抽象父类,有 6 个子类扩展它。我有一个 fileRead(String) 方法,可以从文件中读取数据。文件的第一行有一个类别 ID (FOODTYPE_CATID) 和一个名称,以“|”分隔(管道)字符,这是我在字符串标记生成器中使用的分隔符。我有 6 个 if 语句检查令牌并初始化适当的对象。然而,这就是我遇到问题的地方,我想稍后在方法中使用该对象,但不能,因为
- A) 因为它位于 if() 语句中,编译器认为可能 尚未初始化并且
- B) 我无法在 if 语句之前初始化它,因为它是 抽象的。我也只想使用这个单一的对象,我不想 初始化 6 个不同的对象并有 50 个不同的 if 语句 在单一方法中。
所以我的问题是,如何仅使用一个对象来解决此类问题?这里有一些代码供参考:
Public abstract class Recipe { methods }
Public class Soup extends Recipe { methods } //There are 5 other classes like this
Public class Controller
{
doSomething() { logic }
doThis() { logic };
readFile(String str)
{
recipeFile.open( "recipes.dat");
if ( recipeFile.exists() )
{
// read first line from the recipe file
recipeLine = recipeFile.readLine();
String Tokenizer token;
while ( recipeLine != null)
{
token = new String Tokenizer(recipeLine, "|");
Recipe recipe;
if(token.hasMoreTokens())
{
if(token.equals(SOUP_CATID))
{
recipe = new Soup();
recipe.setName(token.toString());
}
...more if statements checking other catId's
}
Ingredients i = new Ingredient();
recipeFile.open( "ingredients.dat");
while(logic)
{
//This will not work because recipe
//still hasn't been initialized before the if
//statements
recipe.addIngredient(i);
}
}
}
}
}
编辑 已解决 - 我所要做的就是在 if 语句之前将配方初始化为 null。 配方配方= null; 没有产生任何错误并且代码/逻辑有效。
So I have an abstract parent class with 6 child classes extending it. I have a fileRead(String) method that reads data from a file. The first line of the file has a category ID (FOODTYPE_CATID) and a name, separated by the '|' (pipe) character, which is my delimiter to use in String Tokenizer. I have 6 if statements checking the token and initializing the appropriate object. However, this is where I run into problems, I want to use the object later on in the method but can't because
- A) since it's in the if() statements the compiler thinks the might
not have been initialized and - B) I can't initialize it before the if statements because it's
abstract. I also only want to use this single object, I do not want
to initialize 6 different objects and have 50 different if statements
in a single method.
So my question is, how do I only use one object for this type of problem? Here is some code for reference:
Public abstract class Recipe { methods }
Public class Soup extends Recipe { methods } //There are 5 other classes like this
Public class Controller
{
doSomething() { logic }
doThis() { logic };
readFile(String str)
{
recipeFile.open( "recipes.dat");
if ( recipeFile.exists() )
{
// read first line from the recipe file
recipeLine = recipeFile.readLine();
String Tokenizer token;
while ( recipeLine != null)
{
token = new String Tokenizer(recipeLine, "|");
Recipe recipe;
if(token.hasMoreTokens())
{
if(token.equals(SOUP_CATID))
{
recipe = new Soup();
recipe.setName(token.toString());
}
...more if statements checking other catId's
}
Ingredients i = new Ingredient();
recipeFile.open( "ingredients.dat");
while(logic)
{
//This will not work because recipe
//still hasn't been initialized before the if
//statements
recipe.addIngredient(i);
}
}
}
}
}
EDIT Solved - All I had to do was initialize recipe to null before the if statements.
Recipe recipe = null;
Didn't produce any errors and the code/logic works.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不知道为什么您不认为您的
recipe
实例在进入成分处理逻辑之前不会被初始化,但无论如何我建议您阅读 工厂模式。Not sure why you don't think your
recipe
instance won't be initialized before you get to the ingredient processing logic, but in any case I would recommend reading up on Factory patterns.