java - Why can't we use clone method by using approch 1(given below) instead of approch 2 and what's need of marker interface? Why jvm handle clone method?
问题描述
approach 2
/*Using clone method by using marker interface and clone method control by jvm (recommended way)*/
class Employee implements Clonable
{
String name,id;
Employee(String name,String id)
{
this.name=name;
this.id=id;
}
public Employee clone()
throws CloneNotSupportedException
{
return (Employee)super.clone();
}
}
public class ObjectClone
{
public static void main(String[] args)
{
Employee obj1 = new Emloyee("test","6");
Employee obj2=obj1.clone();
}
}
approch 1
/*without using marker interface control clone method by me (not recommended)*/
class Employee extends Object
{
String name,id;
Employee(String name,String id)
{
this.name=name;
this.id=id;
}
public Employee clone()
throws CloneNotSupportedException
{
return (Employee)super.clone();
}
}
public class ObjectClone
{
public static void main(String[] args)
{
Employee obj1 = new Emloyee("test","6");
Employee obj2=obj1.clone();
}
}
解决方案
推荐阅读
- php - 来自 Google 的 Cloud Speech-to-Text gRPC API 的这个错误是什么意思?
- python - 如何将 python 项目添加到 Azure 存储库?
- android - 在回收站视图中过滤结果为 0 时显示空状态
- c# - 如何在 Xamarin Forms 中将 Entity Framework Core 与 SQL Server 一起使用?
- java - Java中的Web浏览器控制?
- node.js - 创建新的 Angular 应用程序时出现多个错误
- python - 以编程方式在 dockerized apache 气流 python 操作符内创建 SSH 隧道
- c++ - 如何将位域写入二进制文件
- php - 按第一个和最后一个字符在 sql 列中搜索
- flutter - 如何使变量最终并在 initState() 中为其赋值