package;
public class Person01 {
private String nickname;
private int age;
private int gender;
public Person01() {
}
public Person01(String nickname, int age, int gender) {
this.nickname = nickname;
this.age = age;
this.gender = gender;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
}
package Test;
import HashTable;
import Person01;
import org.junit.jupiter.api.Test;
public class Person01Test {
@Test
public void test01(){
int a = 10;
int b = 10;
System.out.println(a == b);
Integer x = 10;
Integer y = 10;
System.out.println(x == y);
System.out.println(x.equals(y));
Integer aa = 128;
Integer bb = 128;
System.out.println(aa == bb);
System.out.println(aa.equals(bb));
}
/*
true
true
true
false
true
*/
@Test
public void test02(){
String str01 = new String("abc");
String str02 = new String("abc");
System.out.println(str01 == str02);
System.out.println(str01.equals(str02));
System.out.println(str01.compareTo(str02));
}
/*
false
true
0
*/
@Test
public void test03(){
Person01 person01 = new Person01("姓名", 20, 1);
Person01 person02 = new Person01("姓名", 20, 1);
System.out.println(person01 == person02);
System.out.println(person01.equals(person02));
}
/*
false
false
*/
@Test
public void test04(){
Person01 person01 = new Person01("xm", 20, 1);
Person01 person02 = new Person01("xm", 20, 1);
// Person01@dbd940d
System.out.println(person01);
// 230528013
System.out.println(person01.hashCode());
// dbd940d
System.out.println(Integer.toHexString(person01.hashCode()));
// Person01@71d15f18
System.out.println(person02);
// 1909546776
System.out.println(person02.hashCode());
// 71d15f18
System.out.println(Integer.toHexString(person02.hashCode()));
}
/*
Person01@dbd940d
230528013
dbd940d
Day15.Person01@71d15f18
1909546776
71d15f18
*/
@Test
public void test05(){
String[] names = new String[8];
names[3] = "xm";
names[6] = "x";
for (int i = 0; i < names.length; i++) {
if ("xm".equals(names[i])) {
System.out.println(names[i]);
break;
}
}
System.out.println("程序完成");
}
/*
xm
程序完成
*/
}
02 hash
@Test
public void test06(){
HashTable hashTable = new HashTable();
int[] hashcodes = hashTable.getHashcodes();
hashcodes[33] = 33;
hashcodes[66] = 66;
hashTable.setHashcodes(hashcodes);
String[] values = hashTable.getValues();
values[33] = "xm";
values[66] = "x";
hashTable.setValues(values);
values = hashTable.getValues();
System.out.println(values[33]);
System.out.println(values[66]);
System.out.println("程序完成");
}
/*
xm
x
程序完成
*/
03 Object类equals方法和hasecode方法
package;
public class Person02 {
private String nickname;
private int age;
private int gender;
public Person02() {
}
public Person02(String nickname, int age, int gender) {
this.nickname = nickname;
this.age = age;
this.gender = gender;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
@Override
public boolean equals(Object o) {
if (o == null) {
return false;
}
Person02 person02 = (Person02) o;
if (this.age != person02.getAge()) {
return false;
} else if (this.gender != person02.getGender()) {
return false;
} else if (!this.nickname.equals(person02.getNickname())) {
return false;
} else {
return true;
}
}
@Override
public int hashCode() {
int reference = 31;
return this.age * (this.gender + reference) + this.nickname.hashCode();
}
}
package Test;
import Person01;
import Person02;
import org.junit.jupiter.api.Test;
public class Person02Test {
@Test
public void test01(){
Person01 person011 = new Person01("姓名", 20, 1);
Person01 person012 = new Person01("姓名", 20, 1);
Person02 person021 = new Person02("姓名", 20, 1);
Person02 person022 = new Person02("姓名", 20, 1);
System.out.println(person011.hashCode());
System.out.println(person012.hashCode());
System.out.println(person021.hashCode());
System.out.println(person022.hashCode());
System.out.println(person011 == person012);
System.out.println(person011.equals(person012));
System.out.println(person021 == person022);
System.out.println(person021.equals(person022));
}
/*
611563982
1615039080
735002
735002
false
false
false
true
*/
}
04 Object类toString方法
package;
public class Person03 {
private String nickname;
private int age;
private int gender;
public Person03() {
}
public Person03(String nickname, int age, int gender) {
this.nickname = nickname;
this.age = age;
this.gender = gender;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Person03{" +
"nickname='" + nickname + '\'' +
", age=" + age +
", gender=" + gender +
'}';
}
}
package Test;
import Person03;
import org.junit.jupiter.api.Test;
public class Person03Test {
@Test
public void test(){
Person03 person01 = new Person03("姓名", 20, 1);
Person03 person02 = new Person03("姓", 29, 1);
System.out.println(person01.toString());
System.out.println(person02.toString());
}
/*
Person03{nickname='姓名', age=20, gender=1}
Person03{nickname='姓', age=29, gender=1}
*/
}
05 浅拷贝与深拷贝
package;
public class Person04 implements Cloneable{
private String nickname;
public Person04() {
}
public Person04(String nickname) {
this.nickname = nickname;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
package Test;
import Person04;
import org.junit.jupiter.api.Test;
public class Person04Test {
@Test
public void test01(){
Person04 person01 = new Person04("姓名");
Person04 person02 = person01;
System.out.println(person01.getNickname());
System.out.println(person02.getNickname());
person02.setNickname("姓");
System.out.println(person01.getNickname());
System.out.println(person02.getNickname());
}
/*
姓名
姓名
姓
姓
*/
@Test
public void test02(){
Person04 person01 = new Person04("姓名");
Person04 person02 = new Person04(person01.getNickname());
System.out.println(person01.getNickname());
System.out.println(person02.getNickname());
person02.setNickname("姓");
System.out.println(person01.getNickname());
System.out.println(person02.getNickname());
}
/*
姓名
姓名
姓名
姓
*/
@Test
public void test03() throws CloneNotSupportedException {
Person04 person01 = new Person04("姓名");
Person04 person02 = (Person04) person01.clone();
System.out.println(person01.getNickname());
System.out.println(person02.getNickname());
person02.setNickname("姓");
System.out.println(person01.getNickname());
System.out.println(person02.getNickname());
}
/*
姓名
姓名
姓名
姓
*/
}
package;
/**
* 浅拷贝:指的是你的类本身被拷贝,而没有拷贝类本身属性中的类。
* 深拷贝:指的是包含类本身和属性类在内的所有类的拷贝。
*/
public class Person05 implements Cloneable {
private String nickname;
private Address05 address05;
public Person05(String 姓名, String 姓) {
}
public Person05(String nickname, Address05 address05) {
this.nickname = nickname;
this.address05 = address05;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public Address05 getAddress05() {
return address05;
}
public void setAddress05(Address05 address05) {
this.address05 = address05;
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
@Override
public String toString() {
return "Person05{" +
"nickname='" + nickname + '\'' +
", address05=" + address05 +
'}';
}
}
package Test;
import Address05;
import Person05;
import org.junit.jupiter.api.Test;
public class Person05Test {
@Test
public void test01() throws CloneNotSupportedException {
Person05 person01 = new Person05("姓名", new Address05("名"));
Person05 person02 = (Person05) person01.clone();
System.out.println(person01);
System.out.println(person02);
person02.setNickname("姓");
System.out.println(person01);
System.out.println(person02);
Address05 address05 = person02.getAddress05();
address05.setCity("啦啦啦");
person02.setAddress05(address05);
System.out.println(person01);
System.out.println(person02);
}
/*
Person05{nickname='姓名', address05=Address05{city='名'}}
Person05{nickname='姓名', address05=Address05{city='名'}}
Person05{nickname='姓名', address05=Address05{city='名'}}
Person05{nickname='姓', address05=Address05{city='名'}}
Person05{nickname='姓名', address05=Address05{city='啦啦啦'}}
Person05{nickname='姓', address05=Address05{city='啦啦啦'}}
*/
}
package;
/**
* 浅拷贝:指的是你的类本身被拷贝,而没有拷贝类本身属性中的类。
* 深拷贝:指的是包含类本身和属性类在内的所有类的拷贝。
*/
public class Person06 implements Cloneable {
private String nickname;
private Address06 address06;
public Person06() {
}
public Person06(String nickname, Address06 address06) {
this.nickname = nickname;
this.address06 = address06;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public Address06 getAddress06() {
return address06;
}
public void setAddress06(Address06 address06) {
this.address06 = address06;
}
@Override
public Object clone() throws CloneNotSupportedException {
return new Person06(this.nickname, (Address06) this.address06.clone());
}
@Override
public String toString() {
return "Person06{" +
"nickname='" + nickname + '\'' +
", address06=" + address06 +
'}';
}
}
package Test;
import Address06;
import Person06;
import org.junit.jupiter.api.Test;
public class Person06Test {
@Test
public void test01() throws CloneNotSupportedException {
Person06 person01 = new Person06("姓名", new Address06("名"));
Person06 person02 = (Person06) person01.clone();
System.out.println(person01);
System.out.println(person02);
person02.setNickname("姓");
System.out.println(person01);
System.out.println(person02);
Address06 address06 = person02.getAddress06();
address06.setCity("啦啦啦");
person02.setAddress06(address06);
System.out.println(person01);
System.out.println(person02);
}
/*
Person06{nickname='姓名', address06=Address06{city='名'}}
Person06{nickname='姓名', address06=Address06{city='名'}}
Person06{nickname='姓名', address06=Address06{city='名'}}
Person06{nickname='姓', address06=Address06{city='名'}}
Person06{nickname='姓名', address06=Address06{city='名'}}
Person06{nickname='姓', address06=Address06{city='啦啦啦'}}
*/
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)