Java/Class/Overloading

Материал из Java эксперт
Перейти к: навигация, поиск

Demonstration of both constructor and ordinary method overloading

// : c04:JavaOverloading.java
// Demonstration of both constructor and ordinary method overloading.
// From "Thinking in Java, 3rd ed." (c) Bruce Eckel 2002
// www.BruceEckel.ru. See copyright notice in CopyRight.txt.

class Tree {
  int height;
  Tree() {
    System.out.println("Planting a seedling");
    height = 0;
  }
  Tree(int i) {
    System.out.println("Creating new Tree that is " + i + " feet tall");
    height = i;
  }
  void info() {
    System.out.println("Tree is " + height + " feet tall");
  }
  void info(String s) {
    System.out.println(s + ": Tree is " + height + " feet tall");
  }
}
public class JavaOverloading {
  public static void main(String[] args) {
    for (int i = 0; i < 5; i++) {
      Tree t = new Tree(i);
      t.info();
      t.info("overloaded method");
    }
    // Overloaded constructor:
    new Tree();
  }
} ///:~





Demonstration of overriding fields

/*
 * Copyright (c) Ian F. Darwin, http://www.darwinsys.ru/, 1996-2002.
 * All rights reserved. Software written by Ian F. Darwin and others.
 * $Id: LICENSE,v 1.8 2004/02/09 03:33:38 ian Exp $
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS""
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 * Java, the Duke mascot, and all variants of Sun"s Java "steaming coffee
 * cup" logo are trademarks of Sun Microsystems. Sun"s, and James Gosling"s,
 * pioneering role in inventing and promulgating (and standardizing) the Java 
 * language and environment is gratefully acknowledged.
 * 
 * The pioneering role of Dennis Ritchie and Bjarne Stroustrup, of AT&T, for
 * inventing predecessor languages C and C++ is also gratefully acknowledged.
 */
/** Demonstration of overriding fields. */
public class OverrideField {
  public static void main(String[] av) {
    System.out.println("OA"s version of getAttr returns: " +
      new OA().getAttr());
    System.out.println("OB"s version of getAttr returns: " +
      new OB().getAttr());
    // Declared as OA, instantiated as OB, so gets OB"s version of things.
    OA c = new OB();
    System.out.println("C"s version of getAttr returns: " +
      c.getAttr());
  }
}
class OA {
  final int attr = 1;
  int getAttr() {
    System.out.println("In OA.getAttr");
    return attr;
  }
}
class OB extends OA {
  final int attr = 2;
  int getAttr() {
    System.out.println("In OB.getAttr");
    super.getAttr(); // Just to show flow of control
    return attr;
  }
}





Demotion of primitives and overloading

// : c04:Demotion.java
// Demotion of primitives and overloading.
// From "Thinking in Java, 3rd ed." (c) Bruce Eckel 2002
// www.BruceEckel.ru. See copyright notice in CopyRight.txt.
public class Demotion {
  void f1(char x) {
    System.out.println("f1(char)");
  }
  void f1(byte x) {
    System.out.println("f1(byte)");
  }
  void f1(short x) {
    System.out.println("f1(short)");
  }
  void f1(int x) {
    System.out.println("f1(int)");
  }
  void f1(long x) {
    System.out.println("f1(long)");
  }
  void f1(float x) {
    System.out.println("f1(float)");
  }
  void f1(double x) {
    System.out.println("f1(double)");
  }
  void f2(char x) {
    System.out.println("f2(char)");
  }
  void f2(byte x) {
    System.out.println("f2(byte)");
  }
  void f2(short x) {
    System.out.println("f2(short)");
  }
  void f2(int x) {
    System.out.println("f2(int)");
  }
  void f2(long x) {
    System.out.println("f2(long)");
  }
  void f2(float x) {
    System.out.println("f2(float)");
  }
  void f3(char x) {
    System.out.println("f3(char)");
  }
  void f3(byte x) {
    System.out.println("f3(byte)");
  }
  void f3(short x) {
    System.out.println("f3(short)");
  }
  void f3(int x) {
    System.out.println("f3(int)");
  }
  void f3(long x) {
    System.out.println("f3(long)");
  }
  void f4(char x) {
    System.out.println("f4(char)");
  }
  void f4(byte x) {
    System.out.println("f4(byte)");
  }
  void f4(short x) {
    System.out.println("f4(short)");
  }
  void f4(int x) {
    System.out.println("f4(int)");
  }
  void f5(char x) {
    System.out.println("f5(char)");
  }
  void f5(byte x) {
    System.out.println("f5(byte)");
  }
  void f5(short x) {
    System.out.println("f5(short)");
  }
  void f6(char x) {
    System.out.println("f6(char)");
  }
  void f6(byte x) {
    System.out.println("f6(byte)");
  }
  void f7(char x) {
    System.out.println("f7(char)");
  }
  void testDouble() {
    double x = 0;
    System.out.println("double argument:");
    f1(x);
    f2((float) x);
    f3((long) x);
    f4((int) x);
    f5((short) x);
    f6((byte) x);
    f7((char) x);
  }
  public static void main(String[] args) {
    Demotion p = new Demotion();
    p.testDouble();
  }
} ///:~





Overloaded constructor

public class Point {
  int x, y;
  Point(int x, int y) // Overloaded constructor
  {
    this.x = x;
    this.y = y;
  }
  Point(Point p) // Overloaded constructor
  {
    this(p.x, p.y);
  } // Calls the first constructor
  void move(int dx, int dy) {
    x += dx;
    y += dy;
  }
  public String toString() {
    return "(" + x + ", " + y + ")";
  }
}





Overloaded method

public class Overloading {
  double method(int i) {
    return i;
  }
  boolean method(boolean b) {
    return !b;
  }
  static double method(int x, double y) {
    return x + y + 1;
  }
  static double method(double x, double y) {
    return x + y + 3;
  }
  public static void main(String[] args) {
    System.out.println(method(10, 20)); 
    System.out.println(method(10, 20.0)); 
    System.out.println(method(10.0, 20)); 
    System.out.println(method(10.0, 20.0)); 
  }
}





Overloading a base-class method name in a derived class does not hide the base-class versions

// : c06:Hide.java
// Overloading a base-class method name in a derived class does not hide the base-class versions.
// From "Thinking in Java, 3rd ed." (c) Bruce Eckel 2002
// www.BruceEckel.ru. See copyright notice in CopyRight.txt.
class Homer {
  char doh(char c) {
    System.out.println("doh(char)");
    return "d";
  }
  float doh(float f) {
    System.out.println("doh(float)");
    return 1.0f;
  }
}
class Milhouse {
}
class Bart extends Homer {
  void doh(Milhouse m) {
    System.out.println("doh(Milhouse)");
  }
}
public class Hide {
  public static void main(String[] args) {
    Bart b = new Bart();
    b.doh(1);
    b.doh("x");
    b.doh(1.0f);
    b.doh(new Milhouse());
  }
} ///:~





Overloading based on the order of the arguments

// : c04:OverloadingOrder.java
// Overloading based on the order of the arguments.
// From "Thinking in Java, 3rd ed." (c) Bruce Eckel 2002
// www.BruceEckel.ru. See copyright notice in CopyRight.txt.
public class OverloadingOrder {
  static void print(String s, int i) {
    System.out.println("String: " + s + ", int: " + i);
  }
  static void print(int i, String s) {
    System.out.println("int: " + i + ", String: " + s);
  }
  public static void main(String[] args) {
    print("String first", 11);
    print(99, "Int first");
  }
} ///:~





Promotion of primitives and overloading

// : c04:PrimitiveOverloading.java
// Promotion of primitives and overloading.
// From "Thinking in Java, 3rd ed." (c) Bruce Eckel 2002
// www.BruceEckel.ru. See copyright notice in CopyRight.txt.
public class PrimitiveOverloading {
  void f1(char x) {
    System.out.println("f1(char)");
  }
  void f1(byte x) {
    System.out.println("f1(byte)");
  }
  void f1(short x) {
    System.out.println("f1(short)");
  }
  void f1(int x) {
    System.out.println("f1(int)");
  }
  void f1(long x) {
    System.out.println("f1(long)");
  }
  void f1(float x) {
    System.out.println("f1(float)");
  }
  void f1(double x) {
    System.out.println("f1(double)");
  }
  void f2(byte x) {
    System.out.println("f2(byte)");
  }
  void f2(short x) {
    System.out.println("f2(short)");
  }
  void f2(int x) {
    System.out.println("f2(int)");
  }
  void f2(long x) {
    System.out.println("f2(long)");
  }
  void f2(float x) {
    System.out.println("f2(float)");
  }
  void f2(double x) {
    System.out.println("f2(double)");
  }
  void f3(short x) {
    System.out.println("f3(short)");
  }
  void f3(int x) {
    System.out.println("f3(int)");
  }
  void f3(long x) {
    System.out.println("f3(long)");
  }
  void f3(float x) {
    System.out.println("f3(float)");
  }
  void f3(double x) {
    System.out.println("f3(double)");
  }
  void f4(int x) {
    System.out.println("f4(int)");
  }
  void f4(long x) {
    System.out.println("f4(long)");
  }
  void f4(float x) {
    System.out.println("f4(float)");
  }
  void f4(double x) {
    System.out.println("f4(double)");
  }
  void f5(long x) {
    System.out.println("f5(long)");
  }
  void f5(float x) {
    System.out.println("f5(float)");
  }
  void f5(double x) {
    System.out.println("f5(double)");
  }
  void f6(float x) {
    System.out.println("f6(float)");
  }
  void f6(double x) {
    System.out.println("f6(double)");
  }
  void f7(double x) {
    System.out.println("f7(double)");
  }
  void testConstVal() {
    System.out.println("Testing with 5");
    f1(5);
    f2(5);
    f3(5);
    f4(5);
    f5(5);
    f6(5);
    f7(5);
  }
  void testChar() {
    char x = "x";
    System.out.println("char argument:");
    f1(x);
    f2(x);
    f3(x);
    f4(x);
    f5(x);
    f6(x);
    f7(x);
  }
  void testByte() {
    byte x = 0;
    System.out.println("byte argument:");
    f1(x);
    f2(x);
    f3(x);
    f4(x);
    f5(x);
    f6(x);
    f7(x);
  }
  void testShort() {
    short x = 0;
    System.out.println("short argument:");
    f1(x);
    f2(x);
    f3(x);
    f4(x);
    f5(x);
    f6(x);
    f7(x);
  }
  void testInt() {
    int x = 0;
    System.out.println("int argument:");
    f1(x);
    f2(x);
    f3(x);
    f4(x);
    f5(x);
    f6(x);
    f7(x);
  }
  void testLong() {
    long x = 0;
    System.out.println("long argument:");
    f1(x);
    f2(x);
    f3(x);
    f4(x);
    f5(x);
    f6(x);
    f7(x);
  }
  void testFloat() {
    float x = 0;
    System.out.println("float argument:");
    f1(x);
    f2(x);
    f3(x);
    f4(x);
    f5(x);
    f6(x);
    f7(x);
  }
  void testDouble() {
    double x = 0;
    System.out.println("double argument:");
    f1(x);
    f2(x);
    f3(x);
    f4(x);
    f5(x);
    f6(x);
    f7(x);
  }
  public static void main(String[] args) {
    PrimitiveOverloading p = new PrimitiveOverloading();
    p.testConstVal();
    p.testChar();
    p.testByte();
    p.testShort();
    p.testInt();
    p.testLong();
    p.testFloat();
    p.testDouble();
  }
} ///:~