⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clonedemo.java

📁 cloneable接口的实现与演示
💻 JAVA
字号:
import java.awt.*;
import java.applet.*;

class DrawOval implements Cloneable
{
	int x, y, width, height;

	public void setPos(int x1, int y1)
	{
		x = x1; 
		y = y1;
	}
	public void setOval(int w, int h)
	{
		width = w;
		height = h;
	}
	public void draw(Graphics g)
	{
		g.drawOval(x, y, width, height);
	}

	protected Object clone()
	{
		try
		{
			DrawOval cloneObject = (DrawOval)super.clone();
			return cloneObject;
		}
		catch(CloneNotSupportedException e)
		{
			throw new InternalError();
		}
	}
};

public class CloneDemo extends Applet
{
	public void paint(Graphics g)
	{
		DrawOval c[] = new DrawOval[10];
		DrawOval a = new DrawOval();
		a.setPos(20, 20);
		a.setOval(60, 60);
		for(int i=0; i<10; i++)
		{
			c[i] = (DrawOval)a.clone();
			c[i].setPos(20+i*20, 20+i*4);
			c[i].draw(g);
			System.out.println("c[" + i + "] = " + c[i]);
		}

		DrawOval c1[] = new DrawOval[10];
		for(int j=0; j<10; j++)
		{
			c1[j] = a;
			c1[j].setPos(20+j*20, 20+j*8);
			c1[j].draw(g);
			System.out.println("c1[" + j + "] = " + c1[j]);
		}
	}
};

⌨️ 快捷键说明

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