Наследование методов. Не работают два аналогичных метода - C#
Формулировка задачи:
Доброго времени суток! Пытаюсь реализовать наследование методов, и вот вроде и решил свою задачу, но выяснилось, что второй метод (shiftY), абсолютно аналогичный первому (shiftX). В чем может быть загвоздка?
Скриншот консоли:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{ //создаем точки
Point3D p1 = new Point3D(4, 3, 1);
Point3D p2 = new Point3D(5, 7, 9);
Console.WriteLine("Points initialized. First point (4, 3, 1), second point (5, 7, 9), third point (1,2), fourth point (2,5)...");
//////
Console.WriteLine("Shift first point (start position (4,3,1)");
Console.WriteLine("Input shift for X");
float sX = float.Parse(Console.ReadLine());
p1.ShiftX(sX);
p1.sendXPosition();//Все хорошо
float sY = float.Parse(Console.ReadLine());
p1.ShiftY(sY);
p1.sendYPosition();//Не работает
float sZ = float.Parse(Console.ReadLine());
p1.ShiftZ(sZ);
p1.sendZPosition();//Все нормально
Console.ReadKey();
}
}
class Point2D
{
float x, y;
public Point2D(float x, float y)
{
this.x = x;
this.y = y;
}
public virtual void ShiftX(float shiftx) //сдвигает точку по Х на величину аргумента
{
Console.WriteLine("Shift point by X on {0}", shiftx);
this.x += shiftx;
}
public virtual void ShiftY(float shifty)
{
Console.WriteLine("Shift point by Y on {0}", shifty);
this.x += shifty;
}
public virtual void sendXPosition()
{
Console.WriteLine("Position by X= " + this.x);
}
public virtual void sendYPosition()
{
Console.WriteLine("Position by X= " + this.y);
}
}
class Point3D : Point2D
{
float x, y, z;
public Point3D(float x, float y, float z) : base(x, y)
{
this.x = x;
this.y = y;
this.z = z;
}
public override void ShiftX(float shiftx2)
{
base.ShiftX(shiftx2);
}
public override void ShiftY(float shifty2)
{
base.ShiftY(shifty2);
}
public void ShiftZ(float shiftz)
{
this.z += shiftz;
}
public override void sendXPosition()
{
base.sendXPosition();
}
public override void sendYPosition()
{
base.sendYPosition();
}
public void sendZPosition()
{
Console.WriteLine("Position by Z= " + this.z);
}
}
}Решение задачи: «Наследование методов. Не работают два аналогичных метода»
textual
Листинг программы
public virtual void ShiftY(float shifty)
{
Console.WriteLine("Shift point by Y on {0}", shifty);
this.x += shifty;
}