把A对象的属性值完全拷贝给B对象,也叫对象拷贝,对象复制。
package MyApi.a04objectdemo;
public class ObjectDemo03 {
public static void main(String[] args) throws CloneNotSupportedException {
//1.先创建一个对象
int []data={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0};
User u1=new User(1,"zhangsan","12qwer","girl111",data);
//2.克隆对象
User u2=(User) u1.clone();
System.out.println(u1);
System.out.println(u2);
}
}
package MyApi.a04objectdemo;
import java.util.StringJoiner;
//Cloneable
//如果一个接口里面没有抽象方法
//表示当前的接口是一个标记性接口
//现在Cloneable表示一旦实现,那么当前类的对象可以被克隆
//如果没有实现,当前类的对象就不能克隆
public class User implements Cloneable{
private int id;//游戏角色
private String username;//用户名
private String password;//密码
private String path;//游戏图片
private int [] data;//游戏进度
public User() {
}
public User(int id, String username, String password, String path, int[] data) {
this.id = id;
this.username = username;
this.password = password;
this.path = path;
this.data = data;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public int[] getData() {
return data;
}
public void setData(int[] data) {
this.data = data;
}
@Override
public String toString() {
return "角色编号为"+id+",用户名:"+username+",密码:"+password+",游戏图片:"+path+",进度:"+arrToString();
}
public String arrToString(){
StringJoiner sj=new StringJoiner(",","[","]");
for (int i = 0; i < data.length; i++) {
sj.add(data[i]+" ");
}
return sj.toString();
}
@Override
protected Object clone()throws CloneNotSupportedException{
//调用父类中的clone方法
//相当于让Java帮我们克隆一个对象,并把对象返回出去
return super.clone();
}
}
不管对象内部的属性是基本数据类型还是引用数据类型,都完全拷贝过来。
基本数据类型拷贝过来,字符串复用,引用数据类型会重新创建新的。
package MyApi.a04objectdemo;
import java.util.StringJoiner;
//Cloneable
//如果一个接口里面没有抽象方法
//表示当前的接口是一个标记性接口
//现在Cloneable表示一旦实现,那么当前类的对象可以被克隆
//如果没有实现,当前类的对象就不能克隆
public class User implements Cloneable{
private int id;//游戏角色
private String username;//用户名
private String password;//密码
private String path;//游戏图片
private int [] data;//游戏进度
public User() {
}
public User(int id, String username, String password, String path, int[] data) {
this.id = id;
this.username = username;
this.password = password;
this.path = path;
this.data = data;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public int[] getData() {
return data;
}
public void setData(int[] data) {
this.data = data;
}
@Override
public String toString() {
return "角色编号为"+id+",用户名:"+username+",密码:"+password+",游戏图片:"+path+",进度:"+arrToString();
}
public String arrToString(){
StringJoiner sj=new StringJoiner(",","[","]");
for (int i = 0; i < data.length; i++) {
sj.add(data[i]+" ");
}
return sj.toString();
}
@Override
protected Object clone()throws CloneNotSupportedException{
//调用父类中的clone方法
//相当于让Java帮我们克隆一个对象,并把对象返回出去
//先把被克隆对象中的数组获取出来
int[]data=this.data;
//创建新的数组
int[] newData=new int[data.length];
//拷贝数组中的数据
for (int i = 0; i < data.length; i++) {
newData[i]=data[i];
}
//调用父类中的克隆对象
User u=(User) super.clone();
//因为父类中的克隆方法是浅克隆,替换克隆出来对象中的数组地址值
u.data=newData;
return u;
}
}
package MyApi.a04objectdemo;
public class ObjectDemo03 {
public static void main(String[] args) throws CloneNotSupportedException {
//1.先创建一个对象
int []data={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0};
User u1=new User(1,"zhangsan","12qwer","girl111",data);
//2.克隆对象
User u2=(User) u1.clone();
//验证一件事情:Object中的克隆是浅克隆
int[] arr=u1.getData();
arr[0]=100;
System.out.println(u1);
System.out.println(u2);
}
}package MyApi.a04objectdemo;
public class ObjectDemo03 {
public static void main(String[] args) throws CloneNotSupportedException {
//1.先创建一个对象
int []data={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0};
User u1=new User(1,"zhangsan","12qwer","girl111",data);
//2.克隆对象
User u2=(User) u1.clone();
//验证一件事情:Object中的克隆是浅克隆
int[] arr=u1.getData();
arr[0]=100;
System.out.println(u1);
System.out.println(u2);
}
}
是一个工具类。
成员方法:
package MyApi.a05ObjectsDemo;
import java.util.Objects;
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
return age == student.age && Objects.equals(name, student.name);
}
}
package MyApi.a05ObjectsDemo;
import java.util.Objects;
public class ObjectsDemo01 {
public static void main(String[] args) {
//1.创建学生类对象
Student s1 =null;
Student s2 = new Student("zhangsan", 23);
//2.比较两个对象的属性值是否相同
boolean result = Objects.equals(s1,s2);
System.out.println(result);
//细节:
//1.方法的底层会判断s1是否为null,如果为null直接返回false
//2.如果s1不为null,那么就利用s1再次调用equals方法
//3.此时s1是student类型,所以最终还是会调用stuent中的equals方法
//如果没有重写,比较地址值,如果重写了,比较属性值。
Student s3=new Student();
System.out.println(Objects.isNull(s3));
System.out.println(Objects.isNull(s1));
System.out.println(Objects.nonNull(s1));
System.out.println(Objects.nonNull(s3));
}
}