J2se review notes

Source: Internet
Author: User

Equals Method
The equals method of the object is defined as: X. equals (y) compares the addresses of two objects in the heap memory, which is equivalent to X = Y. Therefore, false is returned unless X and Y point to the same object;
If you want to use the equals method to compare two objects, you can rewrite the equals method of the object and define the method body. For example, the string class overwrites the equals method, as long as 2
String objects X and Y have the same content. X. Equals (y) returns true;

 

Forced conversion

A base class's reference type variable can point to its subclass object ;,
The reference of a base class cannot access the newly added member (attributes and methods) of its subclass object );
You can use the instanceof class name to determine whether the object pointed to by the referenced variable belongs to this class or its subclass;

When the parent class references a subclass object, it only sees the attributes and methods that are referenced as the parent class. As for the attributes and methods that are referenced as subclasses, I'm sorry, I didn't see them;

 

Class animal {
Public string name = "animal ";
Public String age = "7 ";
Animal (string name)
{
This. Name = Name;
}

}
Class cat extends animal {
Public String eyescolor;
Public String age = "3 ";
CAT (string N, string C)
{
Super (N );
Eyescolor = C;
}
}
Class dog extends animal {
Public String furcolor;
Public String age = "4 ";
Dog (string N, string C)
{
Super (N );
Furcolor = C;
}
}

Public class test {
Public static void main (string ARGs [])
{
Animal A = new animal ("name ");
Cat c = new CAT ("catname", "blue ");
DOG d = new dog ("dogname", "black ");

System. Out. println (A instanceof animal); // true
System. Out. println (C instanceof animal); // true
System. Out. println (d instanceof animal); // true
System. Out. println (A instanceof cat); // false

A = new dog ("bigyellow", "yellow"); // A is a reference of the parent class animal, but it points to the Child class dog object;
System. Out. println (A. Name); // bigyellow is assigned a value to the name because the constructor of the parent class is called first.
System. Out. println (A. Age); // 7 use the attributes of the parent class
System. Out. println (C. Age); // 3 use the attributes of the subclass.
// System. Out. println (A. furcolor );//! Error because furcolor is a property of dog and does not belong to the parent class, when the parent class references a subclass object, it only shows the attributes and methods that are referenced as the parent class. As for the attributes and methods that are referenced as child classes, I'm sorry, I didn't see them.
System. Out. println (A instanceof animal); // true
System. Out. println (A instanceof dog); // true
Dog d1 = (DOG) A; // The conversion operator to be enhanced
System. Out. println (d1.furcolor); // yellow
}

}

 

 

 

Polymorphism
There are three necessary conditions for the existence of polymorphism:
1: Inherit
2: rewrite required
3: parent class references to subclass objects
The above three conditions are met. When you call the method in the parent class to reference the method to be overwritten, the new subclass object actually calls the method of the subclass object.

Class animal {
Private string name;
Animal (string name)
{
This. Name = Name;
}
Public void enjoy {
System. Out. println ("Cry .......");
}
}

Class cat extends animal // condition 1: subclass inherits the parent class
{
Private string eyescolor;
CAT (string N, string C)
{
Super (N );
Eyescolor = C;
}
Public void enjoy (){
System. Out. println ("cat call .......");
} // Condition 2: The subclass overrides the enjoy () method of the parent class;
 
}

Class lady {
Private string name;
Private animal PET;
Lady (string name, animal PET)
{
This. Name = Name;
This. Pet = PET;
}
}

Public class test {
Public statc void main (string ARGs [])
{
Cat c = new CAT ("catname", "blue ");
Lady L1 = new lady ("L1", c); // Condition 3: the parent class references a subclass object and should have passed an animal object, the input is its subclass cat, so according
Polymorphism. Here we should call the enjoy () method of the subclass object to determine which subclass object is new.
L1.mypetenjoy (); // The output result is: CAT call .......
}

}

 

 

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.