- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
- 4、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
- 5、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们。
- 6、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
- 7、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
第1部分 Java语言
第1部分 Java语言;继承是面向对象编程技术的一块基石,因为它允许创建分等级层次的类。
运用继承,你能够创建一个通用类,它定义了一系列相关项目的一般特性。
该类可以被更具体的类继承,每个具体的类都增加一些自己特有的东西。
被继承的类叫超类(superclass)。
继承超类的类叫子类(subclass)。
;8.1 继承的基础;例 P 134 一个超类A和一个名为B的子类;class SimpleInheritance {
public static void main(String args[]) {
A superOb = new A();
B subOb = new B();
// The superclass may be used by itself.
superOb.i = 10;
superOb.j = 20;
System.out.println(Contents of superOb: );
superOb.showij();
System.out.println();
/* The subclass has access to all public members of its superclass. */
subOb.i = 7;
subOb.j = 8;
subOb.k = 9;
System.out.println(Contents of subOb: );
subOb.showij();
subOb.showk();
System.out.println();
System.out.println(Sum of i, j and k in subOb:);
subOb.sum();
}
};Ans:;8.1.1 成员的访问和继承;例 P135 不能访问超类中被声明成 private 的成员;// As j is not accessible here.
class B extends A {
int total;
void sum() {
total = i + j; // ERROR, j is not accessible here
}
}
class Access {
public static void main(String args[]) {
B subOb = new B();
subOb.setij(10, 12);
subOb.sum();
System.out.println(Total is + subOb.total);
}
};8.1.2 更实际的例子 Box 类扩展成员weight; // constructor used when no dimensions specified
Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}
// constructor used when cube is created
Box(double len) {
width = height = depth = len;
}
// compute and return volume
double volume() {
return width * height * depth;
}
};class BoxWeight extends Box {
double weight; // weight of box
// constructor for BoxWeight
BoxWeight(double w, double h, double d, double m) {
width = w;
height = h;
depth = d;
weight = m;
}
};class DemoBoxWeight {
public static void main(String args[]) {
BoxWeight mybox1 = new BoxWeight(10, 20, 15, 34.3);
BoxWeight mybox2 = new BoxWeight(2, 3, 4, 0.076);
double vol;
vol = mybox1.volume();
您可能关注的文档
- 朝代排序(Dynastic ordering).doc
- 服饰洗涤常识(Common sense of dress washing).doc
- 朝代顺序(Dynastic order).doc
- 材料介绍(Material introduction).doc
- 材料作文审题训练宁为玉碎,不为瓦全(Material compositionsmoderators training a better life).doc
- 材料作文15篇(Material composition 15).doc
- 材料作文 孔子有个学生看到一小孩掉进湍急的河里(A student of Confucius saw a child fall into a swift river).doc
- 材料力学例题及(Examples of mechanics of materials and).doc
- 材料期刊----接收难易、影响因子、一审周期(Material journal ---- receiving difficulty, influence factor and first trial cycle).doc
- 材料型作文审题角度例析_5526(Material compositionsmoderators angle of _5526).doc
文档评论(0)