Chapter 7 Further Research Methods and Class (1)

xiaoxiao2021-03-06  43

Overload.java

// DemonStrate Method Overloading.class overloadDemo {void test () {system.out.println ("no parameters");} // overload test for one integer parameter. Void test (int A) {system.out.println (" A: " a);} // overload test for two integer parameters. Void Test (int A, int b) {system.out.println (" a and b: " a " b);} / / Overload test for a double parameter. Double test (double a) {system.out.println ("a:" a); return a * a;}} class overload {public static void main (String [] args) { OVERLOADDEMO OB = New overloadDemo (); Double Result; // Call All Versions of Test () ob.test (); ob.test (10); ob.test (10, 20); Result = ob.test (123.25) System.out.println ("Result of Ob.test (123.25):" Result);}}

Overload2.java

// Automatic type conversions apply to overloading.class OverLoadDemo2 {void test () {System.out.println ( "No parameters");} // Overload for two integer parameters void test (int a, int b) {System.. Out.println ("a and b:" a " b);} // overload test for a double parameter void test (double a) {system.out.println (" INSIDE TEST (DOUBLE) A: " a);}} class overload2 {public static void main (string [] args) {overloadDemo2 ob = new overloadDemo2 (); int i = 88; ob.test (); ob.test (10, 20); OB. TEST (I); // this will invoke test (double) ob.test (123.2); // this will invoke test (double)}}

Overloadcons.java

. / * * Here, Box defines three constructors to initialize the dimensions * of a box various ways * / package Chapter7; class Box3 {double width; double height; double depth; // constructor used when all dimensions specified Box3 (double w, Double H, Double D) {width = W; Height = H; depth = D;} // constructor buy3 () {width = -1; // use -1 to indeicate heiGHT = -1; / / an uninitialized depth = -1; // box} // constructor used when cube is created Box3 (double len) {width = height = depth = len;} // compute and return volume double volume () {return width * height * depth;}} class OverLoadCons {public static void main (String [] args) {// Create boxes using the various constructors Box3 mybox1 = new Box3 (10, 20, 15); Box3 mybox2 = new Box3 (); Box3 mycube = New Box3 (7); Double Vol; // Get Volume Of First Box Vol = mybox1.volume (); system.out.println ("Volume of Mybox1 IS" VOL); // Get Volume of Second Box Vol = MyBox2.Volume (); System.out.Println ("Volume of Mybox2 IS " Vol); // Get Volume of Cube Vol = mycube.volume (); System.out.Println (" Volume of Mycube IS " VOL);}} Passob.java

// Objects.Package Chapter7; Class Test {Int A, B; Test (INT I, INT J) {a = i; b = j;} // return true if o is equal to invoking Object Boolean Equals (Test O) {IF (OA == A && Ob == B) Return True; Else Return False;}} Class Passob {Public Static Void Main (String [] args) {test ob1 = new test (100, 22 ); Test ob2 = new test (100, 22); test ob3 = new test (-1, -1); system.out.println ("ob1 == OB2:" ob1.equals (ob2)); system. Out.println ("OB1 == OB3:" ob1.equals (ob3));}} OverloadCons2.java

// Here, Box allows one object to initialize another.package Chapter7; class Box4 {double width; double height; double depth; // construct clone of an object Box4 (Box4 ob) {// pass object to constructor width = ob. Width; height = obsheight; defth = ob.depth;} // constructor used when all Dimensions Specified Box4 (Double W, Double H, Double D) {width = W; Height = H; DEPTH = D;} // Constructor used when no dimensions specified box4 () {width = -1; // use -1 to indeicate height = -1; // an uninitialized depth = -1; // Box} // constructor used when cube is created Box4 ( double len) {width = height = depth = len;} // compute and return volume double volume () {return width * height * depth;}} class OverLoadCons2 {public static void main (String [] args) {// Create Boxes Using The Various Constructionors Box4 mybox1 = new box4 (10, 20, 15 ); Box4 mybox2 = new box4 (); box4 mycube = new box4 (7); Box4 myclone = new box4 (MyBox1); Double Vol; // Get Volume of first box vol = mybox1.volume (); system.out. Println ("Volume of Mybox1 IS" VOL); // Get Volume of Second Box Vol = MyBox2.Volume (); System.out.Println ("Volume Of Mybox2 IS" VOL); // Get Volume of Cube Vol = mycube.volume (); system.out.println ("Volume of mycube is" vol); // get volume of clone vol = myclone.volume (); system.out.println ("Volume Of Clone IS" Vol);}}

CallByvalue.java// Simple Types Are Passed by Value.Package Chapter7; Class Test2 {Void Meth (INT I, INT J) {i * = 2; J / = 2;}} Class CallbyValue}} Class CallbyValue}} Class Callbyvalue}} Class CallbyValue}} ] args) {test2 ob = new test2 (); int A = 15, b = 20; system.out.println ("a and b before call:" "" b); ob.meth (A, b); System.out.Println ("A and B After Call:" A "" B);}}

CallByref.java

// Object is passed by reference.Package Chapter7; Class test3 {Int a, b; test3 (int i, int j) {a = i; b = j;} // pass an Object void method (test3 o) {OA * = 2; OB / = 2;}} Class Callbyref {public static void main (string [] args) {test3 ob = new test3 (15, 20); system.out.println ("ob.a and ob.b Before call: " obs.a " ob.b); ob.meth (ob); system.out.println ("Ob.a and ob.b after call:" ob.a "" Ob.b);}}

Retob.java

// Return An Object.Package Chapter7; Class Test4 {Int A; Test4 (INT I) {A = I; Test4 IncrbyTen ()} TEST4 TEMP = New Test4 (A 10); Return Temp;}} Class Retob { Public static void main (string [] args) {test4 ob1 = new test4 (2); test4 ob2; ob2 = ob1.incrbyten (); system.out.println ("ob1.a:" Ob1.a); system .out.println ("ob2.a:" ob2.a); ob2 = ob2 .incrbyten (); system.out.println ("ob2.a after a second increase:" ob2.a);}}

转载请注明原文地址:https://www.9cbs.com/read-54408.html

New Post(0)