¼Ì³ÐÊÇÃæÏò¶ÔÏó±à³Ì¼¼ÊõµÄÒ»¿é»ùʯ£¬ÒòΪËüÔÊÐí´´½¨·ÖµÈ¼¶²ã´ÎµÄÀà¡£ÔËÓü̳У¬ÄãÄܹ»´´½¨Ò»¸öͨÓÃÀ࣬Ëü¶¨ÒåÁËһϵÁÐÏà¹ØÏîÄ¿µÄÒ»°ãÌØÐÔ¡£¸ÃÀà¿ÉÒÔ±»¸ü¾ßÌåµÄÀà¼Ì³Ð£¬Ã¿¸ö¾ßÌåµÄÀ඼Ôö¼ÓһЩ×Ô¼ºÌØÓеĶ«Î÷¡£ÔÚJava ÊõÓïѧÖУ¬±»¼Ì³ÐµÄÀà½Ð³¬Àࣨsuperclass £©£¬¼Ì³Ð³¬ÀàµÄÀà½Ð×ÓÀࣨsubclass £©¡£Òò´Ë£¬×ÓÀàÊdz¬ÀàµÄÒ»¸öרÃÅÓÃ;µÄ°æ±¾£¬Ëü¼Ì³ÐÁ˳¬ÀඨÒåµÄËùÓÐʵÀý±äÁ¿ºÍ·½·¨£¬²¢ÇÒΪËü×Ô¼ºÔöÌíÁ˶ÀÌصÄÔªËØ¡£
¼Ì³ÐÒ»¸öÀֻ࣬ҪÓÃextends ¹Ø¼ü×Ö°ÑÒ»¸öÀàµÄ¶¨ÒåºÏ²¢µ½ÁíÒ»¸öÖоͿÉÒÔÁË¡£ÎªÁËÀí½âÔõÑù¼Ì³Ð£¬ÈÃÎÒÃÇ´Ó¼ò¶ÌµÄ³ÌÐò¿ªÊ¼¡£ÏÂÃæµÄÀý×Ó´´½¨ÁËÒ»¸ö³¬ÀàAºÍÒ»¸öÃûΪBµÄ×ÓÀà¡£×¢ÒâÔõÑùÓùؼü×Öextends À´´´½¨AµÄÒ»¸ö×ÓÀà¡£
// A simple example of inheritance.
// Create a superclass.
class A {
int i, j;
void showij() { System.out.println("i and j: " + i + " " + j); }}
// Create a subclass by extending class A.
class B extends A {
int k;
void showk() {
System.out.println("k: " + k); } void sum() {
System.out.println("i+j+k: " + (i+j+k));
}
}
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();
}
}
¸Ã³ÌÐòµÄÊä³öÈçÏ£º
Contents of superOb:
i and j: 10 20
Contents of subOb:
i and j: 7 8
k: 9
Sum of i, j and k in subOb:
i+j+k: 24
ÏñÄãËù¿´µ½µÄ£¬×ÓÀàB°üÀ¨ËüµÄ³¬ÀàAÖеÄËùÓгÉÔ±¡£ÕâÊÇΪʲôsubOb ¿ÉÒÔ»ñÈ¡iºÍj ÒÔ¼°µ÷ÓÃshowij( ) ·½·¨µÄÔÒò¡£Í¬Ñù£¬sum( ) ÄÚ²¿£¬iºÍj¿ÉÒÔ±»Ö±½ÓÒýÓ㬾ÍÏñËüÃÇÊÇBµÄÒ»²¿·Ö¡£
¾¡¹ÜAÊÇBµÄ³¬À࣬ËüÒ²ÊÇÒ»¸öÍêÈ«¶ÀÁ¢µÄÀà¡£×÷Ϊһ¸ö×ÓÀàµÄ³¬Àಢ²»Òâζ×ų¬À಻Äܱ»×Ô¼ºÊ¹Ó᣶øÇÒ£¬Ò»¸ö×ÓÀà¿ÉÒÔÊÇÁíÒ»¸öÀàµÄ³¬Àà¡£ÉùÃ÷Ò»¸ö¼Ì³Ð³¬ÀàµÄÀàµÄͨ³£ÐÎʽÈçÏ£º
class subclass-name extends superclass-name {
// body of class
}
ÄãÖ»ÄܸøÄãËù´´½¨µÄÿ¸ö×ÓÀඨÒåÒ»¸ö³¬Àà¡£Java ²»Ö§³Ö¶à³¬ÀàµÄ¼Ì³Ð£¨ÕâÓëC++ ²»Í¬£¬ÔÚC++ÖУ¬Äã¿ÉÒԼ̳жà¸ö»ù´¡Àࣩ¡£Äã¿ÉÒÔ°´Õչ涨´´½¨Ò»¸ö¼Ì³ÐµÄ²ã´Î¡£¸Ã²ã´ÎÖУ¬Ò»¸ö×ÓÀà³ÉΪÁíÒ»¸ö×ÓÀàµÄ³¬ÀࡣȻ¶ø£¬Ã»ÓÐÀà¿ÉÒÔ³ÉΪËü×Ô¼ºµÄ³¬Àà¡£
8.1.1 ³ÉÔ±µÄ·ÃÎʺͼ̳Ð
¾¡¹Ü×ÓÀà°üÀ¨³¬ÀàµÄËùÓгÉÔ±£¬Ëü²»ÄÜ·ÃÎʳ¬ÀàÖб»ÉùÃ÷³Éprivate µÄ³ÉÔ±¡£ÀýÈ磬¿¼ÂÇÏÂÃæ¼òµ¥µÄÀà²ã´Î½á¹¹£º
/* In a class hierarchy, private members remain private to their class.
This program contains an error and will not
compile.
*/
// Create a superclass.
class A {int i; // public by default private int j; // private to A
void setij(int x, int y) { i = x; j = y;
}
}
// A's 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);
}
}
¸Ã³ÌÐò²»»á±àÒ룬ÒòΪBÖÐsum( ) ·½·¨ÄÚ²¿¶ÔjµÄÒýÓÃÊDz»ºÏ·¨µÄ¡£¼ÈÈ»j±»ÉùÃ÷³Éprivate£¬ËüÖ»Äܱ»Ëü×Ô¼ºÀàÖеÄÆäËû³ÉÔ±·ÃÎÊ¡£×ÓÀàûȨ·ÃÎÊËü¡£
×¢Ò⣺һ¸ö±»¶¨Òå³Éprivate µÄÀà³ÉԱΪ´ËÀà˽ÓУ¬Ëü²»Äܱ»¸ÃÀàÍâµÄËùÓдúÂë·ÃÎÊ£¬°üÀ¨×ÓÀà¡£
8.1.2 ¸üʵ¼ÊµÄÀý×Ó
ÈÃÎÒÃÇ¿´Ò»¸ö¸üʵ¼ÊµÄÀý×Ó£¬¸ÃÀý×ÓÓÐÖúÓÚ²ûÊö¼Ì³ÐµÄ×÷Óá£ÕâÀǰÃæÕ½ڸĽøµÄBoxÀàµÄ×îºó°æ±¾½«±»À©Õ¹¡£Ëü°üÀ¨µÚËijÉÔ±ÃûΪweight ¡£ÕâÑù£¬ÐµÄÀཫ°üº¬Ò»¸öºÐ×ӵĿí¶È¡¢¸ß¶È¡¢Éî¶ÈºÍÖØÁ¿¡£
// This program uses inheritance to extend Box.
class Box { double width; double height; double depth;
// construct clone of an object
Box(Box ob) { // pass object to constructor width = ob.width; height = ob.height; depth = ob.depth;
}
// constructor used when all dimensions specified
Box(double w, double h, double d) { width = w; height = h; depth = d;
}
// 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; }}
// Here, Box is extended to include weight.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(); System.out.println("Volume of mybox1 is " + vol); System.out.println("Weight of mybox1 is " + mybox1.weight); System.out.println();
vol = mybox2.volume(); System.out.println("Volume of mybox2 is " + vol); System.out.println("Weight of mybox2 is " + mybox2.weight);
}
}
¸Ã³ÌÐòµÄÊä³öÏÔʾÈçÏ£º
Volume of mybox1 is 3000.0
Weight of mybox1 is 34.3
Volume of mybox2 is 24.0
Weight of mybox2 is 0.076
BoxWeight ¼Ì³ÐÁËBox µÄËùÓÐÌØÕ÷²¢Îª×Ô¼ºÔöÌíÁËÒ»¸öweight ³ÉÔ±¡£Ã»ÓбØÒªÈÃBoxWeight ÖØд´½¨Box ÖеÄËùÓÐÌØÕ÷¡£ÎªÂú×ãÐèÒªÎÒÃÇÖ»ÒªÀ©Õ¹Box¾Í¿ÉÒÔÁË¡£
¼Ì³ÐµÄÒ»¸öÖ÷ÒªÓÅÊÆÔÚÓÚÒ»µ©ÄãÒѾ´´½¨ÁËÒ»¸ö³¬À࣬¶ø¸Ã³¬ÀඨÒåÁËÊÊÓÃÓÚÒ»×é¶ÔÏóµÄÊôÐÔ£¬Ëü¿ÉÓÃÀ´´´½¨ÈκÎÊýÁ¿µÄ˵Ã÷¸ü¶àϸ½ÚµÄ×ÓÀࡣÿһ¸ö×ÓÀàÄܹ»ÕýºÃÖÆ×÷Ëü×Ô¼ºµÄ·ÖÀà¡£ÀýÈ磬ÏÂÃæµÄÀà¼Ì³ÐÁËBox²¢Ôö¼ÓÁËÒ»¸öÑÕÉ«ÊôÐÔ£º
// Here, Box is extended to include color.
class ColorBox extends Box {
int color; // color of box
ColorBox(double w, double h, double d, int c) { width = w; height = h; depth = d; color = c;
}
}
¼Çס£¬Ò»µ©ÄãÒѾ´´½¨ÁËÒ»¸ö¶¨ÒåÁ˶ÔÏóÒ»°ãÊôÐԵij¬À࣬¸Ã³¬Àà¿ÉÒÔ±»¼Ì³ÐÒÔÉú³ÉÌØÊâÓÃ;µÄÀࡣÿһ¸ö×ÓÀàÖ»ÔöÌíËü×Ô¼º¶ÀÌصÄÊôÐÔ¡£ÕâÊǼ̳еı¾ÖÊ¡£
8.1.3 ³¬Àà±äÁ¿¿ÉÒÔÒýÓÃ×ÓÀà¶ÔÏó
³¬ÀàµÄÒ»¸öÒýÓñäÁ¿¿ÉÒÔ±»ÈκδӸó¬ÀàÅÉÉúµÄ×ÓÀàµÄÒýÓø³Öµ¡£Ä㽫·¢Ïּ̳еÄÕâ¸ö·½ÃæÔںܶàÌõ¼þÏÂÊǺÜÓÐÓõġ£ÀýÈ磬¿¼ÂÇÏÂÃæµÄ³ÌÐò£º
class RefDemo {
public static void main(String args[]) { BoxWeight weightbox = new BoxWeight(3, 5, 7, 8.37); Box plainbox = new Box(); double vol;
vol = weightbox.volume(); System.out.println("Volume of weightbox is " + vol); System.out.println("Weight of weightbox is " +
weightbox.weight); System.out.println();
// assign BoxWeight reference to Box reference
plainbox = weightbox;
vol = plainbox.volume(); // OK, volume() defined in Box
System.out.println("Volume of plainbox is " + vol);
/* The following statement is invalid because plainbox does not define a weight member. */ // System.out.println("Weight of plainbox is " + plainbox.weight); }}
ÕâÀweightbox ÊÇBoxWeight ¶ÔÏóµÄÒ»¸öÒýÓã¬plainbox ÊÇBox¶ÔÏóµÄÒ»¸öÒýÓ᣼ÈÈ»BoxWeight ÊÇBoxµÄÒ»¸ö×ÓÀ࣬ÔÊÐíÓÃÒ»¸öweightbox ¶ÔÏóµÄÒýÓøøplainbox ¸³Öµ¡£
Àí½âÊÇÒýÓñäÁ¿µÄÀàÐÍ----¶ø²»ÊÇÒýÓöÔÏóµÄÀàÐÍ----¾ö¶¨ÁËʲô³ÉÔ±¿ÉÒÔ±»·ÃÎÊ¡£Ò²¾ÍÊÇ˵£¬µ±Ò»¸ö×ÓÀà¶ÔÏóµÄÒýÓñ»¸³¸øÒ»¸ö³¬ÀàÒýÓñäÁ¿Ê±£¬ÄãÖ»ÄÜ·ÃÎʳ¬ÀඨÒåµÄ¶ÔÏóµÄÄÇÒ»²¿·Ö¡£ÕâÊÇΪʲôplainbox ²»ÄÜ·ÃÎÊweight µÄÔÒò£¬ÉõÖÁÊÇËüÒýÓÃÁËÒ»¸öBoxWeight ¶ÔÏóÒ²²»ÐС£×ÐϸÏëÒ»Ï룬ÕâÊÇÓеÀÀíµÄ£¬ÒòΪ³¬À಻֪µÀ×ÓÀàÔö¼ÓµÄÊôÐÔ¡£Õâ¾ÍÊDZ¾³ÌÐòÖеÄ×îºóÒ»Ðб»×¢Ê͵ôµÄÔÒò¡£BoxµÄÒýÓ÷ÃÎÊweight ÓòÊDz»¿ÉÄܵģ¬ÒòΪËüûÓж¨Òå¡£
¾¡¹ÜÇ°Ã沿·Ö¿´ÆðÀ´ÓÐÒ»µãÉî°Â£¬ËüÊǺÜÖØÒªµÄʵ¼ÊÓ¦ÓÃ----±¾ÕºóÃ潫ÌÖÂÛµÄÁ½ÖÖÓ¦ÓÃÖ®Ò»¡£