Эй, ребята,
У меня проблема с пониманием процесса наследования в C #.Я работаю над домашней работой, и я хотел бы поделиться своим кодом, который я придумал.Я также включаю задачу.
Задача:
Work 1:
Develop a hierarchic structure of classes: shape, circle and cylinder:
Write the base class Shape which has two fields x and y coordinates The function
Display() returns a text string, which contains the point position.
The derived classes Circle and Cylinder inherit x , y coordinates and the method
Display() from base class Shape. You define the new necessary fields and methods
and you override the method Display() to return a text string with the coordinates,
the area and/or the volume.
The computing formulas are:
Circle area : p * r 2
Cylinder area: (2*p * r 2 ) + (2*p * r * h)
Cylinder volume: p * r 2 * h
И вот мой код:
Форма класса:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace A_Work_1
{
class Shape
{
public int xCoordinate = 0;
public int yCoordinate = 2;
public virtual void Display()
{
Console.WriteLine("The position of the point is: [{0};{1}].", xCoordinate, yCoordinate);
}
}
}
Class Circle:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace A_Work_1
{
class Circle : Shape
{
public override void Display()
{
double PI = Math.PI;
double radius = 2;
double circleArea = (PI * radius * radius);
Console.WriteLine("The area of the circle is: {0:F2}", circleArea);
}
}
}
Class Cylinder:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace A_Work_1
{
class Cylinder : Shape
{
public override void Display()
{
double PI = Math.PI;
double radius = 2;
double height = 5.5;
double cylinderArea = (2* PI * radius * radius) + (2 * PI * radius * height);
Console.WriteLine("The area of the cylinder is: {0:F2}", cylinderArea);
double cylinderVolume = (PI * radius * radius * height);
Console.WriteLine("The volume of the cylinder is: {0:F3}\n", cylinderVolume);
}
}
}
И основной класс:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace A_Work_1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("A: Work 1\n");
Shape position = new Shape();
position.Display();
Circle circleArea = new Circle();
circleArea.Display();
Cylinder cylinderArea = new Cylinder();
cylinderArea.Display();
}
}
}
Я хотел бы знать, где я неправильно понял.Мне немного сложно понять идею наследования.Как я могу улучшить это упражнение, чтобы выполнить задание?
Спасибо за ваши ответы!V.
EDIT:
Я отредактировал код, так что теперь должно быть правильное наследование.И последний вопрос.Где я должен поставить код Console.WriteLine, чтобы получить вывод?
Форма класса:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace A_Work_1
{
class Shape
{
public int xCoordinate = 0;
public int yCoordinate = 2;
public string Display()
{
string xCoordinateString = xCoordinate.ToString();
string yCoordinateString = yCoordinate.ToString();
return xCoordinateString + yCoordinateString;
}
}
}
Круг класса:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace A_Work_1
{
class Circle : Shape
{
public new string Display()
{
double PI = Math.PI;
double radius = 2;
double circleArea = (PI * radius * radius);
string circleAreaString = circleArea.ToString();
return circleAreaString;
}
}
}
Цилиндр класса:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace A_Work_1
{
class Cylinder : Circle
{
public string Display(double radius, double PI)
{
double height = 5.5;
double cylinderArea = (2* PI * radius * radius) + (2 * PI * radius * height);
string cylinderAreaString = cylinderArea.ToString();
double cylinderVolume = (PI * radius * radius * height);
string cylinderVolumeString = cylinderVolume.ToString();
return cylinderVolumeString + cylinderAreaString;
}
}
}
Основной класс остался без изменений.Спасибо, V.
ПОСЛЕДНИЕ РЕДАКТИРОВАТЬ:
Я изменил код основного класса, так что теперь есть код Console.WriteLine, как это:
Shape position = new Shape();
Console.WriteLine(position.Display());
Последняя проблема у меня естьявляется то, что когда я запускаю приложение, я получаю одно и то же число для Circle Area и Cylinder Area.Похоже, что метод Display в классе Cylinder использует возвращаемое значение из класса Circle.Как это возможно?