15 Object class java object-oriented unit testing and JUnit

/*

  • Object class
  • 1, Object class is the root of all Java classes of the parent, if the parent class is not specified using the extends keyword in the class declaration, the default parent class to class java.lang.Object
  • Means that all objects of the class can use methods defined Object class, Object class only a null constructor parameter.
  • 2, the method Object class: Common equals () comparison, toString () output, clone () to copy the object, the hashCode () using a set of
  • getClass () class query object belongs, finalize () This method is called before the garbage collector GC recycling target, notify (), notifyAll (), wait (), multi-threaded use
  • 3, the difference == and equals () of
  • 3.1Is the operator,Either more basic types can also compare reference types. For basic type is the comparison value for reference types
  • It is a comparison of the memory address.
  • 3.2equals () method which belongs to the class java.lang.Object, if the method is also not been rewritten default
  • It is ==; we can see that equals String and other classes () method is being rewritten, and daily development in the String class
  • With more, over time, form a equals () misconception that the comparison value.
  • 3.3 depends on the specific custom class have the equals method of Object no override is determined. Under normal circumstances, override equals () method, the corresponding property in comparison whether class are equal.
  • 4 == operator if the comparison is a basic variable data types, whether to save the data comparing the two variables are equal, (not necessarily the same type)
  • If the comparison reference data type of the variable, comparing the address values ​​for these variables are the same. That is, two references point to the same object entity heap space.
  • Special attention String s1 = new String ( "A"); and s1 = "A"; the difference between the results of different comparative == two ways,
  • Because the string data type is stored in the constant pool, not heap space. Data in the constant pool if the two objects are assigned to the same data, then the heap space variables are pointing
  • The same address in the constant pool.
  • equals () method instead of operators, the method is only applicable to reference data types.
  • equals defined in the class Object () == same method and effect, the comparison object value of the address
  • In the String, Date, File, packaging (Wrapper Class) are rewriting the equals Object () method, the comparison is no longer the address value
  • But the actual value to compare two objects are the same.
  • Custom class can override equals () methods for comparing two attribute values ​​are the same whether the object.
  • Principle 5, custom class overrides equals () method
  • 5.1, symmetry: if x.equals (y) returns is "true", then y.equals (x) should return a "true".
  • 5.2, reflexivity: x.equals (x) must return a "true".
  • 5.3, transitive: if x.equals (y) returns a "true", and y.equals (z) returns yes "true", then z.equals (x) should also return a "true".
  • 5.4 Consistency: If x.equals (y) returns is "true", as long as the x and y have the same content, whether you repeat x.equals (y) the number of times, returns are "true".
  • 5.5 In any case, x.equals (null), will always return a "false"; x.equals (and different types of objects x) always returns is "false".
  • eclips provides automatic override hashCode () and equals () function.
  • 5.6. If the attributes are in class A reference data type class B, then class A after rewriting equals () method, also requires equals () method of class B override.
  • 6, toString () method;
  • 6.1 toString () methods defined in the class Object, the return value is of type String, it returns a reference to the class name and address.
  • During operation 6.2 String connection with other types of data, automatically calls toString () method
  • E.g. MyDate a = new MyDate ();
  • System.out.println (a); equivalent to System.out.println (a.toString ()); an output address value
  • 6.3 can be rewritten in a user-defined type toString () method, such as String class overrides toString () method returns the value of the string.
  • E.g. String s = "Hello"; System.out.println (s); equivalent to System.out.println (s.toString ()); output Hello
  • Remaining there Date, File, packaging (Wrapper class) are rewriting the toString () method. Return address but not the value of the actual information when you call
  • 6.4 basic types of data is converted into String type, called toString () method corresponding to the wrapper class
  • E.g. int a = 11; System.out.println (a);
  • Use 7.Eclips unit tests (the JUnit) of
  • Step 1, select the current project - to select the right build path --add Library --JUnit
  • Step 2, create a java class test,
  • Required test class: class permission to public, a public constructor with no arguments
  • Step 3, to create a unit test method in the class
  • Test method requires: Permission method is public, no return value, intangible parameters
  • Step 4, to declare a @Test on this method, and introduced in the test class import java.org.junit.Test
  • After a good unit test method step 5, a statement can be tested in vivo correlation code
  • Step 6, after writing the test code, unit test methods were left click, right: run as - JUnit Test
  • Description:
  • 1, if the execution result is not abnormal, show green bar
  • 2, if the results are abnormal, red bar and error messages
  • 3, a simple method, find or create the test class @Test input, and the input public void method name () {}
  • After using ctrl + 1 to complete the automatic repair leader packet and import operations of JUnit.

*/

package object_chapter2;
import java.lang.Object;
public class Object_Class {
 public static void main(String[] args) {
	Person p = new Person();	
	System.out.println(p.getClass());//class object_chapter2.Person
	System.out.println(p.getClass().getSuperclass());//class java.lang.Object
	int[] arr = new int[10];
	int[] arr1 = arr.clone();
	arr1 = null;//将对象赋值为null,暗示gc可以回收对象
	System.gc();//通知gc回收对象
	Runtime.getRuntime().gc();//通知gc回收对象
	//垃圾回收机制不可预知,无法精确执行垃圾回收,垃圾回收只针对JVM堆内存的对象空间
	//垃圾回收机制对于其他物理连接比如数据库连接,IO流,Socket连接无能为力。
	//如果覆盖finalize方法,将一个新的引用变量重新引用该对象,则会重新激活对象
	//不要主动调用对象的finalize方法,应由垃圾回收器执行。
	
	//测试==运算符
	int i = 10;
	int j = 10;
	double d = 10.0;
	char c = 10;
	System.out.println(i == j);//true
	System.out.println(i == d);//true ,对于运算符都有自动类型提升
	char c1 = 'a';
	int k = 97;
	System.out.println(i == c);//true 
	System.out.println(k == c1);//true,对于char型数据 ==比较的是ASCII值。
	//boolean类型无法与其他基本数据类型比较。
	p = new Man();
	Man m = (Man) p;
	System.out.println(p == m);//true
	String s1 = new String("A");//创建String类型的对象
	String s2 = new String("A");
	System.out.println(s1 == s2);//false,比较对象地址值,不相同
	s1 = "A" ;//为String类型的对象赋值
	s2 = "A" ;
	System.out.println(s1 == s2);//true,字符串数据储存在常量池中,同一数据的地址值相同。
	//测试equals()方法
	System.out.println("----------------------------");
	Person p1 = new Person("Fun",23);
	Person p2 = new Person("Fun",23);
	System.out.println(p1.equals(p2));//false,执行Object类中的方法,与==作用相同,比较地址值
	System.out.println(s1.equals(s2));//true,执行String类中重写的方法,比较实际值,
	Moon moon = new Moon();
	MyDate m1 = new MyDate(1,1,1999,moon);
	MyDate m2 = new MyDate(1,1,2000,moon);
	m2.setYear(1999);
	System.out.println(m1.equals(m2));	
	m2 = null;
	System.out.println(m1.equals(m2));
	
	//toString()方法测试
	Moon moon1 = new Moon();
	System.out.println(moon1);
	System.out.println(moon1.toString());
	String k1 = "Key";
	System.out.println(k1);
	System.out.println(k1.toString());
	System.out.println(m1.toString());
}
}

class MyDate{
	private int day;
	private int month;
	private int year;
	private String str = "公历";
	private Moon moon;
 
	public MyDate() {
		super();
	}

	public MyDate(int day, int month, int year, Moon moon) {
		super();
		this.day = day;
		this.month = month;
		this.year = year;
		this.moon = moon;
	}



	public int getDay() {
		return day;
	}

	public void setDay(int day) {
		this.day = day;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}
	
	
	public String getStr() {
		return str;
	}

	public void setStr(String str) {
		this.str = str;
	}
    
	public Moon getMoon() {
		return moon;
	}

	public void setMoon(Moon moon) {
		this.moon = moon;
	}

	@Override
	public boolean equals(Object obj) {
		// TODO Auto-generated method stub
		if(this == obj) {
			return true;
		}
		if(obj == null) {
			return false;
		}
		if(obj instanceof MyDate) {
			MyDate m = (MyDate)obj;
			return  this.day == m.day && this.month == m.month && 
					this.year == m.year && this.str.equals(m.str) && 
					this.moon.equals(m.moon);
		//this.str.equals(m.str)不能改写为this.str == m.str。
		//如果形参使用m.setStr(new String("非闰年"))的方式传入参数,地址值就会不同,使用 == 比较会返回false。
		//如果形参使用m.setStr("非闰年")的方式传入,因为数据储存在常量池中,所以相同数据只有一个地址值,使用 == 比较会返回true
		}else {
			return false;
		}
	
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return super.toString() + "\n" + getClass() + "[" + year + "年" + month + "月" + day + "日," + str + "]";
	}
		
}

class Moon{
	private int cycle = 28;
	@Override
	public boolean equals(Object obj) {//eclips自动重写的equals()方法
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Moon other = (Moon) obj;
		if (cycle != other.cycle)
			return false;
		return true;
	}
	
}
Published 47 original articles · won praise 1 · views 1058

Guess you like

Origin blog.csdn.net/wisdomcodeinside/article/details/104162761