问题描述
public class B {
public String getMe()
{
return "Some";
}
}
假设我有一个上面的类,我们应该通过哪个参数决定使用什么?是还是有关系?
Assume that i have a above class , by which parameters should we decide what to use ?? Whether is a or Has a Relation ??
HAS-A
public class A {
public static void main(String args[])
{
B b = new B();
System.out.println(b.getMe());
}
}
或
public class A extends B
{
public static void main(String args[])
{
A b = new A();
System.out.println(b.getMe());
}
}
推荐答案
取决于逻辑关系.只是有道理.
Depends on the logical relation. It just needs to make sense.
示例:
假设您有动物类.
因此,您有以下类别:动物,狗,猫,豹,毛皮,脚
Lets say you have Animal classes.
So you have these classes: Animal, Dog, Cat , Leopard, Fur, Feet
猫和狗 IS A 动物.
豹 IS A 目录
动物 HAS A 毛皮,脚.
Cat and Dog IS A Animal.
Leopard IS A Cat.
Animal HAS A Fur, Feet.
简而言之:
IS A 关系表示您继承并扩展了基类的功能.
具有A 关系意味着该班级正在使用另一个班级,因此它已成为该班级的成员.
IS A relationship means you inherit and extend the functionality of the base class.
HAS A relationship means the class is using another class, so it has it as a member.
这篇关于如何确定使用IS A还是HAS关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!