Object type *

When we learn to use the class keyword to define classes, on the application of the principle of inheritance, as all classes in Java are directly or indirectly
inherit java.lang.Object class.
Object is the parent class of all classes, Java layer is the top class, we want to inherit from other classes unless you specify when you create a class,
or else he is to inherit from java.lang.Object class comes a Java root class.
For example, the code words with your class Ang {} is equivalent to class Ang extends Object {}
in Object include clone (), finalize ()) , equals (), toString () method and the like, the two most commonly used a method
that equals (), toString () method, because of our classes inherit the Object, his sub-class, so we can rewrite his
method.
Let's talk some important ways for several Object class
1.getClass () method
in Java's Object class roots, we have an important method getClass () we can meet toString () method to use. getClass ()
method is a method of the Object class definition, he returned back to the Class instance when the object to perform, and then use this call getName () method can be a real column
to obtain the name of the class.
Method: getClass().getname()
2.toString () method
function toString () method returns the object is a string, he would a String instance. In practical applications, usually rewrite toString
() Method, the object to provide a specific output mode. When this category is converted to a string or strings connected, it will automatically call override toString
() method.

package yulu.tss;

public class TOString_te {
		public static void main(String[]args) {
			Test_a p=new Test_a("缓缓", 8);
			System.out.println(p.toString());
			
		}
}
class Test_a{
	String name;
	int age;
	public Test_a(String name,int age) {
		this.name=name;
		this.age=age;
	}
	
	public String toString() {
		return name+age+"岁了";
	}
}

The code is the best way to solve the problem
3.equals () method
when it comes to equals () method we have to mention "==" They are the judge, except that the "==" is used to determine whether two object references are equal,
and equals () methods compare the actual content of the two objects.
What better to say on the code:

package yulu.tss;
/*
 * 测试equsls()方法的重写
 * 
 */

public class Test_equals {
	public static void main(String[]args) {
		Test_q q=new Test_q(123,"路露",12.3);
		Test_q q1= new Test_q(123,"路路",12.5);
		System.out.println(q==q1);
		System.out.println(q.equals(q1));
		
	}

}
class Test_q{
	int id;
	String name;
	double qwe;
	public Test_q(int id, String name, double qwe) {
		super();
		this.id = id;
		this.name = name;
		this.qwe = qwe;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Test_q other = (Test_q) obj;
		if (id != other.id)
			return false;
		return true;
	}
	

	
}

Is not it amazing

Chicken soup: do not listen to old words, happy years.

Published 30 original articles · won praise 0 · Views 438

Guess you like

Origin blog.csdn.net/weixin_45736498/article/details/103341606