对象中创建对象的类 [英] Creating a Class of Objects within an Object

查看:143
本文介绍了对象中创建对象的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有什么似乎是一个简单的问题,但由于某些原因,我有让我的大脑有左右在它与多个对象的对象的概念问题。例如,假设我们已经有一个头,并与之间的多个对象页脚中的对象。

就像一份报告,标题将有姓名和地址。页脚将有一个总的项目,凡买了。在这两者之间是有零件编号,说明和价格的行项目。

我想我可以有与页眉,页脚和行项目对象的数组,都用自己的属性的对象。我使用的报告为例,因为它是我能想到的,能接触解释我的问题的唯一概念。

可有人请给我一个链接或如何创建这种类型的对象(S)的解释。

我使用VS 2010和VB.net,我可以转换从C#到VB。

 报表对象
       标题对象
         物业名称
         物业日期
       结束
       LineItem的()数组对象
         物业编号
         物业部分描述
         项目物业数目
         地产每件价格
         物业总价
       结束
       页脚对象
         地产总项目数
         物业总价
       结束
     结束


解决方案

杰夫,在C#中,并在其最基础的:

 公共类报告
{
    //在报告中典型的简单属性
    公共字符串ReportUid {搞定;组; }
    //对象的属性
    公共标题标题{搞定;组; }
    大众健美{搞定;组; }
    公共页脚页脚{搞定;组; }    公开报告()
    {
        标题=新标题();
        车身=新的机构();
        尾部=新页脚();
    }    内部空隙CalculateFooterTotals()
    {
        // summerize在页脚了LineItem值
        Footer.TotalItems = Body.LineItems
            .SUM(X => x.Quantity);
        Footer.TotalPrice = Body.LineItems
            .SUM(X => x.Total);
    }
}公共类头
{
    公共字符串名称{;组; }
    公众的DateTime日期{搞定;组; }
}公共类体
{
    公众的IList<&LineItem的GT; {了LineItem获得;组; }    公共机构()
    {
        了LineItem =新的List<&LineItem的GT;();
    }
}公共类的LineItem
{
    公共字符串PARTNUMBER {搞定;组; }
    公共字符串PartDescription {搞定;组; }
    公众诠释数量{搞定;组; }
    公众持股量ITEMPRICE {搞定;组; }
    //计算
    公众持股量合计
    {
        {返回数量* ITEMPRICE; }
    }
}公共类页脚
{
    //通过report.CalculateFooterTotals填充()
    公众诠释TOTALITEMS {搞定;内部设置; }
    公众持股量TotalPrice {搞定;内部设置; }
}

在一些属性,当然计算,而不是获取/设置。

- 认为它会是很好的做法,加了一点用法,因为我看到你问道格拉斯这个问题(从数据库或其他来源更可能):

  //使用 - 建立报告
VAR报告=新的报告{
    ReportUid = Guid.NewGuid()的ToString()
    标题=
    {
        NAME =我的新报告
        日期= DateTime.UtcNow
    }};
//添加了LineItem身体(在实际情况下,可能是一个循环)
report.Body.LineItems.Add(新的LineItem()
    {
        数量= 1,
        ITEMPRICE = 12.30f,
        PartDescription =新鞋,
        部分号码=SHOE123
    });
report.Body.LineItems.Add(新的LineItem()
    {
        数量= 3,
        ITEMPRICE = 2.00f,
        PartDescription =老鞋,
        部分号码=SHOE999
    });
report.Body.LineItems.Add(新的LineItem()
    {
        数量= 7,
        ITEMPRICE = 0.25f,
        PartDescription =经典红袜队
        部分号码=SOX567
    });
// summerize在页脚了LineItem值
report.CalculateFooterTotals();

现在申请报告,以你的画布表面(HTML等)

 私有静态无效DispalyData(报告报道)
{
    //设置了基本知识
    Console.WriteLine(头);
    Console.WriteLine(report.ReportUid);
    Console.WriteLine(report.Header.Date);
    Console.WriteLine(report.Header.Name);    //现在循环圆体项目
    Console.WriteLine(项目);
    的foreach(在report.Body.LineItems VAR订单项)
    {
        Console.WriteLine(新项目---);
        Console.WriteLine(lineItem.PartDescription);
        Console.WriteLine(lineItem.Quantity);
        Console.WriteLine(lineItem.ItemPrice);
        Console.WriteLine(lineItem.PartNumber);
        Console.WriteLine(lineItem.Total);
        Console.WriteLine(最终产品---);
    }    //显示页脚项目
    Console.WriteLine(页脚);
    Console.WriteLine(report.Footer.TotalItems);
    Console.WriteLine(report.Footer.TotalPrice);
}//调用code为:
DispalyData(报告);

希望这会扫描确定...它推到社区维基(通过修改),因为它是一个普遍追捧的话题了。

[BTW] - 本书虽然你会知道C#来vb.net转换器,我想这一次,它看起来pretty前途:的 http://www.developerfusion.com/tool​​s/convert/csharp-to-vb

I have what might seem like a simple question, but for some reason I am having a problem getting my brain around the concept of having a object with multiple object with in it. For example lets say we have a object with a header and a footer with multiple objects in between.

Like a report, the header would have the name and address. The footer would have a total of the item that where bought. In between would be the line items with the part number, description and a price.

I guess the I can have a object with an header, footer and an array of line item object, all with their own properties. I am using a report as the example, because it’s the only concept that I can think of that will get close to explaining my question.

Can someone please send me a link to or explain on how to create this type of object(s).

I am using VS 2010 and VB.net and I can translate from C# to VB.

    Report Object
       Header Object
         Property Name
         Property Date
       End
       LineItem() Array Object
         Property Part Number
         Property Part Description
         Property Number of Items
         Property Per Item Price
         Property Total price
       End 
       Footer Object
         Property Total Items count
         Property Total Price
       End
     End

解决方案

Jeff, in c# and at its most basic:

public class Report
{
    // typical simple property in report
    public string ReportUid { get; set; }
    // object properties
    public Header Header { get; set; }
    public Body Body { get; set; }
    public Footer Footer { get; set; }

    public Report()
    {
        Header = new Header();
        Body = new Body();
        Footer = new Footer();
    }

    internal void CalculateFooterTotals()
    {
        // summerize the lineitems values in the footer
        Footer.TotalItems = Body.LineItems
            .Sum(x => x.Quantity);
        Footer.TotalPrice = Body.LineItems
            .Sum(x => x.Total);
    }
}

public class Header
{
    public string Name { get; set; }
    public DateTime Date { get; set; }
}

public class Body
{
    public IList<LineItem> LineItems { get; set; }

    public Body()
    {
        LineItems = new List<LineItem>();
    }
}

public class LineItem
{
    public string PartNumber { get; set; }
    public string PartDescription { get; set; }
    public int Quantity { get; set; }
    public float ItemPrice { get; set; }
    // computed 
    public float Total
    {
        get { return Quantity * ItemPrice; }
    }
}

public class Footer
{
    // populated via report.CalculateFooterTotals()
    public int TotalItems { get; internal set; }
    public float TotalPrice { get; internal set; }
}

Some of the properties are of course computed, rather than get/set.

[edit] - thought it'd be good practice to add a bit of usage, as I saw you ask Douglas this question (more than likely from DB or other source):

// usage - set up report
var report = new Report {
    ReportUid = Guid.NewGuid().ToString(),
    Header =
    {
        Name = "My new report", 
        Date = DateTime.UtcNow
    }};
// add lineitems to body (in real case, probably a loop)
report.Body.LineItems.Add(new LineItem()
    {
        Quantity = 1,
        ItemPrice = 12.30f,
        PartDescription = "New shoes", 
        PartNumber = "SHOE123"
    });
report.Body.LineItems.Add(new LineItem()
    {
        Quantity = 3, 
        ItemPrice = 2.00f, 
        PartDescription = "Old shoes", 
        PartNumber = "SHOE999"
    });
report.Body.LineItems.Add(new LineItem()
    {
        Quantity = 7, 
        ItemPrice = 0.25f, 
        PartDescription = "Classic Sox", 
        PartNumber = "SOX567"
    });
// summerize the lineitems values in the footer
report.CalculateFooterTotals();

now apply report to your canvas surface (html etc..)

private static void DispalyData(Report report)
{
    // set out the basics
    Console.WriteLine("Header");
    Console.WriteLine(report.ReportUid);
    Console.WriteLine(report.Header.Date);
    Console.WriteLine(report.Header.Name);

    // now loop round the body items
    Console.WriteLine("Items");
    foreach (var lineItem in report.Body.LineItems)
    {
        Console.WriteLine("New Item---");
        Console.WriteLine(lineItem.PartDescription);
        Console.WriteLine(lineItem.Quantity);
        Console.WriteLine(lineItem.ItemPrice);
        Console.WriteLine(lineItem.PartNumber);
        Console.WriteLine(lineItem.Total);
        Console.WriteLine("End Item---");
    }

    // display footer items
    Console.WriteLine("Footer");
    Console.WriteLine(report.Footer.TotalItems);
    Console.WriteLine(report.Footer.TotalPrice);
}

// called in code as:
DispalyData(report);

Hope this scans ok... pushed it to community wiki (via edits), as it's a universally sought after topic.

[btw] - altho you'll be aware of c# to vb.net convertors, I tried this one and it looks pretty promising: http://www.developerfusion.com/tools/convert/csharp-to-vb

这篇关于对象中创建对象的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆