学习OOP的更多冒险经历 [英] More adventures in learning OOP

查看:59
本文介绍了学习OOP的更多冒险经历的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在探索OOP,我正在努力掌握基础知识。下面是

创建具有基本属性的基本Person的代码。


我还有一个Orders类,其中Orders由Persons制作。我有麻烦找出如何连接这两个,所以当一个新的订单是

创建时,我可以指定它所属的人。


这是代码......我怎么做去做一个属于一个人的订单......或者

我离开这里的基地吗?


谢谢!

Ron


使用System;


使用System.Collections.Generic;


使用System。文字;


命名空间ClassTesting


{


公共类人员


{


private string firstName;


private string lastName;


private地址mailingAddress;


private地址billingAddress;


private地址shippingAddress;


private电话主页电话;


私人电话手机;


公众人物()


{


this.mailingAddress = new Address();


this.billingAddress = new Address();

this.shippingAddress =新地址();


this.homePhone = new Phone();


this.cellPhone = new Phone();


}


公共地址ShippingAddress


{


get {return shippingAddress; }


}


公共地址账单地址


{


get {return billingAddress; }


}


公共地址邮件地址


{


get {return mailingAddress; }


}


公共电话HomePhone


{


get {return homePhone; }


}


公用电话手机


{


get {return cellPhone; }


}


公共字符串姓氏


{


get {return lastName; }


set {lastName = value; }


}


公共字符串FirstName


{


get {return firstName; }


set {firstName = value; }


}


}


公共类地址


{


private string line1;


私人字符串城市;


私人string state;


private string postalCode;


public string PostalCode


{


得到{return postalCode; }


set {postalCode = value; }


}


公共字符串状态


{


get {return state; }


set {state = value; }


}


公共字符串城市


{


获得{返回城市; }


set {city = value; }


}


公共字符串第1行


{


get {return line1; }


set {line1 = value; }


}


}


公共类电话


{


private int areaCode;


private int prefix;


private int postfix;


private int extension;


public int AreaCode


{


get {return areaCode; }


set {areaCode = value; }


}


public int前缀


{


get {return prefix; }


set {prefix = value; }


}


public int Postfix


{


get {return postfix; }


set {postfix = value; }


}


public int扩展


{


get {return extension; }


set {extension = value; }


}


公共字符串PhoneNumber


{


get {return"(" + this.AreaCode.ToString()+")" + this.Prefix.ToString()+

" - " + this.Postfix.ToString(); }


}


}



公共类订单


{


private int orderNumber;


private DateTime orderDate;


私人字符串数量;


私人字符串产品;


public int OrderNumber


{


get {return orderNumber; }


set {orderNumber = value; }


}


public DateTime OrderDate


{


get {return orderDate; }


set {orderDate = value; }


}


公共字符串数量


{


获得{返回数量; }


set {quantity = value; }


}


公共字符串产品


{


获得{返回产品; }


set {product = value; } b / b

}


}


I am exploring OOP and I am trying to get a grasp on the basics. Below is
the code that creates a basic Person with basic properties.

I also have an Orders Class where Orders are made by Persons. I am having
trouble figuring out how to "connect" the two so that when a new Order is
created I can specify the Person that it belongs to.

Here is the code...how do I go about making an Order belong to a Person...or
am I way off base here?

Thanks!
Ron

using System;

using System.Collections.Generic;

using System.Text;

namespace ClassTesting

{

public class Person

{

private string firstName;

private string lastName;

private Address mailingAddress;

private Address billingAddress;

private Address shippingAddress;

private Phone homePhone;

private Phone cellPhone;

public Person()

{

this.mailingAddress = new Address();

this.billingAddress = new Address();

this.shippingAddress = new Address();

this.homePhone = new Phone();

this.cellPhone = new Phone();

}

public Address ShippingAddress

{

get { return shippingAddress; }

}

public Address BillingAddress

{

get { return billingAddress; }

}

public Address MailingAddress

{

get { return mailingAddress; }

}

public Phone HomePhone

{

get { return homePhone; }

}

public Phone CellPhone

{

get { return cellPhone; }

}

public string LastName

{

get { return lastName; }

set { lastName = value; }

}

public string FirstName

{

get { return firstName; }

set { firstName = value; }

}

}

public class Address

{

private string line1;

private string city;

private string state;

private string postalCode;

public string PostalCode

{

get { return postalCode; }

set { postalCode = value; }

}

public string State

{

get { return state; }

set { state = value; }

}

public string City

{

get { return city; }

set { city = value; }

}

public string Line1

{

get { return line1; }

set { line1 = value; }

}

}

public class Phone

{

private int areaCode;

private int prefix;

private int postfix;

private int extension;

public int AreaCode

{

get { return areaCode; }

set { areaCode = value; }

}

public int Prefix

{

get { return prefix; }

set { prefix = value; }

}

public int Postfix

{

get { return postfix; }

set { postfix = value; }

}

public int Extension

{

get { return extension; }

set { extension = value; }

}

public string PhoneNumber

{

get { return "(" + this.AreaCode.ToString() + ")" + this.Prefix.ToString() +
"-" + this.Postfix.ToString(); }

}

}



public class Orders

{

private int orderNumber;

private DateTime orderDate;

private string quantity;

private string product;

public int OrderNumber

{

get { return orderNumber; }

set { orderNumber = value; }

}

public DateTime OrderDate

{

get { return orderDate; }

set { orderDate = value; }

}

public string Quantity

{

get { return quantity; }

set { quantity = value; }

}

public string Product

{

get { return product; }

set { product = value; }

}

}

}

推荐答案

你可能需要的是你订单中的一个属性叫(例如)

OrderedBy是Person类型。


当您创建订单时,您将分配Person属性为

下订单的人的实例。

What you probably need is a property in your order called (for example)
OrderedBy which is of type Person.

When you create your order, you would assign the Person property to be
the instance of the Person placing the order.


哦,这是糟糕的屁股!


谢谢!

" Chris Dunaway" <杜****** @ gmail.com>在消息中写道

news:11 ********************** @ g14g2000cwa.googlegr oups.com ...
Oh that is bad ass!

Thanks!
"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
你可能需要的是你订单中的一个属性叫(例如)
OrderedBy是Person类型。

当你创建订单时,你会分配Person属性作为下订单的人的实例。
What you probably need is a property in your order called (for example)
OrderedBy which is of type Person.

When you create your order, you would assign the Person property to be
the instance of the Person placing the order.



通常有两种定义所有权的方法。根据您的需要,

您可以使用其中一个或两个。


1)如果订单只属于一个人,那么您可以通过

进入构造函数的人的实例然后提供一个

proeprty来访问所有者。这样订单知道谁拥有它。这个

所有你在没有大量工作的情况下从订单到人。


公共类订单{

公共秩序(人物所有者){...}


公共人物所有者{get {...}}


私人订单m_owner = null; < br $>
}


2)你可以在Person类中提供一个集合(或一些列表)

包含所有列表命令。这允许你从人到订单。

下面是一个通用列表,但你总是可以通过

创建你自己的集合或使用泛型来强力输入。 br />

公共类人员{

public ArrayList Orders {get {...}}


private ArrayList m_orders =新的ArrayList();

}

现在,通过上述内容,您可以执行以下操作:


Person myPerson = new人();

myPerson.Orders.Add(新订单(myPerson));

存在这种变化,但这些是维持的两种主要方式
关系。


" RSH" < WA ************* @ yahoo.com>在消息中写道

news:%2 **************** @ TK2MSFTNGP14.phx.gbl ...
There are usually two ways of defining ownership. Depending on your needs,
you can use either one or both.

1) If an order belongs to only a single person, then you can pass an
instance of the person into the constructor for the order then provide a
proeprty to access the owner. This way the order knows who owns it. This
allwos you to go from order to person without a lot of work.

public class Order{
public Order( Person owner ){...}

public Person Owner{get{...}}

private Order m_owner = null;
}

2) You can provide a collection (or some list) in the Person class that
contains a list of all orders. This allows you to go from person to orders.
Below is a generic list, but you can always make it strongly typed by
creating your own collection or using generics.

public class Person{
public ArrayList Orders {get{...}}

private ArrayList m_orders = new ArrayList();
}
Now, with the above, you can do something like:

Person myPerson = new Person();
myPerson.Orders.Add(new Order(myPerson));
Variations on this exist, but these are the two primary ways to maintain
relationships.

"RSH" <wa*************@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...

我还有一个订单类,其中订单由人员制作。我无法弄清楚如何连接这两个,所以当创建一个新的订单时,我可以指定它所属的人。

这是代码...我如何让订单属于一个
有人......还是我离开基地?

谢谢!
Ron

使用系统;

使用System.Collections.Generic;

使用System.Text;

命名空间ClassTesting



公共类人员



私有字符串firstName;

私有字符串lastName;

私人地址mailingAddress;

私人地址billingAddress;

私人地址shippingAddress;

私人电话homePhone;

私人电话手机;

> public Person()

这个.mailingAddress =新地址();

this.billingAddress = new Address();

this.shippingAddress =新地址();

this.homePhone = new Phone();

this.cellPhone = new Phone(); < b公共地址ShippingAddress


获取{return shippingAddress; }


公共地址账单地址


获取{return billingAddress; }


公共地址邮件地址


获取{return mailingAddress;公共电话HomePhone


获取{return homePhone;公共电话CellPhone


获取{return cellPhone; }



公共字符串姓氏


获取{return lastName; }

设置{lastName = value; }



公共字符串FirstName


获取{return firstName; }

设置{firstName = value; }

}


公共课地址



私人字符串line1 ;

私有字符串城市;

私有字符串状态;

私有字符串postalCode;

公共字符串PostalCode

{

获得{return postalCode; }

设置{postalCode = value; }


公共字符串状态


获取{返回状态; }

设置{state = value; }


公共字符串城市


获得{返回城市; }

设置{city = value; }


公共字符串第1行


获取{return line1; }

设置{line1 = value; }

}


公共类电话



private int areaCode ;

私有int前缀;

private int postfix;

private int扩展;

public int AreaCode

{

获取{return areaCode; }

设置{areaCode = value; }


public int前缀


获取{返回前缀; }

设置{prefix = value; }


public int Postfix


获取{return postfix; }

设置{postfix = value; }


公共int扩展


获得{return extension; }

设置{extension = value; }


公共字符串PhoneNumber



get {return"(" + this.AreaCode .ToString()+")" + this.Prefix.ToString()
+" - " + this.Postfix.ToString();公共课程订单

{
>
private int orderNumber;

私人DateTime orderDate;

私人字符串数量;

私人字符串产品;

public int OrderNumber


获取{return orderNumber; }

设置{orderNumber = value; }


公共日期时间订单日期


获取{return orderDate; }

设置{orderDate = value; }


公共字符串数量


获取{返回数量; }

设置{quantity = value; }


公共字符串产品


获取{返回产品; }

设置{product = value; }





I am exploring OOP and I am trying to get a grasp on the basics. Below is
the code that creates a basic Person with basic properties.

I also have an Orders Class where Orders are made by Persons. I am having
trouble figuring out how to "connect" the two so that when a new Order is
created I can specify the Person that it belongs to.

Here is the code...how do I go about making an Order belong to a
Person...or am I way off base here?

Thanks!
Ron

using System;

using System.Collections.Generic;

using System.Text;

namespace ClassTesting

{

public class Person

{

private string firstName;

private string lastName;

private Address mailingAddress;

private Address billingAddress;

private Address shippingAddress;

private Phone homePhone;

private Phone cellPhone;

public Person()

{

this.mailingAddress = new Address();

this.billingAddress = new Address();

this.shippingAddress = new Address();

this.homePhone = new Phone();

this.cellPhone = new Phone();

}

public Address ShippingAddress

{

get { return shippingAddress; }

}

public Address BillingAddress

{

get { return billingAddress; }

}

public Address MailingAddress

{

get { return mailingAddress; }

}

public Phone HomePhone

{

get { return homePhone; }

}

public Phone CellPhone

{

get { return cellPhone; }

}

public string LastName

{

get { return lastName; }

set { lastName = value; }

}

public string FirstName

{

get { return firstName; }

set { firstName = value; }

}

}

public class Address

{

private string line1;

private string city;

private string state;

private string postalCode;

public string PostalCode

{

get { return postalCode; }

set { postalCode = value; }

}

public string State

{

get { return state; }

set { state = value; }

}

public string City

{

get { return city; }

set { city = value; }

}

public string Line1

{

get { return line1; }

set { line1 = value; }

}

}

public class Phone

{

private int areaCode;

private int prefix;

private int postfix;

private int extension;

public int AreaCode

{

get { return areaCode; }

set { areaCode = value; }

}

public int Prefix

{

get { return prefix; }

set { prefix = value; }

}

public int Postfix

{

get { return postfix; }

set { postfix = value; }

}

public int Extension

{

get { return extension; }

set { extension = value; }

}

public string PhoneNumber

{

get { return "(" + this.AreaCode.ToString() + ")" + this.Prefix.ToString()
+ "-" + this.Postfix.ToString(); }

}

}



public class Orders

{

private int orderNumber;

private DateTime orderDate;

private string quantity;

private string product;

public int OrderNumber

{

get { return orderNumber; }

set { orderNumber = value; }

}

public DateTime OrderDate

{

get { return orderDate; }

set { orderDate = value; }

}

public string Quantity

{

get { return quantity; }

set { quantity = value; }

}

public string Product

{

get { return product; }

set { product = value; }

}

}

}



这篇关于学习OOP的更多冒险经历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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