将现有父对象添加到扩展子对象? [英] adding existing parent object to extended child object?

查看:66
本文介绍了将现有父对象添加到扩展子对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的NewsGroupers,


我对OOP相对较新,cannet可以解决这个问题。

我有两节课。 Class Child扩展Parent。

Child类没有构造函数。因此,当我创建子对象

时,将调用父对象的构造函数。这很好。


但现在我遇到了一个问题,我想添加一个已经存在的Parent

对象来创建一个新的Child对象。怎么办呢?


TIA


pablo


Dear NewsGroupers,

I am relatively new to OOP and cannet get my head around this problem.
I have two classes. Class Child extends Parent.
There is no constructor for the Child class. So when I create a child object
the constructor for the parent object is called. That works fine.

But now I have the problem that I want to add an already existing Parent
object to create a new Child object. How can this be done?

TIA

pablo


推荐答案

一旦你扩展了一个(父)类并创建了一个(子)子类,它就是

根本无法将另一个父类添加到该子类中。这对所有OO语言都适用

,而不仅仅是PHP。每个孩子班只能有一个孩子的b $ b $

你想要做的是什么?


-

Tony Marston

http: //www.tonymarston.net


" pablo" < PA **** @ exeit.removethis.demon.nl>在消息中写道

新闻:10 ************* @ corp.supernews.com ...
Once you have extended a (parent) class and created a (child) subclass it is
simply not possible to add another parent to that child class. This applies
to all OO languages, not just PHP. Each child class can have only one
parent.

What is it exactly that you are trying to do?

--
Tony Marston

http://www.tonymarston.net

"pablo" <pa****@exeit.removethis.demon.nl> wrote in message
news:10*************@corp.supernews.com...
亲爱的NewsGroupers,
我对OOP比较陌生,cannet可以解决这个问题。
我有两节课。 Class Child扩展Parent。
Child类没有构造函数。因此,当我创建一个子
对象时,将调用父对象的构造函数。这很好。

但是现在我遇到的问题是我想添加一个已经存在的Parent
对象来创建一个新的Child对象。怎么办呢?

TIA

pablo
Dear NewsGroupers,

I am relatively new to OOP and cannet get my head around this problem.
I have two classes. Class Child extends Parent.
There is no constructor for the Child class. So when I create a child object the constructor for the parent object is called. That works fine.

But now I have the problem that I want to add an already existing Parent
object to create a new Child object. How can this be done?

TIA

pablo




" Tony Marston" <至** @ NOSPAM.demon.co.uk>在消息中写道

news:ca ****************** @ news.demon.co.uk ...

"Tony Marston" <to**@NOSPAM.demon.co.uk> wrote in message
news:ca******************@news.demon.co.uk...
一旦扩展了(父)类并创建了(子)子类,就不能将
添加到该子类中。这个
适用于所有OO语言,而不仅仅是PHP。每个子类只能有一个父级。
Once you have extended a (parent) class and created a (child) subclass it is simply not possible to add another parent to that child class. This applies to all OO languages, not just PHP. Each child class can have only one
parent.




父类是相同的。

假设父类是人类儿童班是人员。

我已经创造了一些人,并希望将这些添加到人员中


pablo



The parent class is the same.
Suppose the parent class is humans and the child class is personnel.
I already have created some humans and want to add these to the personnel

pablo


Tony Marston写道:
Tony Marston wrote:
一旦你扩展了一个(父)类并创建了一个(子)子类,就不可能再添加另一个父类了儿童班。这适用于所有OO语言,而不仅仅是PHP。每个孩子班只能有一个孩子。
Once you have extended a (parent) class and created a (child) subclass it is
simply not possible to add another parent to that child class. This applies
to all OO languages, not just PHP. Each child class can have only one
parent.




Tony,我希望这不是你的意思吗?许多(非神秘的)OOPL提供了多元继承的
(C ++,Python,Common Lisp ......)。


< op>

现在,PHP只支持单一继承。但是继承

远不是OOP的alpha和ome。聚合|组合+

委托是另一种重用代码的方法(好吧,这意味着有更多的代码可以写b
,但是这很简单......) />

ex:


class Parent

{

function parent(



Tony, I hope this is not what you meant ? Many (non cryptic) OOPLs offer
multiple inheritence (C++, Python, Common Lisp...).

<op>
Now it''s true that PHP only support single inheritence. But inheritence
is far from being the alpha and omega of OOP. Aggregation|composition +
delegation is another way to reuse code (ok, this means having more code
to write, but what, this is straightforward...)

ex :

class Parent
{
function Parent(


这篇关于将现有父对象添加到扩展子对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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