1.Explorationjdksource.java Example
In this example, a class ais defined, and it does not have any members:
Class A {}
Example directly outputs the object created by this class
public static void Main (string[] args) {
System.out.println (New A ());
}
SOURCE program:
public class Explorationjdksource {
/**
* @param args
*/
public static void Main (string[] args) {
System.out.println (New A ());
}
}
Class a{}
Operation Result:
Run result analysis :
The main method actually calls the public void println (Object x), which invokes the valueOf of the String class internally method;
The object.tostring method is also called inside the ValueOf method:
Public String toString () {
Return getclass () getName () + "@" +integer.tohexstring (hashcode ());
}
The Hashcode method is a local method that is implemented by the JVM Designer:
Public native Inthashcode ();
So the last output is the class name [email protected], the output behind is random.
2, Fruit.java
Code:
public class Fruit
{
Public String toString ()
{
Return "Fruit toString.";
}
public static void Main (String args[])
{
Fruit f=new Fruit ();
System.out.println ("f=" +f);
System.out.println ("f=" +f.tostring ());
}
}
Run result analysis :
The main method actually calls the public void println (Object x), which internally invokes the vulueof method of the String class;
The object.tostring method is also called inside the ValueOf method:
Public String toString () {
Return getclass () getName () + "@" +integer.tohexstring (hashcode ());
}
The Hashcode method is a local method that is implemented by the JVM Designer:
Public native Inthashcode ();
So the last output is the class name [email protected], the output behind is random.
2, Fruit.java
Code:
public class Fruit
{
Public String toString ()
{
Return "Fruit toString.";
}
public static void Main (String args[])
{
Fruit f=new Fruit ();
System.out.println ("f=" +f);
System.out.println ("f=" +f.tostring ());
}
}
Note the last sentence, a string and an object "add" to get the following results:
Why the results?
Results Analysis:
fruit tostring method. In " + " in , string method, by default, this method returns "class name @ + hashcode ". To return meaningful information, subclasses can override tostring () method.
2. The main points of the "method overlay (override)"
? (1) The method overrides the requirement that the subclass is the same as the method of the parent class, otherwise it is the method overload (overload);
(2) The static method and the final method (the method with the keyword final ) cannot be overwritten;
(3) An abstract method must be overridden in a specific class (see the abstract modifier chapter).
In the subclass, to invoke the overridden method in the parent class, you can use the super keyword (which is found to be not used at last)
Source:
Class A
{
int i=1,j=2;
int Plus ()
{
return I +j;
}
}
Class B extends A
{
int i=10,k=20;// Overrides I of the parent class
int Plus ()
{
Return i+j+k;// overrides the parent class with the same name method
}
}
public class Father {
public void Main (string[] args)
{
A a=new a ();// Define the object of the parent class
b b=new B ();
System.out.println ("a.i=" +a.i+ "," + "a.j=" +a.j+ "," + "a.plus () =" +a.plus () "); System.out.println ("b.i=" +b.i+ "," + "b.j=" +b.j+ "," + "b.plus () =" +b.plus () ");
}
}
Hands on Brain 6