pair.java

来自「程序练习中包括书中实例程序代码和练习中要用到的代码,是压缩文件」· Java 代码 · 共 41 行

JAVA
41
字号
package examples.cloning;

/** An example class used to demonstrate how to
  * enable cloning for a class without fields that
  * contain object references..
  */
public class Pair implements Cloneable {

   private int a, b;

   /** Class constructor method
     * @param a Initial value for the first field
     * @param b Initial value for the second field
     */
   public Pair( int initA, int initB ) {
      a = initA;
      b = initB;
   }

   /** Convert object to String representation
     * @return The value of the array as a String
     */
   public String toString() {
      return "( " + a + ", " + b + " )";
   }

   /** The test method for the class
     * @param args Not used
     * @exception CloneNotSupportedException
     *            If clone not supported by
     *            inherited clone method
     */
   public static void main( String[] args ) 
         throws CloneNotSupportedException {
      Pair x = new Pair( 5, 6 );
      System.out.println( x );

      Pair y = (Pair) x.clone();
      System.out.println( y );
   }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?