📄 j02080402.java
字号:
import java.lang.*;
class j02080402
{
public static void main(String[] para)
{
myBorder obj1 = new myBorder();
myBorder obj2 = null;
System.out.println("obj1 = " + obj1);
obj1.writeLine('*');
obj2 = (myBorder)obj1.toClone();
System.out.println("obj2 = " + obj2);
obj2.writeLine('~');
obj2.writeLine('#');
/* // 配合 override clone() 的方式
myBorder obj3 = null;
try
{
obj3 = (myBorder)obj1.clone(); // 使用 Object 的
}
catch(CloneNotSupportedException E){ }
System.out.println("obj3 = " + obj3);
*/
}
}
interface border
{
int length = 40; // 自动成为 static final 成员变量
public void writeLine(char symbol); //自动成为 abstract 成员函数
}
class myBorder implements border ,Cloneable
{ //自定的接口 //内建的接口
public void writeLine(char symbol)
{
String line = "";
for(int x =1;x<=length;x++)
{ //length 是 static ,所以也可用 border.length 的方式
line = line + symbol;
}
System.out.println(line);
}
public Object toClone()
{
System.out.println("欲复制一份实例");
Object newObj = null ;
try
{
newObj = clone(); // 使用 Object 的 protected 方法
}
catch(CloneNotSupportedException E)
{
System.out.println("例外!无法复制");
}
System.out.println("newObj = " + newObj);
return newObj;
}
/*========= 若以 override Object 之 method 的方式 ============*/
/* //两种方式选一种即可,此方式请了解 override 后再使用
public Object clone() throws CloneNotSupportedException
{
System.out.println("欲复制一份实例");
Object newObj = super.clone(); // 使用 Object 的
System.out.println("newObj = " + newObj);
return newObj;
}*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -