我可以在功能中初始化类似类型(子和父)的多个类别的类别

发布于 2025-01-28 10:01:11 字数 1326 浏览 1 评论 0原文

我有一个父母类,“场景”和多个继承它的类。我想拿一个“ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寻找一个思念的角度 2025-02-04 10:01:11

当您在方法中声明一个变量时,您将无法重新调整它。

此代码应有效:

Scene scene;

if (sceneType == 1) {
 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();

When you declare a variable in a method, you can't re-declare it.

This code should works :

Scene scene;

if (sceneType == 1) {
 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();
迷离° 2025-02-04 10:01:11

可以使用字典< tkey,tvalue>在Java中的C#或Hashtable中,以避免使用语句。

让我通过C#展示一个例子。

您的基本和派生类:

public class Scene 
{
    public string Name { get; set; }
}

public class AnswerScene : Scene { }

public class InformationScene : Scene { }

场景类型:

public enum SceneType
{
    Base, Answer, Information
}

以及Factory创建场景的实例 speceType :

public class SceneToType
{
    public Dictionary<SceneType, Scene> SceneByType { get; private set; } = new()
    {
        { SceneType.Information, new InformationScene() },
        { SceneType.Answer, new AnswerScene() },
        { SceneType.Base, new Scene() }
    };
}

然后您可以通过调用方法来创建对象:

Scene GetInstance(SceneType sceneType) 
{
    return new SceneToType().SceneByType[sceneType];
}

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:

public class Scene 
{
    public string Name { get; set; }
}

public class AnswerScene : Scene { }

public class InformationScene : Scene { }

and scene types:

public enum SceneType
{
    Base, Answer, Information
}

And factory which creates an instance of Scene by SceneType:

public class SceneToType
{
    public Dictionary<SceneType, Scene> SceneByType { get; private set; } = new()
    {
        { SceneType.Information, new InformationScene() },
        { SceneType.Answer, new AnswerScene() },
        { SceneType.Base, new Scene() }
    };
}

and then you can create objects by calling a method:

Scene GetInstance(SceneType sceneType) 
{
    return new SceneToType().SceneByType[sceneType];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文