English 中文(简体)
C# 继承——等级
原标题:C# inheritance -> hierarchy of classes

Hej guys,

我对C#中的继承过程有一个问题。 我正在从事家政工作,我愿赞同我提出的法典。 我也将这项任务包括在内。

The task:

    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

这是我制定的法典:

Class类:

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);
    }
}
}

班轮:

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);
    }

}
}

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}
", 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
");
        Shape position = new Shape();
        position.Display();

        Circle circleArea = new Circle();
        circleArea.Display();

        Cylinder cylinderArea = new Cylinder();
        cylinderArea.Display();

    }
}
}

我要知道,在什么地方我错了。 继承的想法对我来说是很难理解的。 我如何改进这项工作以完成任务?

感谢你的答复! 页: 1

EDIT:

我已经编辑了这部法典,现在应当有适当的继承。 最后一个问题。 如果是这样的话,我就把法典放在一起。 获得产出的写作能力?

Class类:

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;
    }
}
}

Main class remained unchanged. Thanks, V.

LAST EDIT:

现在有康索尔,我已经修改了主班法典。 诸如:

Shape position = new Shape();
Console.WriteLine(position.Display());

最后一个问题 我已经知道,在我管理申请时,我获得的圆环区和Cylinder区数目相同。 它像Cylinder中显示器法一样,正在使用同级圆环的回报值。 如何做到这一点?

最佳回答

最好把rad和高温带往班级,例如:

class Circle : Shape
{
   public double radius = 2;
   ...

class Cylinder : Circle
{
   public double height = 5.5;
   ...

And your Display() is not good now, it was better before the edit. It should be parameterless and it should override your base method. Display() in Shape:

public virtual string Display()
{
   return "[" + this.xCoordinate + ", " + this.yCoordinate + "]";
}

www.un.org/Depts/DGACM/index_spanish.htm

public override string Display()
{
   return base.Display() + Environment.NewLine + 
       "Area = " + (Math.PI * this.radius * this.radius).ToString();
}

www.un.org/Depts/DGACM/index_spanish.htm

public override string Display()
{
   return base.Display() + Environment.NewLine + 
       "Volume = " + (Math.PI * this.radius * this.radius * this.height).ToString();
}

撰写成果使用:

Circle myCircle = new Circle();
Console.WriteLine(myCircle.Display());

Cylinder myCylinder = new Cylinder();
Console.WriteLine(myCylinder.Display());
问题回答

我认为,大家更容易理解从真正的生活例子中继承遗产。

请允许我说,你有一个人,一个人有身份证和名字。 现在,请允许我说,有一份复印件,一份复印件正从一个人继承,为什么? 由于每个复印件都首先属于个人(他有一个名字和一个身份证),只有这样,如果他有其他EXTRA细节,如复印件和武器,你才能告诉他。

在OOP,一个继承类别(儿童类别)的特性与继承类别(父亲类别)相同,但如同你的家务一样,一个“圈子”在沙伯类之外具有更独特的足迹。 我们不是在圈子里再写所有形状,而是继承沙伯级,在圈子内增加额外的胎儿。

这使得我们和其他方案人员更容易理解软件目标是什么,以及它们如何相互连接,如何在“真实世界”中与物体相类似(如果你拥有管理劳动力的信息系统,你可以有一个父亲类别“工人”和孩子级监督员”,因为每个主管首先都是工人)。

希望能有所帮助。

在你具体的家务工作中,我将做些什么是改变遗产,以便有一个<>。 圆环正从形状中继承,圆环()正在继承。 由于每个弹.也是循环,具有额外特性。

Shapes是继承的典型课本。 不幸的是,这也是一个非常难以正确的例子。

继承的基本规则是,你可以形成与继承的亲子关系。

你可以说, rec是一种形式,你也可以说,广场是一种形状。 理论上,你也可以说,每一方都是一片 rec。 然而,如果你从直径中找到了广场,那么你就要与Lisskov。 广场是模拟而不是直径。

举这个例子(与条款有关的可能出现的问题的进一步解释)

public class Rectangle : Shape{
   public virtual int Width{get;set}
   public virtual int Height{get;set}
}

public class Square : Rectangle{
   public override int Width{get{return Height;} set{Height = value;}}
}

页: 1

rect.Height = 10;
rect.Width = 5;

第2行可能会改变视力,因为存在将一平方位换为平方的问题。

圆环和圆.也是如此。 有多少圆形的圈子——0.2”,你能够利用4个r圈的纸面? 有多少气球? 我没有计算出前者,但对后者的答复是没有的。 它具有3个层面,可在2个层面进行。 你们怎么能够根据圈子和气球高度的知识,建造一个yl子。 这种关系是以组成模式的。

在这种情况下,你可以做些什么。

abstract class Shape{
   public abstract double Area{get;} 
}

class Cylinder : Shape{
private Circle baseCircle;
private double height;

public override double Area{
get{//use height and baseCircle for the calculation left. 
    //Actual code left out because it s homework}
}

数量使用高峰和基流区计算数量的情况也是如此。

家庭工作问题请你从展示中恢复一段文字,目前你正在返回。 你们需要改变签名的方法,以恢复雕像,然后调整你的电话代码,以显示使用这种标识(Console.WriteLine)。

Each class that overrides shape should define it s own private properties for example circle should have a constant pi (because it never changes) and a property for the radius. This will allow you to do other operations on the circle without having to redefine these variables.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签