使用链接方法 c# 创建新变量时如何在内部 class returns 之间跳转

How to hop between inner class returns when creating a new variable with chaining methods c#

我不知道跳跃是否是我想要实现的目标的最佳词(因为我从未正式学习过编程)基本上我有一个 class 有两个内部 classes 这里是代码

using System.Drawing;

namespace Leveltar_Fresh
{
   class Scene
   {
      private Image image;

      public class Sphere : Scene
      {
          public Sphere()
          {
            
          }

          public Sphere addClickable(int x, int y, int r)
          {
             Clickable clickable = new Clickable.Sphere(x, y, r);
             return this;
          }
      }

      public class Rectangle : Scene
      {
         public Rectangle()
         {

         }

         public Rectangle addClickable(int x, int y, int width, int height)
         {
            Clickable clickable = new Clickable.Rectangle(x, y, width, height);
            return this;
         }
      }

      public static Scene createNewScene(Scene scene)
      {
         return scene;
      }
   }
}

我尝试向外部 class 添加一个方法,我可以将其用作我的方法链的另一个链此方法

public static Scene getScene()
{
   return this;
}

但是当我在链中调用此方法时,我无法访问内部 classes 我做了 new Scene.Sphere().addClickable(20, 20, 50).getScene() 然后我无法调用内部 class es 我对 java 有更多的经验,我知道在 c# 和 java 中内部 classes 的使用存在一些差异,但是是什么原因导致的,我该如何解决这个问题?

这里有点难说你想做什么。也许是这样的?

public interface IScene
{
    bool PointIsWithinScene( int x, int y);
} 

public Sphere: IScene
{
    public Sphere( int x, int y, int r) { ... }
    
    public bool PointIsWithinScene( int x, int y) { return ...; }
}

public Rectangle: IScene
{
    public Rectangle( int x, int y, int w, int h) { ... }

    public bool PointIsWithinScene( int x, int y) { return ...; }
}

public CompositeScene: List<IScene>, IScene
{
    public CompositeScene(): base() { ... }

    public bool PointIsWithinScene( int x, int y) 
    {
        return this.Any( subScene => subScene.PointIsWithinScene( x, y));
    }
}

我不确定我明白你想做什么。但是调用getScene()方法后无法访问嵌套的classes是因为你的嵌套classes的作用域被限制在父class内] 而已。

回应我的评论class是否需要嵌套,你提到你只需要它来组织代码而不是限制范围,所以我会建议将那些 classes 移出。这实际上会产生更清晰的代码。

下面我试图保留你的功能,只是让你的代码更流畅

public class Scene
{
    public Scene()
    {
         ChildSphere = new Sphere();
         ChildRectangle = new Rectangle();
    }

    // 2 previously nested classes are now public properties.
    public Sphere ChildSphere { get; }
    public Rectangle ChildRectangle { get; }

    public static Scene CreateNewScene(Scene scene)
    {
        //logic...
        return scene;
    }

    public Scene GetScene()
    {
        return this;
    }
}

public class Sphere : Scene
{
    public Sphere AddClickable(int x, int y, int r)
    {
        Clickable clickable = new Clickable.Sphere(x, y, r);
        // logic..
        return this;
    }
}

public class Rectangle : Scene
{
    public Rectangle AddClickable(int x, int y, int width, int height)
    {
        Clickable clickable = new Clickable.Rectangle(x, y, width, height);
        // logic..
        return this;
    }
}

结果是:

new Scene().ChildSphere
.AddClickable(20, 20, 50)
.GetScene()
.ChildRectangle
.AddClickable(20,40,60,80)
.GetScene();

我包含 GetScene() 方法只是因为您的示例中有它。鉴于您提供的代码,我认为它没有多大用处。

需要指出的是在上面的例子中,一旦你调用这个 new Scene().ChildSphere 你就不能再访问(通过链接)原始(第一个)场景。换句话说,每条附加链都将返回当前实例。

因此,就每个链之后返回的对象而言,我们有:

new Scene().ChildSphere     // scene1 -> scene1.ChildSphere
.AddClickable(20, 20, 50)   // scene1.ChildSphere
.GetScene()                 // scene1.ChildSphere
.ChildRectangle             // scene1.ChildSphere.ChildRectangle
.AddClickable(20,40,60,80)  // scene1.ChildSphere.ChildRectangle
.GetScene();                // scene1.ChildSphere.ChildRectangle

如果你想在任何时候回到 scene1 或其他一些以前的父级,那么你需要实现一种链接父级和子级的方法。这是可能的,但已将其排除在这个问题的范围之外。