java的几道基础选择题222. Which statement is true about has-a and is-a relationships?A.Inheritancerepresents an is-a relationship.B.Inheritance represents a has-a relationship.C.Interfaces must be use when creating a has-arelationship.D.Inst
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/19 08:36:56
java的几道基础选择题222. Which statement is true about has-a and is-a relationships?A.Inheritancerepresents an is-a relationship.B.Inheritance represents a has-a relationship.C.Interfaces must be use when creating a has-arelationship.D.Inst
java的几道基础选择题2
22. Which statement is true about has-a and is-a relationships?
A.Inheritancerepresents an is-a relationship.
B.Inheritance represents a has-a relationship.
C.Interfaces must be use when creating a has-arelationship.
D.Instance variables must be used when creatingan is-a relationship.
选A
21. Given:
public abstract interface Frobnicate{
public void twiddle(String s);
}
Which is a correct class?
A.public abstract class Frob implements Frobnicate { public abstract void twiddle(String s) { } }
B.public abstractclass Frob implements Frobnicate { }
C.public class Frob extends Frobnicate { public void twiddle(Integer i) { } }
D.public class Frob implements Frobnicate { public void twiddle(Integer i) { } }
选B
为什么不懂,求例子,
java的几道基础选择题222. Which statement is true about has-a and is-a relationships?A.Inheritancerepresents an is-a relationship.B.Inheritance represents a has-a relationship.C.Interfaces must be use when creating a has-arelationship.D.Inst
.“is a”代表的是类之间的继承关系,比如PC机是计算机,工作站也是计算机.PC机和工作站是两种不同类型的计算机,但都继承了计算机的共同特性.因此在用 Java语言实现时,应该将PC机和工作站定义成两种类,均继承计算机类.“has a”代表的是对象和它的成员的从属关系.同一种类的对象,通过它们的属性的不同值来区别.比如一台PC机的操作系统是Windows,另一台PC机的操作系统是Linux.操作系统是PC机的一个成员变量,根据这一成员变量的不同值,可以区分不同的PC机对象.再比如张三和李四都是人,但他们的名字不一样,可以以此区分这两个具体的人.名字应该作为人的成员变量.如果将名字叫“张三”的人和名字叫“李四”的人分别定义成两个类,均继承“人”这个类,显然是不合理的.
关于第二题,继承接口的只能是接口,所以c不对.
在实现接口时,实体类要完全实现所有的抽象方法,而d中的方法参数是integer类型的所以不对.
抽象方法不能有方法体,所以A不对.
在实现接口时,抽象类可以实现也可以不实现所有的抽象方法,所以B正确.