我可以在功能中初始化类似类型(子和父)的多个类别的类别
我有一个父母类,“场景”和多个继承它的类。我想拿一个“ ID”值并初始化某个类别,但是如果使用语句,则有重复类型的问题。
if (sceneType == 1) {
Scene scene = new Scene(jsonScene.getInt("id"));
} else if (sceneType == 2) {
EndingScene scene = new EndingScene(jsonScene.getInt("id"));
} else if (sceneType == 3) {
InformationScene scene = new InformationScene(jsonScene.getInt("id"));
} else if (sceneType == 4) {
AnswerScene scene = new AnswerScene(jsonScene.getInt("id"));
} else if (sceneType == 5) {
ActionScene scene = new ActionScene(jsonScene.getInt("id"));
}
scene.doSomething();
场景。如果说明语句之外的范围,则尚未初始化场景。
Scene scene;
if (sceneType == 1) {
Scene scene = new Scene(jsonScene.getInt("id"));
} else if (sceneType == 2) {
EndingScene scene = new EndingScene(jsonScene.getInt("id"));
} else if (sceneType == 3) {
InformationScene scene = new InformationScene(jsonScene.getInt("id"));
} else if (sceneType == 4) {
AnswerScene scene = new AnswerScene(jsonScene.getInt("id"));
} else if (sceneType == 5) {
ActionScene scene = new ActionScene(jsonScene.getInt("id"));
}
scene.doSomething();
另外,如果我在功能范围中定义场景,则初始化不同类型的场景是重复的本地变量。
当返回类型特定于父对象的一般函数时,我也有问题,但是如果我希望它返回传递到其中的对象类型,而不是默认情况下。
I have a parent class , 'Scene' and multiple classes that inherit it. I want to take an 'id' value and initialize a certain class, but have a problem with duplicate types if a switch / if statements are used.
if (sceneType == 1) {
Scene scene = new Scene(jsonScene.getInt("id"));
} else if (sceneType == 2) {
EndingScene scene = new EndingScene(jsonScene.getInt("id"));
} else if (sceneType == 3) {
InformationScene scene = new InformationScene(jsonScene.getInt("id"));
} else if (sceneType == 4) {
AnswerScene scene = new AnswerScene(jsonScene.getInt("id"));
} else if (sceneType == 5) {
ActionScene scene = new ActionScene(jsonScene.getInt("id"));
}
scene.doSomething();
scene.doSomething throws error as in the scope outside the if statements, scene has not been initialized.
Scene scene;
if (sceneType == 1) {
Scene scene = new Scene(jsonScene.getInt("id"));
} else if (sceneType == 2) {
EndingScene scene = new EndingScene(jsonScene.getInt("id"));
} else if (sceneType == 3) {
InformationScene scene = new InformationScene(jsonScene.getInt("id"));
} else if (sceneType == 4) {
AnswerScene scene = new AnswerScene(jsonScene.getInt("id"));
} else if (sceneType == 5) {
ActionScene scene = new ActionScene(jsonScene.getInt("id"));
}
scene.doSomething();
Alternatively if I define scene in the scope of my function, initializing the different types of scene are duplicate local variables.
I also have the problem when a general function that's return type is specific to parent object, but if I want it to return the object type passed into it, rather than the parent by default.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您在方法中声明一个变量时,您将无法重新调整它。
此代码应有效:
When you declare a variable in a method, you can't re-declare it.
This code should works :
可以使用字典< tkey,tvalue>在Java中的C#或Hashtable中,以避免使用语句。
让我通过C#展示一个例子。
您的基本和派生类:
场景类型:
以及Factory创建
场景
的实例 speceType :然后您可以通过调用方法来创建对象:
It is possible to use Dictionary<TKey, TValue> in C# or HashTable in Java to avoid multiple
if else
statements.Let me show an example via C#.
Your base and derived classes:
and scene types:
And factory which creates an instance of
Scene
bySceneType
:and then you can create objects by calling a method: