自引用对象 [英] Self referencing object

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

问题描述

首先是几条信息:


*我有一个名为Folder的类,它代表一个

数据库表中的一行数据。事物的数据访问方面不是问题。


*该表有一个引用自身的父列(即邻接

或父/子模型)


*我有一个名为''Parent'的公共属性,它返回一个新的

对包含父行数据的Folder实例的引用。

例如。


公共文件夹父{

get {


/ /如果父级可用

if(_iParent> 0){


//创建文件夹对象的新实例,引用

父母

_parent =新文件夹(_iParent);

}


返回_parent;


}


}


*这意味着我能够访问当前文件夹parent''s ,

父母这样的名字(文件夹是文件夹的一个实例):


folder.Parent.Parent.Name;

首先,我是否走在正确的轨道上?这是建议使用自引用类来构建

的方法吗?是否有一个设计模式覆盖这个

关闭?


其次,文件夹是一个抽象的概念。在实践中,我实际上将要处理一个ImageFolder或UserFolder。我想将

文件夹作为一个抽象类,然后创建其他类,这些类可以从文件夹继承
。问题是,当我引用

父属性时,我需要返回一个新的

ImageFolder / UserFolder实例,而不是文件夹。


希望这是有道理的!


干杯,


macka。

解决方案

Macka,


我相信你走在正确的轨道上。只要你不等于
不断返回一个新的文件夹实例,让你的实例获得第一个

时间(对于你的操作限制,那就是),你应该

没问题。


至于抽象类,你只需拥有你的父母财产

返回一个抽象类的类型。只要抽象类具有在派生类之间共享的

属性,您就可以了(比如

name属性)。只需创建适当的实例然后返回

它,派生类将自动转换为基类。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- ni ************** @ exisconsulting.com


" Macka" ; < ja*@mcguinness.co.uk>在消息中写道

news:f1 ************************* @ posting.google.co m ... < blockquote class =post_quotes>首先是几条信息:

*我有一个名为Folder的类,它代表
数据库表中的一行数据。事物的数据访问方面不是问题。

*该表有一个引用自身的父列(即邻接
或父/子模型)

> *我有一个名为''Parent'的公共属性,它返回一个新的
引用,包含父行的数据。
例如。

公共Folder Parent {

//如果父级可用
if(_iParent> 0){

//创建一个新实例文件夹对象引用
父文件
_parent = new文件夹(_iParent);


返回_parent;

}

}

*这意味着我能够像这样访问当前文件夹的父母','
父母'的名字(文件夹是一个实例)文件夹):

folder.Parent.Parent.Name;

首先,我是否在正确的轨道上?这是构建自引用类的推荐方法吗?有没有一个设计模式覆盖这个
关闭?

其次,文件夹是一个抽象的概念。在实践中,我实际上将处理ImageFolder或UserFolder。我想将
Folder作为一个抽象类,然后创建其他可以从Folder继承的类。问题是,当我引用
Parent属性时,我需要返回一个新的
ImageFolder / UserFolder实例,而不是文件夹。

希望这会使感觉!

干杯,

macka。



你可以寻找Singleton模式。

在编写Parent属性的方式中有它的味道。


Fred


Macka,


仅供参考,这被称为反身关系或递归

关系

这样的布局提供了一个树形结构,如果你还记得CSCI 201

,你的将是一个n-ary树,所以你可以使用递归来获得所有的

遍历方法。最底层的元素是叶子节点,最顶层的元素是根节点...


JIM

" Macka" < ja*@mcguinness.co.uk>在消息中写道

news:f1 ************************* @ posting.google.co m ... < blockquote class =post_quotes>首先是几条信息:

*我有一个名为Folder的类,它代表
数据库表中的一行数据。事物的数据访问方面不是问题。

*该表有一个引用自身的父列(即邻接
或父/子模型)

> *我有一个名为''Parent'的公共属性,它返回一个新的
引用,包含父行的数据。
例如。

公共Folder Parent {

//如果父级可用
if(_iParent> 0){

//创建一个新实例文件夹对象引用
父文件
_parent = new文件夹(_iParent);


返回_parent;

}

}

*这意味着我能够像这样访问当前文件夹的父母','
父母'的名字(文件夹是一个实例)文件夹):

folder.Parent.Parent.Name;

首先,我是否在正确的轨道上?这是构建自引用类的推荐方法吗?有没有一个设计模式覆盖这个
关闭?

其次,文件夹是一个抽象的概念。在实践中,我实际上将处理ImageFolder或UserFolder。我想将
Folder作为一个抽象类,然后创建其他可以从Folder继承的类。问题是,当我引用
Parent属性时,我需要返回一个新的
ImageFolder / UserFolder实例,而不是文件夹。

希望这会使感觉!

干杯,

macka。



A few pieces of information first:

* I have a class called Folder which represents a row of data in a
database table. The data access side of things is not an issue.

* The table has a parent column which references itself (ie. Adjacency
or parent/child model)

* I have a public property called ''Parent'' which returns me a new
reference to a Folder instance containing the data of the parent row.
eg.

public Folder Parent {
get {

//if the parent is available
if (_iParent > 0) {

//create a new instance of the folder object referring to the
parent
_parent = new Folder(_iParent);
}

return _parent;

}

}

* This means that I am able to access the current folders parent''s,
parent''s name like this (folder is an instance of Folder):

folder.Parent.Parent.Name;
Firstly, am I on the right track? Is this the recommended way to build
a self referencing class? Is there a design pattern which covers this
off ?

Secondly, Folder is an abstract concept. In practise I will actually
be dealing with an ImageFolder or a UserFolder. I''d like to make
Folder an abstract class, then create these other classes which can
inherit from Folder. The problem is that when I''m referencing the
Parent property, I need to be returning a new instance of the
ImageFolder/UserFolder rather than Folder.

Hope this makes sense!

Cheers,

macka.

解决方案

Macka,

I believe you are on the right track with this. As long as you don''t
continuously return a new folder instance, getting an instance the first
time you are using (for the limit of your operations, that is), you should
be fine.

As for the abstract class, you will just have your Parent property
return a type of the abstract class. As long as the abstract class has the
properties that are shared among the derived classes, you will be fine (like
the name property). Just create the appropriate instance and then return
it, the derived class will be cast up automatically to the base class.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- ni**************@exisconsulting.com

"Macka" <ja*@mcguinness.co.uk> wrote in message
news:f1*************************@posting.google.co m...

A few pieces of information first:

* I have a class called Folder which represents a row of data in a
database table. The data access side of things is not an issue.

* The table has a parent column which references itself (ie. Adjacency
or parent/child model)

* I have a public property called ''Parent'' which returns me a new
reference to a Folder instance containing the data of the parent row.
eg.

public Folder Parent {
get {

//if the parent is available
if (_iParent > 0) {

//create a new instance of the folder object referring to the
parent
_parent = new Folder(_iParent);
}

return _parent;

}

}

* This means that I am able to access the current folders parent''s,
parent''s name like this (folder is an instance of Folder):

folder.Parent.Parent.Name;
Firstly, am I on the right track? Is this the recommended way to build
a self referencing class? Is there a design pattern which covers this
off ?

Secondly, Folder is an abstract concept. In practise I will actually
be dealing with an ImageFolder or a UserFolder. I''d like to make
Folder an abstract class, then create these other classes which can
inherit from Folder. The problem is that when I''m referencing the
Parent property, I need to be returning a new instance of the
ImageFolder/UserFolder rather than Folder.

Hope this makes sense!

Cheers,

macka.



You could look for the Singleton pattern.
There''s a taste of it in your way of coding the Parent property.

Fred


Macka,

FYI, this is called either a ''Reflexive Relationship'' or ''Recursive
Relationship''

Such a layout provides a tree structure, and if you remember CSCI 201
yours would be a n-ary tree so you can use recursion for all your
traversal methods. Bottom most elements are leaf nodes, top most
elements are root nodes ...

JIM
"Macka" <ja*@mcguinness.co.uk> wrote in message
news:f1*************************@posting.google.co m...

A few pieces of information first:

* I have a class called Folder which represents a row of data in a
database table. The data access side of things is not an issue.

* The table has a parent column which references itself (ie. Adjacency
or parent/child model)

* I have a public property called ''Parent'' which returns me a new
reference to a Folder instance containing the data of the parent row.
eg.

public Folder Parent {
get {

//if the parent is available
if (_iParent > 0) {

//create a new instance of the folder object referring to the
parent
_parent = new Folder(_iParent);
}

return _parent;

}

}

* This means that I am able to access the current folders parent''s,
parent''s name like this (folder is an instance of Folder):

folder.Parent.Parent.Name;
Firstly, am I on the right track? Is this the recommended way to build
a self referencing class? Is there a design pattern which covers this
off ?

Secondly, Folder is an abstract concept. In practise I will actually
be dealing with an ImageFolder or a UserFolder. I''d like to make
Folder an abstract class, then create these other classes which can
inherit from Folder. The problem is that when I''m referencing the
Parent property, I need to be returning a new instance of the
ImageFolder/UserFolder rather than Folder.

Hope this makes sense!

Cheers,

macka.



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

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