avatar
c*o
1
如果有两个类ab,注意不是接口,你想同时使用这2个类的功能,应该如何定义c类呢
avatar
g*g
2
You don't have to extend, you can encapsulate them.
Pass a and b in C's contructor and keep a and b as
instance variables.

【在 c**o 的大作中提到】
: 如果有两个类ab,注意不是接口,你想同时使用这2个类的功能,应该如何定义c类呢
avatar
c*o
3
thanks.
我就觉得题目比较模糊.不知道它是不是想考概念.
而且我觉得用instance variable.改变的永远是instance,跟c没关系啊.c的instance永远不能直接用ab
的功能.
avatar
g*g
4
Sure it can. Let's say a has a function test(), you can have a function
test() for c, all public api should be fine.
class C{
A a;
B b;
void test() {
a.test();
}
}

永远不能直接用ab

【在 c**o 的大作中提到】
: thanks.
: 我就觉得题目比较模糊.不知道它是不是想考概念.
: 而且我觉得用instance variable.改变的永远是instance,跟c没关系啊.c的instance永远不能直接用ab
: 的功能.

avatar
S*t
5
我猜 这个是考 is-a 和 has-a 的区别。
inheritance vs. composition

永远不能直接用ab

【在 c**o 的大作中提到】
: thanks.
: 我就觉得题目比较模糊.不知道它是不是想考概念.
: 而且我觉得用instance variable.改变的永远是instance,跟c没关系啊.c的instance永远不能直接用ab
: 的功能.

avatar
c*o
6
I got it.
avatar
t*e
7

generalization vs. aggregation (composition is a strong form of aggregation)

【在 S*********t 的大作中提到】
: 我猜 这个是考 is-a 和 has-a 的区别。
: inheritance vs. composition
:
: 永远不能直接用ab

avatar
c*t
8
is-a / has-a 其实并不是好的概念。这个东西的使用范围是 non-recursive
data 。所谓的 1:1,1:n 都是 relational database 上的概念。而 relational
database 的适用范围很窄。所以并不适合 class design 。
同时,表面上 inheritance 从定义上就是 is-a ,而 composition 就是
has-a,但是这两个概念并不是 mutually exclusive 的概念(在 relational
database 里则是)。
例子:
class A : implements B
{
private B b;
A () { this (null); }
A (B other) { b = other; }
public void action () { if (b == null) foo (); else b.action (); }
...
}
这种情况下,这种 composition 其实同时是 is-a 也是 has-a 。所以简单的
用 is-a / has-a

【在 S*********t 的大作中提到】
: 我猜 这个是考 is-a 和 has-a 的区别。
: inheritance vs. composition
:
: 永远不能直接用ab

avatar
t*e
9

What you described here is a Gof composite pattern.
Recursive relationship exists in relational data model.

【在 c*****t 的大作中提到】
: is-a / has-a 其实并不是好的概念。这个东西的使用范围是 non-recursive
: data 。所谓的 1:1,1:n 都是 relational database 上的概念。而 relational
: database 的适用范围很窄。所以并不适合 class design 。
: 同时,表面上 inheritance 从定义上就是 is-a ,而 composition 就是
: has-a,但是这两个概念并不是 mutually exclusive 的概念(在 relational
: database 里则是)。
: 例子:
: class A : implements B
: {
: private B b;

avatar
c*t
10
Nope. In relational database, recursive relationship is nothing
more than an attribute that refers to the primary key. It doesn't
mean 1:n or has-a relationship.
Being able to store is-a and has-a of the same type would mean
hierarchical database, which doesn't fit in relationship database
well. If you can recall, the attributes of hierarhical database
must be flattened to "abc / def / ghi" type of attributes to be
stored in relational databases.
Thus to be able to store is-a and has-a of sam

【在 t*******e 的大作中提到】
:
: What you described here is a Gof composite pattern.
: Recursive relationship exists in relational data model.

avatar
F*n
11
Containment and inheritance are two basic ways of reuse in OO.
In fact inheritance can be regarded as a special case of containment.
They are trying to test you OO concepts I guess.

永远不能直接用ab

【在 c**o 的大作中提到】
: thanks.
: 我就觉得题目比较模糊.不知道它是不是想考概念.
: 而且我觉得用instance variable.改变的永远是instance,跟c没关系啊.c的instance永远不能直接用ab
: 的功能.

avatar
F*n
12
你这个例子不对,IS A 对应 Class, conceptually是一个集合
Has-a 对应的是Instance,conceptually 是 set member
所以is-a和has-a至少在Java这种分Class和Object的语言来说是严格分开的。

【在 c*****t 的大作中提到】
: is-a / has-a 其实并不是好的概念。这个东西的使用范围是 non-recursive
: data 。所谓的 1:1,1:n 都是 relational database 上的概念。而 relational
: database 的适用范围很窄。所以并不适合 class design 。
: 同时,表面上 inheritance 从定义上就是 is-a ,而 composition 就是
: has-a,但是这两个概念并不是 mutually exclusive 的概念(在 relational
: database 里则是)。
: 例子:
: class A : implements B
: {
: private B b;

avatar
F*n
13
我觉得把JAVA的conceptual foundation说成relational DB肯定不对,你说的问题在
Relation DB中存在但在OO设计中基本上都能解决,JAVA有很好的OO模型相反
Relational DB才不具有OO概念,所以我们才需要EJB,SPRING。JAVA对应的应该是OODB。

【在 c*****t 的大作中提到】
: Nope. In relational database, recursive relationship is nothing
: more than an attribute that refers to the primary key. It doesn't
: mean 1:n or has-a relationship.
: Being able to store is-a and has-a of the same type would mean
: hierarchical database, which doesn't fit in relationship database
: well. If you can recall, the attributes of hierarhical database
: must be flattened to "abc / def / ghi" type of attributes to be
: stored in relational databases.
: Thus to be able to store is-a and has-a of sam

avatar
i*w
14
Adapter Pattern
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。