其他分享
首页 > 其他分享> > CodeGo.net>使用一个不同的名称的系统类?

CodeGo.net>使用一个不同的名称的系统类?

作者:互联网

我需要一种方法来跟踪网格中的行数和列数.如果使用System.Point,我总是会忘记“ x”是行数还是列数.所以我有下面的课程.

但是我想知道是否可以使用具有不同命名外观的System.Point?换句话说,我不想在System.Point上定义常规的“ NRows”或“ NColumns”方法.但是我确实希望能够返回一个对象,该对象的代码将被视为“ NRowsColumns”对象,但实际上可以编译为System.Point.访问“ NRowsColumns”对象时,我们使用字段“ NRows”和“ NColumns”而不是“ x”和“ y”.但实际上,它实际上可以编译为System.Point.

理想情况下,此定义将不限于单个文件.

public class NRowsColumns
{
  public int NRows {get;set;}
  public int NColumns {get;set;}
  public NRowsColumns(int nRows, int nColumns)
  {
    this.NRows = nRows;
    this.NColumns = nColumns;
  }
}

解决方法:

不,您不能像这样“重命名”成员.您可以根据需要将System.Point称为NRowsColumns,

using NRowsColumns = System.Point;

…但是它仍然具有与System.Point相同的成员.

通过组合System.Point来实现NRowsColumns会更简单:

public class NRowsColumns
{
    private Point point;

    public int NRows
    {
        get { ... } // Code using point
        set { ... } // Code using point
    }

    ...
}

话说回来:

>我看不到Point确实与许多行和列有关.为什么不只有两个整数?
>我会在这里再次给您命名… N前缀是非常规的.我可能会称其为具有行和列的GridSize-尽管一般来说,即使这作为单独的类型似乎也没有必要. (为什么网格本身不通过Rows和Columns属性公开其大小?)

标签:naming,renaming,c
来源: https://codeday.me/bug/20191028/1949912.html