1. Run the Testinherits.java example, observe the output, and note the call relationship that summarizes the constructor method between the parent class and the subclass modifies the code of the parent constructor method, explicitly calls the other constructor of grandparent, and note whether the calling code is the first sentence, Significant Impact!
On the base class construction method called by supper must be the first statement in the constructor of a subclass
See Explorationjdksource.java example A Class A is defined in this example, and it does not have any Members: Class A {} Example directly outputs the object created by this class see Explorationjdksource.java example a Class A is defined in this example, and it does not have any members: the Class A {} example directly outputs objects created by the classes
class file compilation can be
In the previous instance, the main method calls the
public void println (Object x), which internally invokes the ValueOf method of the String class
Public String toString () {
Return GetClass (). GetName () + "@" +integer.tohexstring (hashcode);
}
The Hashcode method is a local method, with the JVM designer implementing
Let's take a look at a piece of code (example Fruit.java): 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:
Conclusion in the "+" operation, when any object is connected to a string object,
Its ToString () method is called implicitly, and by default, this method returns "class name @ + Hashcode". In order to return a meaningful
Information
, subclasses can override ToString () ZZ
1 |
in the "+" operation, when any one object is associated with a string object , the ToString () method is called implicitly when connected, and,<br> This method returns "class name @ + hashcode" by default. To return meaningful information, subclasses can override the ToString () method. |
1 2 3 |
方法覆盖要求子类与父类的方法一模一样,否则就是方法重载(overload)! 请自行编写代码测试以下特性: 在子类中,若要调用父类中被覆盖的方法,可以使用 super 关键字。 |
Class fulei{
public int Jianfa (int a,int b) {
int s;
S=a-b;
return s;
}
}
Class Add1 extends fulei{
public int Jianfa (int a,int b) {System.out.println ("Call the parent class" +SUPER.JIANFA (3,4));
int s;
S=a-b;
return s;
}
}
public class Testinherits {
public static void Main (String args[]) {
Add1 C = new Add1 ();
System.out.println ("Call subclass" +c.jianfa (3,4));
}
}
Java hands-on brain