設計了一個MyArray類,希望在輸入不同的陣列引數(一維陣列、二維陣列和交錯陣列)時,實作建構式的多載,代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class MyArray
{
public int no;
public string[] numbers;
public string[,] names;
public string[][] scores;
public MyArray()
{
this.no = 0;
}
public MyArray(string[] numbers)
{
this.no = 1;
this.numbers = numbers;
}
public MyArray(string[,] names)
{
this.no = 2;
this.names = names;
}
public MyArray(string[][] scores)
{
this.no = 3;
this.scores = scores;
}
class Program
{
static void Main(string[] args)
{
string[] mf3 = { "c", "c++", "c#" };
MyArray myArray1 = new MyArray();
myArray1.no = 1;
myArray1.numbers = mf3;
Console.WriteLine("呼叫默認建構式輸出一維陣列numbers");
foreach (string i in mf3) { System.Console.WriteLine(i); }
string[,] siblings = new string[,] { { "Mike", "Amy" }, { "Mary", "Albert" } };
MyArray myArray2 = new MyArray(string[,] siblings); //編譯時這一句不能通過
Console.WriteLine("呼叫帶引數的建構式輸出多維陣列names");
foreach (string i in siblings) { Console.Write("{0} ", i); }
}
}
}
}
以上代碼在編譯時不能通過,請問何故?
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/152999.html
標籤:C#
