Перегрузка конструкторов класса - C#
Формулировка задачи:
Помогите, необходимо реализовать 1-2 перегруженных конструкторов класса. Желательно перегрузить класс "Program".
Вот сам код, который нужно модифицировать:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace lab7
{
class Matrix
{
private float Sum;
int[,] a;
public Matrix(int M, int N)
{
a = new int[M, N];
}
public void InputMatrix()
{
Random r = new Random();
for (int i = 0; i < a.GetLength(0); i++)
for (int j = 0; j < a.GetLength(1); j++)
a[i, j] = (int)r.Next(10);
}
public override string ToString()
{
string s = null;
for (int i = 0; i < a.GetLength(0); i++, s += "\n")
for (int j = 0; j < a.GetLength(1); j++)
s += a[i, j] + " ";
return s;
}
public void OutputMatrix()
{
string s = this.ToString();
Console.WriteLine(s);
}
static public Matrix operator ~(Matrix m)
{
Matrix newM = new Matrix(m.a.GetLength(1),
m.a.GetLength(0));
for (int i = 0; i < m.a.GetLength(0); i++)
for (int j = 0; j < m.a.GetLength(1); j++)
newM.a[j, i] = m.a[i, j];
return newM;
}
public float SrArth()
{
for (int i = 0; i < a.GetLength(0); i++)
{
Sum = 0;
for (int j = 0; j < a.GetLength(1); j++)
{
Sum += a[i, j];
}
} return Sum / a.GetLength(1);
}
public void PrintFI()
{
string str = " |-***********************-| \n" +
" |* *| \n" +
" |* Объект матрица M x N *| \n" +
" |* *| \n" +
" |-***********************-| \n";
Console.WriteLine(str);
Console.WriteLine("Кол-во строк = {0}", a.GetLength(0));
Console.WriteLine("Кол-во столбцов = {0}", a.GetLength(1));
Console.WriteLine("Сред. арифм. всех элементов исходной матрицы = {0}", SrArth());
}
class Program
{
static void Main(string[] args)
{
Console.Title = "Матрица M x N";
Console.ForegroundColor = ConsoleColor.Yellow;
Console.BackgroundColor = ConsoleColor.Red;
Console.Clear();
Console.WriteLine("Введите M =");
int M = int.Parse(Console.ReadLine());
Console.WriteLine("Введите N =");
int N = int.Parse(Console.ReadLine());
Console.WriteLine();
Matrix x, z;
x = new Matrix(M, N);
Console.WriteLine("Исх. матрица:");
x.InputMatrix();
x.OutputMatrix();
x.PrintFI();
Console.WriteLine();
Console.WriteLine("Транспонир. матрица:");
z = ~x;
z.OutputMatrix();
Console.ReadKey();
}
}
}
}Решение задачи: «Перегрузка конструкторов класса»
textual
Листинг программы
int[,] arr = {{1,2,3},{4,5,6},{7,8,9}};
var matrix = new Matrix(arr);
arr[1,1] = 100500;
Console.WriteLine(matrix);
// 1 2 3
// 4 100500 6
// 7 8 9