简单的(浅)克隆
来源:互联网 发布:淘宝卖家如何发布链接 编辑:程序博客网 时间:2024/05/20 01:39
为一个不含对象引用域的类启动克隆的示例:
1/** *//** An example class used to demonstrate how to
2 * enable cloning for a class without fields that
3 * contain object references..
4 */
5public class Pair implements Cloneable { //必须实现Cloneable,
6
7 private int a, b; //否则会抛出CloneNotSupportedException异常
8
9 /** *//** Class constructor method
10 * @param initA Initial value for the first field
11 * @param initB Initial value for the second field
12 */
13 public Pair( int initA, int initB ) {
14 a = initA;
15 b = initB;
16 }
17
18 /** *//** Convert object to String representation
19 * @return The value of the array as a String
20 */
21 public String toString() {
22 return "( " + a + ", " + b + " )";
23 }
24
25 /** *//** The test method for the class
26 * @param args Not used
27 * @exception CloneNotSupportedException
28 * If clone not supported by
29 * inherited clone method
30 */
31 public static void main( String[] args )
32 throws CloneNotSupportedException {
33 System.out.println( "examples.cloning.Pair" );
34 Pair x = new Pair( 5, 6 );
35 System.out.println( x );
36
37 Pair y = (Pair) x.clone();
38 System.out.println( y );
39 }
运行结果为:
examples.cloning.Pair
( 5, 6 )
( 5, 6 )
2 * enable cloning for a class without fields that
3 * contain object references..
4 */
5public class Pair implements Cloneable { //必须实现Cloneable,
6
7 private int a, b; //否则会抛出CloneNotSupportedException异常
8
9 /** *//** Class constructor method
10 * @param initA Initial value for the first field
11 * @param initB Initial value for the second field
12 */
13 public Pair( int initA, int initB ) {
14 a = initA;
15 b = initB;
16 }
17
18 /** *//** Convert object to String representation
19 * @return The value of the array as a String
20 */
21 public String toString() {
22 return "( " + a + ", " + b + " )";
23 }
24
25 /** *//** The test method for the class
26 * @param args Not used
27 * @exception CloneNotSupportedException
28 * If clone not supported by
29 * inherited clone method
30 */
31 public static void main( String[] args )
32 throws CloneNotSupportedException {
33 System.out.println( "examples.cloning.Pair" );
34 Pair x = new Pair( 5, 6 );
35 System.out.println( x );
36
37 Pair y = (Pair) x.clone();
38 System.out.println( y );
39 }
运行结果为:
examples.cloning.Pair
( 5, 6 )
( 5, 6 )
- 简单的(浅)克隆
- Java中深克隆与浅克隆的简单理解
- 克隆的简单运用
- Java(对象的深克隆和浅克隆)
- 克隆bsp的简单步骤
- 简单炮弹的克隆发射
- 关于Java深度克隆和浅度克隆的简单说明
- 对象的克隆——原型模式(三):浅克隆,深克隆
- 对象的克隆——原型模式(三):浅克隆,深克隆
- java的克隆(深克隆和浅克隆)
- 深克隆和浅克隆的概念
- java的深克隆与浅克隆
- Java的浅克隆与深克隆
- Java的深克隆和浅克隆
- js的浅克隆和深度克隆
- Java的深克隆与浅克隆
- js的浅克隆和深度克隆
- Java对象克隆方法(浅克隆、深克隆)
- 利用 NDIS 中间层驱动程序截获网络封包
- Hook ZwQuerySystemInformation 隐藏qq程序
- icesword 驱动部分分析
- TextEditor 1.0 发布
- Java完成器的使用
- 简单的(浅)克隆
- 理解(深)克隆
- 远程支持美国CDS(很被动)
- Effective STL 读书笔记 4
- asp简单的广告过滤处理方法
- 巧用Gmail邮箱备份你的Blog
- 高性能计算工作站的尴尬
- 2006-03-23炸弹人开发日志
- JBuilder2006在Linux下安装及的中文显示问题的解决方法