I, entitled: class rectangle having a long, wide, and the method of calculating the area of three areas of the member variables, and then create a subclass of the class of rectangular - square class, method of calculating the overwritten area. In the cartridge type, the class object to create a rectangular bottom made variable as a cylinder member, define methods soled and volume calculation. In the main class, computing a rectangular bottom cylinder volume object, and then use a method to replace soled square bottom, and to calculate the volume again.
Second, the code and comments
/ ** * define a rectangle class. There are long. width. Three members of the variable area, a rectangular writing class constructor. The method has two parameters, a method of calculating the area definition * define a rectangular square class inherits class constructor is defined. Which method a parameter. Call the parent class constructor with a super () constructor years. Rewritable area calculated * method. A cylinder defined class. There rectangle p1. high. Volume three member variables. The method defined soled method of obtaining the volume of the cylinder. * / Class Juxing { int Chang; int Kuan; int Area; public Juxing ( int Chang, int Kuan) { the this .chang = Chang; the this .kuan = Kuan; } void setArea () { Area = * Chang Kuan; } } classzhengfangxing the extends Juxing { public zhengfangxing ( int Bian) { Super (Bian, Bian); } void setArea () { Area = Chang * Chang; } } class zhuti { Juxing P1; int Gao; int Volum; zhuti (Juxing P1, int Gao) { the this .P1 = P1; the this .gao = Gao; } void SETP1 (Juxing P1) { // soled method of the this .P1 =P1; } void getvolum () { Volum = p1.area * Gao; } } public class examplesuper { public static void main (String [] args) { Juxing P1 = new new Juxing (. 3,. 4 ); p1.setArea (); C zhuti = new new zhuti (P1,. 5 ); c.getvolum (); System.out.println ( "rectangular cylinder volume:" + c.volum); c.setp1 ( new new zhengfangxing (. 5 )); C. p1.setArea (); c.getvolum (); System.out.println ( "square cylinder volume" + c.volum); } }
operation result: