【问题标题】:declare object in c# with x,y paramateres [closed]在 c# 中使用 x,y 参数声明对象 [关闭]
【发布时间】:2016-03-22 10:42:13
【问题描述】:

我想创建具有 x、y 参数的对象“A”。 A 必须是一个数组。声明和构造此类对象的最佳实践是什么?例如这样的:

A[0].x=5
A[0].y=3

A[1].x=6
A[1].y=4

更新 我是这样做的:

    public class PersonalWaypoints
    {
        public float x { get; set; }
        public float y { get; set; }
    }
public class MainClass:MonoBehaviour
{
then in void Start() {
PersonalWaypoints[] pw = new PersonalWaypoints[waypoints.Length];

pw[0].x = waypoints[0].transform.position.x ;
pw[0].y = waypoints[0].transform.position.y ;

但是我不能在 Update() { 中使用 pw 因为它在当前上下文中不存在。

为了在上下文中使用它,我无法移动 PersonalWaypoints[] pw = new PersonalWaypoints[waypoints.Length];到类定义,因为在定义类时 waypoints.Length 是未知的。

【问题讨论】:

  • 不要让别人为你做你的学习......为自己阅读。 msdn.microsoft.com/en-us/library/ms173109.aspx
  • 如果您有一个表单项目,请使用包含 x & y 属性的 Point 或 PointF。您还可以使用 KeyValuePair。字典是 KeyValuePair 的集合,因此您可以使用字典来收集所有值 Dictionary.

标签: c# arrays object


【解决方案1】:

你的问题有点含糊,如果你想要自动扩展类似数组的集合你必须有两个对象:一个用于点

  //TODO: think over, may be you want a struct, not class
  // May be you want just a standard Point struct
  public class Item {
    public int x {get; set;}
    public int y {get; set;}
  }

一个用于“数组”(实际上是一个索引器

  //TODO: you may want to implement IEnumerable<Item>
  public class MyClass {
    private List<Item> m_Items = new List<Item>();

    private void Expand(int index) {
      if (index < 0) 
        throw new ArgumentOutOfRangeException("index");

      for (int i = m_Items.Count; i <= index; ++i)
        m_Items.Add(new Item());
    }

    // indexer: mimics auto expanding array
    public Item this[int index] {
      get {
        Expand(index); 

        return m_Items[index];
      }
    } 

    public int Count {
      get {
        return m_Items.Count;
      }  
    }
  }

...

  MyClass test = new MyClass();
  // test will be expanded to have 1 item: test[0]
  test[0].y = 456;
  // test will be expanded to have 6 items [0..5]
  test[5].x = 123; 

【讨论】:

    【解决方案2】:

    定义一个包含您的字段的Point 类型:

    public class Point
    {
        public int X { get; set; }
        public int Y { get; set; }
    }
    

    然后创建一个Points(或List&lt;Point&gt;)的数组:

    var A = new[] {
        new Point { X = 5, Y = 3 },
        new Point { X = 6, Y = 4 }
    };
    

    当然,您可以从xy 的值中获取这些值,这些值没有硬编码到对象创建中。例如,假设您有一个列表xs,其中的整数应该是x 坐标,还有一个类似(并且长度相等!)列表ys 带有y 值。然后,您将获得来自

    Points 列表
    var points = xs.Zip(ys, (x,y) => new Point { X = x, Y = y });
    

    然后以您想要的方式访问坐标:points[1].x 是第二个点的 x 坐标。

    【讨论】:

    • 我怎样才能创建类似 var a = new Point[5] 的东西,然后再做 a[1].x=1 的事情?
    • var a = new Point[5] 分配数组,但实际上并没有用某些东西填充它 - 所有元素都将为空。在某些时候,您必须为数组中的每个元素调用new Point()。 (当然,你可以将它封装在某个工厂方法中,但你不能完全避免它。)
    • 我在打电话时遇到了一个小问题。我必须在 void Start(){} 和 void Update(){} 中使用它,因此我必须直接从主类调用它。它可以完成,除非我调用 new Point[5] 而不是 5 我使用了一些变量,并且在类声明期间不可能在那里使用变量。
    • @David:您引用了很多未显示的代码。 Start()Update() 方法是什么?您尝试做的事情的背景是什么?
    • stackoverflow.com/questions/36119592/… 这是代码和我想要做的,但不幸的是没有任何运气:(
    【解决方案3】:

    您可以使用您的参数创建一个类,然后创建该类的数组或列表:

    public class _A
    {
        public int x;
        public int y;
    } 
    
    _A[] A;
    

    或者

    List<_A> = new List<_A>();
    

    【讨论】:

      【解决方案4】:
      public class A
      {
        int x;
        int y;
      }
      
      A[] numbers = new A[10];
      

      【讨论】:

      • 也许您可以为此添加一些文字/解释?
      【解决方案5】:
      sealed class A {
          readonly int m_x;
          readonly int m_y;
      
          internal A(int x, int y) {
              m_x = x;
              m_y = y;
          }
      
          internal int X {
              get {
                  return m_x;
              }
          }
      
          internal int Y {
              get {
                  return m_y;
              }
          }
      }
      

      然后获取对象数组:

       var myVar = new A[1]; // Array size between []
      

      【讨论】:

        【解决方案6】:

        首先你必须创建一个类坐标

        class Coordinate{
        public int x;
        public int y;
        public Coordinate(int x, int y){
        this.x=x;
        this.y=y;
        }
        

        然后你创建一个坐标数组

        Coordinate[]A=new Coordinate[10];
        A[0]=new Coordinate(0,0);
        A[1]=new Coordinate(5,4);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-12-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多