Hello. I have 4 classes in the program:

Basic Figure:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace FIGURES_V2 { class Figure : IComparable<Figure> { public virtual double getPerimeter() {return 0; } public int CompareTo(Figure f) { return this.Perimeter.CompareTo(f.Perimeter); } public static int X; public static int Y; public static int Z; public static double SideSize; public double Perimeter; public int[] Xcoord = new int[8]; public int[] Ycoord = new int[8]; public int[] Zcoord = new int[8]; public string Name; } } 

And derivatives, Round:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FIGURES_V2 { class Round : Figure { //public static int X; //public static int Y; //public static double SideSize; //public static double Perimeter; //public static int[] Xcoord = new int[4]; //public static int[] Ycoord = new int[4]; //public string Name; public override double getPerimeter() { return SideSize * Math.PI; } void getCoords() { Xcoord[0] = X; Ycoord[0] = Y + (Convert.ToInt32(SideSize) / 2); Xcoord[1] = X + (Convert.ToInt32(SideSize) / 2); Ycoord[1] = Y; Xcoord[2] = X; Ycoord[2] = Y - (Convert.ToInt32(SideSize) / 2); Xcoord[3] = X - (Convert.ToInt32(SideSize) / 2); Ycoord[3] = Y; } public Round(int sideSize, int x, int y) { X = x; Y = y; SideSize = sideSize; //getPerimeter(); getCoords(); } } } 

Cube:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FIGURES_V2 { class Cube : Figure { //public static int X; //public static int Y; //public static int Z; //public static double SideSize; //public static double Perimeter; //public static int[] Xcoord = new int[8]; //public static int[] Ycoord = new int[8]; //public static int[] Zcoord = new int[8]; //public string Name; public override double getPerimeter() { return SideSize * 8; } void getCoords() { Xcoord[0] = X - (Convert.ToInt32(SideSize) / 2); Ycoord[0] = Y + (Convert.ToInt32(SideSize) / 2); Zcoord[0] = Z - (Convert.ToInt32(SideSize) / 2); Xcoord[1] = X + (Convert.ToInt32(SideSize) / 2); Ycoord[1] = Y + (Convert.ToInt32(SideSize) / 2); Zcoord[1] = Z - (Convert.ToInt32(SideSize) / 2); Xcoord[2] = X + (Convert.ToInt32(SideSize) / 2); Ycoord[2] = Y - (Convert.ToInt32(SideSize) / 2); Zcoord[2] = Z - (Convert.ToInt32(SideSize) / 2); Xcoord[3] = X - (Convert.ToInt32(SideSize) / 2); Ycoord[3] = Y - (Convert.ToInt32(SideSize) / 2); Zcoord[3] = Z - (Convert.ToInt32(SideSize) / 2); Xcoord[4] = X - (Convert.ToInt32(SideSize) / 2); Ycoord[4] = Y + (Convert.ToInt32(SideSize) / 2); Zcoord[4] = Z + (Convert.ToInt32(SideSize) / 2); Xcoord[5] = X + (Convert.ToInt32(SideSize) / 2); Ycoord[5] = Y + (Convert.ToInt32(SideSize) / 2); Zcoord[5] = Z + (Convert.ToInt32(SideSize) / 2); Xcoord[6] = X + (Convert.ToInt32(SideSize) / 2); Ycoord[6] = Y - (Convert.ToInt32(SideSize) / 2); Zcoord[6] = Z + (Convert.ToInt32(SideSize) / 2); Xcoord[7] = X - (Convert.ToInt32(SideSize) / 2); Ycoord[7] = Y - (Convert.ToInt32(SideSize) / 2); Zcoord[7] = Z + (Convert.ToInt32(SideSize) / 2); } public Cube(int sideSize, int x, int y, int z) { X = x; Y = y; Z = z; SideSize = sideSize; getPerimeter(); getCoords(); } } } 

And Line:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FIGURES_V2 { class Line : Figure { //public static int X; //public static int Y; //public static double SideSize; //public static double Perimeter; //public static int[] Xcoord = new int[4]; //public static int[] Ycoord = new int[4]; //public string Name; public override double getPerimeter() { return SideSize; } void getCoords() { Xcoord[0] = X - (Convert.ToInt32(SideSize) / 2); Ycoord[0] = Y; Xcoord[1] = X + (Convert.ToInt32(SideSize) / 2); Ycoord[1] = Y; } public Line(int sideSize, int x, int y) { X = x; Y = y; SideSize = sideSize; //getPerimeter(); getCoords(); } } } 

In the main program, when reading from a file in a loop, objects with different parameters should be created.

File contents:

 round 10 2 3 round 5 4 6 round 3 5 -1 cube 4 -3 -2 3 cube 3 3 5 -2 cube 7 6 8 9 line 1 1 2 line 6 7 4 line 19 4 9 

The program itself:

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Collections; namespace FIGURES_V2 { class Program : IEnumerable { static Figure[] arr = new Figure[0]; static void Main(string[] args) { StreamReader sr = new StreamReader("File.txt"); string text = sr.ReadToEnd(); string[] figuresParam = text.Split(' '); List<Round> rounds = new List<Round>(); List<Cube> cubes = new List<Cube>(); List<Line> lines = new List<Line>(); for (int i = 0, j = 0, c = 0, l = 0, r = 0; i < figuresParam.Length; i += 4, j += 1) { if (figuresParam[i] == "round") { rounds.Add(new Round(Convert.ToInt32(figuresParam[i + 1]), Convert.ToInt32(figuresParam[i + 2]), Convert.ToInt32(figuresParam[i + 3]))); rounds[r].Name = "round"; r++; } if (figuresParam[i] == "cube") { cubes.Add(new Cube(Convert.ToInt32(figuresParam[i + 1]), Convert.ToInt32(figuresParam[i + 2]), Convert.ToInt32(figuresParam[i + 3]), Convert.ToInt32(figuresParam[i + 4]))); cubes[c].Name = "cube"; c++; i++; } if (figuresParam[i] == "line") { lines.Add(new Line(Convert.ToInt32(figuresParam[i + 1]), Convert.ToInt32(figuresParam[i + 2]), Convert.ToInt32(figuresParam[i + 3]))); lines[l].Name = "line"; l++; } } 

The result is this (the type of the figure and the perimeter are displayed): two muppets Tobish creates 3 identical objects of each class while the source data for each object differ from each other.

Why is this and how to fix it?

    1 answer 1

    I think the problem is that this is a static field.

      public static double SideSize; 

    And the perimeter calculation in the heirs is somehow like this:

     public override double getPerimeter() { return SideSize * 8; } 
    • thanks, it worked) and for 2 hours I tried to catch a mistake in the class of the heir, not looking at the parent at all - Artem Kichigin