Inside C# 2nd Edition 边看边记 (3)

第五章

1.属性

没什么值得提的……………………

2.多维数组与数组的数组

第二个的名字听着很别扭,我也不知道叫什么好,书里叫Jagged Arrays

首先是多维数组,没有什么特别的,就是定义的时候和c++有不同
double[,,] numbers;这个定义了一个三维的数组,知道数组的维数挺容易的,数数有几个逗号再加1就可以了
再看看怎么使用多维数组

using System;

class MultiDimArrayApp
{
    protected int currentMonth;
    protected double[,] sales;

    MultiDimArrayApp()

    {
        currentMonth=10;

        sales = new double[2, currentMonth];
        for (int i = 0; i < sales.GetLength(0); i++)
        {
            for (int j=0; j < 10; j++)
            {
                sales[i,j] = (i * 100) + j;
            }
        }
    }

    protected void PrintSales()
    {
        Console.WriteLine("Printing a multi-dimensional " +
                          "array of numbers.../n");
        for (int i = 0; i < sales.GetLength(0); i++)
        {
            for (int j=0; j < sales.GetLength(1); j++)
            {
                Console.WriteLine("[{0}][{1}]={2}", i,
                    j, sales[i,j]);
            }
        }
    }
 
    public static void Main()
    {
        MultiDimArrayApp app = new MultiDimArrayApp();
        app.PrintSales();
    }
}

需要注意的地方就是那个GetLength函数,如果你使用Length属性获得数组的长度话,对于单维数组来说,和GetLength的结果相同
但是对于多维的数组,Length获得的是整个的长度,也就是个各个维数的积,对应上面的例子是20,但是用GetLength(n)获得的就是
第n维的长度了。
ps:获得数组的维数使用Rank属性

再看数组的数组,感觉和c++里面的vector差不多,用于把其他的数组再组织起来的东东
这个是定义int[][] jaggedArray;和多维数组不同吧
再来个小例子吧
using System;

class Control
{
    virtual public void SayHi()
    {
        Console.WriteLine("base control class");
    }
}

class Button : Control
{
    override public void SayHi()
    {
        Console.WriteLine("button control");
    }
}

class Combo : Control
{
    override public void SayHi()
    {
        Console.WriteLine("combobox control");
    }
}

class JaggedArrayApp
{
    public static void Main()
    {
    // Define a two-dimensional array as a "parent" array.
    // The first dimension represents how many arrays
    // this parent array will contain.
    Control[][] controls;
    controls = new Control[2][];

    // The first dimension will contain three controls...
    controls[0] = new Control[3];
    for (int i = 0; i < controls[0].Length; i++)
    {
        controls[0][i] = new Button();
    }

    // The second dimension will contain two controls...
    controls[1] = new Control[2];
    for (int i = 0; i < controls[1].Length; i++)
    {
        controls[1][i] = new Combo();
    }

    // Now I can simply iterate through a nested for loop
    // and using polymorphism access all my controls'
    // members!
    for (int i = 0; i < controls.Length;i++)
    {
        for (int j=0;j< controls[i].Length;j++)
        {
            Control control = controls[i][j];
            control.SayHi();
        }
    }       
}

输出的结果是
button control
button control
button control
combobox control
combobox control


3.Indexer象数组一样对待你的类

c#的新特性,看看例子就知道了

using System;
using System.Collections;

class MyListBox

{
    protected ArrayList data = new ArrayList();

    public object this[int idx]
    {
        get
        {
            if (idx > -1 && idx < data.Count)
            {
                return (data[idx]);
            }
            else
            {
                throw new InvalidOperationException("[MyListBox.set_Item]" +
                    " Index out of range");
            }
        }
        set
        {
            if (idx > -1 && idx < data.Count)
            {
                data[idx] = value;
            }
            else if (idx == data.Count)
            {
                data.Add(value);
            }
            else
            {
                throw new InvalidOperationException(
                    "[MyListBox.get_Item] Index out of range");
            }
        }
    }
}

class IndexersApp
{
    public static void Main()
    {
        MyListBox lbx = new MyListBox();
        lbx[0] = "foo";
        lbx[1] = "bar";
        lbx[2] = "baz";
        Console.WriteLine("{0} {1} {2}",
            lbx[0], lbx[1], lbx[2]);
    }
}

this关键字这里用来表示indexer。此时MyListBox这个类就像是一个数组一样了。在内部是如何实现的呢?
编译器会给MyListBox加上一个属性叫Item,一下就是调用这个属性了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值