Skip to content

Интерфейсы Мария Попова #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: Marija_Popova
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions CourseApp.Tests/InterfaceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using Xunit;

namespace CourseApp.Tests
{
public class InterfaceTests
{
[Fact]
public void Test1()
{
Assert.True(true);
}

[Fact]
public void IComparable()
{
Animal[] mass = new Animal[3];
mass[0] = new Cat("Name1", 6, true, true);
mass[1] = new Mouse("Name2", 1, false, true);
mass[2] = new Cat("Name3", 3, false, true);
Array.Sort(mass);
Assert.Equal("Name2", mass[0].Name);
Assert.Equal("Name3", mass[1].Name);
Assert.Equal("Name1", mass[2].Name);
}
}
}
7 changes: 6 additions & 1 deletion CourseApp/Animal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace CourseApp
{
public abstract class Animal
public abstract class Animal : IComparable<Animal>
{
private int age;

Expand Down Expand Up @@ -51,5 +51,10 @@ public override string ToString()
}

public abstract string Voice();

public int CompareTo(Animal an)
{
return this.Age.CompareTo(an.Age);
}
}
}
6 changes: 4 additions & 2 deletions CourseApp/Cat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace CourseApp
{
public class Cat : Animal
public class Cat : Animal, IFacts
{
public Cat()
: this("Неизвестно")
Expand Down Expand Up @@ -58,5 +58,7 @@ public override string Voice()
{
return "Meow";
}

void IFacts.Facts() => Console.WriteLine("Сердце кошки бьется около 140 ударов в минуту.\nМяуканьем кошки пытаются обратить внимание человека.\n Усы, необходимы кошке для перемещения в пространстве.");
}
}
}
7 changes: 7 additions & 0 deletions CourseApp/IFacts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace CourseApp
{
public interface IFacts
{
void Facts();
}
}
24 changes: 18 additions & 6 deletions CourseApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;

namespace CourseApp
Expand All @@ -7,8 +7,8 @@ public class Program
{
public static double MyFunction(double a, double x)
{
var y = Math.Pow(Math.Log10(a + x), 2) / Math.Pow(a + x, 2);
return y;
var y = Math.Pow(Math.Log10(a + x), 2) / Math.Pow(a + x, 2);
return y;
}

public static List<double> TaskA(double a, double xn, double xk, double dx)
Expand Down Expand Up @@ -62,16 +62,28 @@ public static void Main(string[] args)

Console.WriteLine();

Animal[] masss = new Animal[2];
masss[0] = new Mouse("Larisa", 2, false, true);
masss[1] = new Cat("Kyza", 7, true, true);
Animal[] masss = new Animal[3];
masss[0] = new Cat("Kyza", 7, true, true);
masss[1] = new Mouse("Larisa", 2, false, true);
masss[2] = new Cat("Murzik", 4, true, false);
foreach (var item in masss)
{
Console.WriteLine(item.ToString());
Console.WriteLine(item.Voice());
Console.WriteLine();
}

Array.Sort(masss);
Console.WriteLine("Отсортированный по возрасту массив:");
foreach (Animal an in masss)
{
Console.WriteLine($"{an.Name} - {an.Age}");
}

Console.WriteLine("================================");

IFacts barsik = new Cat("Barsik", 5, true, true);
barsik.Facts();
Console.ReadLine();
}
}
Expand Down