一、Clonable 接口和深拷贝
1.clone接口的使用
注意以下几个点:
import javax.jws.soap.SOAPBinding;
class Person implements Cloneable{
public String name;
public int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class Test {
public static void main(String[] args) throws CloneNotSupportedException {
Person person = new Person("张三",10);
Person person2 = (Person) person.clone();
System.out.println(person);
System.out.println(person2);
}
}
2.浅拷贝 和 深拷贝
1)浅拷贝
import javax.jws.soap.SOAPBinding;
class Money{
public double m = 19.9;
}
class Person implements Cloneable{
public String name;
public int age;
public Money money = new Money();
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class Test {
public static void main(String[] args) throws CloneNotSupportedException {
Person person = new Person("张三",10);
Person person2 = (Person) person.clone();
System.out.println(person.money.m);
System.out.println(person2.money.m);
System.out.println("===========");
person.money.m = 99.99;
System.out.println(person.money.m);
System.out.println(person2.money.m);
}
public static void main1(String[] args) throws CloneNotSupportedException {
Person person = new Person("张三",10);
Person person2 = (Person) person.clone();
System.out.println(person);
System.out.println(person2);
}
}
2)深拷贝
import javax.jws.soap.SOAPBinding;
class Money implements Cloneable{
public double m = 19.9;
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Person implements Cloneable{
public String name;
public int age;
public Money money = new Money();
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
protected Object clone() throws CloneNotSupportedException {
//深拷贝
Person tmp = (Person) super.clone(); //将拷贝的第一层的this地址给tmp
tmp.money = (Money) this.money.clone();//再拷贝第二层this下的money
return tmp;
//return super.clone();
}
}
public class Test {
public static void main(String[] args) throws CloneNotSupportedException {
Person person = new Person("张三",10);
Person person2 = (Person) person.clone();
System.out.println(person.money.m);
System.out.println(person2.money.m);
System.out.println("===========");
person.money.m = 99.99;
System.out.println(person.money.m);
System.out.println(person2.money.m);
}
public static void main1(String[] args) throws CloneNotSupportedException {
Person person = new Person("张三",10);
Person person2 = (Person) person.clone();
System.out.println(person);
System.out.println(person2);
}
}
二、抽象类和接口的区别
抽象类和接口都是 Java 中多态的常见使用方式.
核心区别 : 抽象类中可以包含普通方法和普通字段 , 这样的普通方法和字段可以被子类直接使用 ( 不必重写 ), 而接口中 不能包含普通方法, 子类必须重写所有的抽象方法 .如之前写的 Animal 例子 . 此处的 Animal 中包含一个 name 这样的属性 , 这个属性在任何子类中都是存在的 . 因此此处的 Animal 只能作为一个抽象类 , 而不应该成为一个接口
三、object类
Object 是 Java 默认提供的一个类。Java 里面除了 Object 类,所有的类都是存在继承关系,默认会继承 Object 父 类。即所有类的对象都可以使用Object 的引用进行接收。绿色的解锁标志:public 公开的
灰色的钥匙:protected 受保护的
橙色的锁:private 私人的
例如equals:
重写equals方法
1)快捷键自动生成
2)手动重写