Hands on the brain 6

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.