Создать класс, два полиморфных метода, конструктор С#

Модераторы: Hawk, Romeo, Absurd, DeeJayC, WinMain

Ответить
ЛевикТютькин

21 янв 2017, 07:27

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PraktikaTP
{
class Class : Class1
{
int[] a;

//возвращает строку с четными элементами
public override string method()
{
string str = "";
for (int i = 0; i < a.Length; i++)
{
if (a % 2 == 0)
{
str += a + " ";
}
}
return str;
}
public Class(int n)
{
Random r = new Random();
a = new int[r.Next(10) + 10];
for (int i = 0; i < a.Length; i++)
{
a = r.Next(10) + n;
}
}
}
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PraktikaTP
{
abstract class Class1
{
public abstract string method();
}
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PraktikaTP
{
class Class2 : Class1
{

int[] a;

//возвращает строку с нечетными элементами
public override string method()
{
string str = "";
for (int i = 0; i < a.Length; i++)
{
if (a % 2 == 1)
{
str += a + " ";
}
}
return str;
}

public Class2(int n)
{
Random r = new Random();
a = new int[r.Next(10) + 10];
for (int i = 0; i < a.Length; i++)
{
a = r.Next(10) + n;
}
}

}
}
ЛевикТютькин

21 янв 2017, 07:28

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PraktikaTP
{
public partial class Form1 : Form
{
Class arr;
public Form1()
{
InitializeComponent();
}

private void buttonCreateObj_Click(object sender, EventArgs e)
{
int n=0;
while (n < 10)
{
try
{
n = Convert.ToInt32(textBoxRazmer.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (n < 10)
{
MessageBox.Show("Введите размер массива больше десяти!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
else
{
MessageBox.Show("Ok", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

arr = new Class(n);
}

private void buttonMethod1_Click(object sender, EventArgs e)
{
richTextBoxRes.Text = arr.method();
}

private void buttonMethod2_Click(object sender, EventArgs e)
{
richTextBoxRes.Text = arr.method(1);
}
}
}
ЛевикТютькин

21 янв 2017, 07:28

namespace PraktikaTP
{
partial class Form1
{
/// <summary>
/// Требуется переменная конструктора.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Освободить все используемые ресурсы.
/// </summary>
/// <param name="disposing">истинно, если управляемый ресурс должен быть удален; иначе ложно.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Код, автоматически созданный конструктором форм Windows

/// <summary>
/// Обязательный метод для поддержки конструктора - не изменяйте
/// содержимое данного метода при помощи редактора кода.
/// </summary>
private void InitializeComponent()
{
this.labelRasmer = new System.Windows.Forms.Label();
this.textBoxRazmer = new System.Windows.Forms.TextBox();
this.buttonCreateObj = new System.Windows.Forms.Button();
this.buttonMethod1 = new System.Windows.Forms.Button();
this.buttonMethod2 = new System.Windows.Forms.Button();
this.richTextBoxRes = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
//
// labelRasmer
//
this.labelRasmer.AutoSize = true;
this.labelRasmer.Location = new System.Drawing.Point(23, 18);
this.labelRasmer.Name = "labelRasmer";
this.labelRasmer.Size = new System.Drawing.Size(96, 13);
this.labelRasmer.TabIndex = 0;
this.labelRasmer.Text = "Размер массива:";
//
// textBoxRazmer
//
this.textBoxRazmer.Location = new System.Drawing.Point(125, 15);
this.textBoxRazmer.Name = "textBoxRazmer";
this.textBoxRazmer.Size = new System.Drawing.Size(100, 20);
this.textBoxRazmer.TabIndex = 1;
//
// buttonCreateObj
//
ЛевикТютькин

21 янв 2017, 07:31

this.buttonCreateObj.Location = new System.Drawing.Point(125, 45);
this.buttonCreateObj.Name = "buttonCreateObj";
this.buttonCreateObj.Size = new System.Drawing.Size(100, 27);
this.buttonCreateObj.TabIndex = 2;
this.buttonCreateObj.Text = "Создать массив";
this.buttonCreateObj.UseVisualStyleBackColor = true;
this.buttonCreateObj.Click += new System.EventHandler(this.buttonCreateObj_Click);
//
// buttonMethod1
//
this.buttonMethod1.Location = new System.Drawing.Point(24, 45);
this.buttonMethod1.Name = "buttonMethod1";
this.buttonMethod1.Size = new System.Drawing.Size(95, 27);
this.buttonMethod1.TabIndex = 3;
this.buttonMethod1.Text = "Метод \"1\"";
this.buttonMethod1.UseVisualStyleBackColor = true;
this.buttonMethod1.Click += new System.EventHandler(this.buttonMethod1_Click);
//
// buttonMethod2
//
this.buttonMethod2.Location = new System.Drawing.Point(24, 78);
this.buttonMethod2.Name = "buttonMethod2";
this.buttonMethod2.Size = new System.Drawing.Size(95, 27);
this.buttonMethod2.TabIndex = 4;
this.buttonMethod2.Text = "Метож \"2\"";
this.buttonMethod2.UseVisualStyleBackColor = true;
this.buttonMethod2.Click += new System.EventHandler(this.buttonMethod2_Click);
//
// richTextBoxRes
//
this.richTextBoxRes.Location = new System.Drawing.Point(231, 15);
this.richTextBoxRes.Name = "richTextBoxRes";
this.richTextBoxRes.Size = new System.Drawing.Size(312, 90);
this.richTextBoxRes.TabIndex = 5;
this.richTextBoxRes.Text = "Результат";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(553, 141);
this.Controls.Add(this.richTextBoxRes);
this.Controls.Add(this.buttonMethod2);
this.Controls.Add(this.buttonMethod1);
this.Controls.Add(this.buttonCreateObj);
this.Controls.Add(this.textBoxRazmer);
this.Controls.Add(this.labelRasmer);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.Label labelRasmer;
private System.Windows.Forms.TextBox textBoxRazmer;
private System.Windows.Forms.Button buttonCreateObj;
private System.Windows.Forms.Button buttonMethod1;
private System.Windows.Forms.Button buttonMethod2;
private System.Windows.Forms.RichTextBox richTextBoxRes;
}
}
Ответить