The basic concepts of Java day8 [XVII] Object class

The basic concept [XVII] Object class

.Object a basic concept:

The main features of the Object class unity problem can be solved parameters, that use the Object class can receive all data types.

Introduction to Object class:

  In only one Java class inheritance is not there, then this is the Object class, that is a subclass of all classes default is the Object class

The following two types of definition of exactly the same effect.

class Person {}
class Person extends Object {}

  When the Object class design takes into account all inherited problems, so whether the class provides the constructor parameters (sub-class called the default constructor with no arguments to call the parent class).

  Since then the Object class is the parent of all classes, then in this case can be used to receive all the Object class subclasses the object.

  If a program requires a method is acceptable when all class objects can be implemented using Object processing.

  Another point to note, however, in making Java programs receive references for all data types can actually make use of Object, including arrays can be.

Example: Using Object class receiving array:

class Person{}

 public class tsy
{
	public static void main(String[] args) 
	{
		Object obj = new int [] {1,2,3};	//向上转型
		if(obj instanceof int[]){		
			int date[] = (int[])obj;
			for (int temp:date){
				System.out.println(temp);
			}
		}
	}
}

  Object data type is a universal, standard design is more suitable for the procedure.

 

Second, the acquisition target information (toString ()):

  Object Although it is a class, but the class itself also provides some processing methods.

  ToString Object can get the complete information of an object:

class Person{}

 public class tsy
{
	public static void main(String[] args) 
	{
		Person per = new Person();
		System.out.println(per);
		System.out.println(per.toString());
	}
}
//输出Person@2a84aee7
//输出Person@2a84aee7

  Can be found is toString () method when the object-direct output before the call, so this method calls and calls are not the same

  So in the future development of object information can be obtained directly override this method

Example: override toString:

{the Person class 
	Private String name; 
	Private int Age; 
	public the Person (String name, int Age) { 
		this.name = name; 
		this.age = Age; 
	} 
	public String toString () { 
		return "Name:" + this.name + " , Age: "+ this.age; 
	} 
} 

 public class TSY 
{ 
	public static void main (String [] args) 
	{ 
		the Person = new new per the Person (" with siyuan ",. 19); 
		System.out.println (per); 
		System.out.println (per.toString ()); 
	} 
} 
// output name: same Siyuan, Age: 19 
// output name: same Siyuan, Age: 19

  

Guess you like

Origin www.cnblogs.com/xiwenxinaini/p/11708280.html