The multi-state of the-java Foundation of Dark Horse programmer

Source: Internet
Author: User

Multi-State of object-oriented

polymorphic (one of the object-oriented features): The function itself is polymorphic, a certain thing has different concrete embodiment.

Embodiment: A reference to a parent class or interface refers to its own subclass object. Animal a = new Cat ();

Benefits of polymorphism: improve the extensibility of the program.

Disadvantages of polymorphism: When a parent class reference points to a subclass object, the extensibility is improved, but only the methods available in the parent class can be accessed, and the methods specific to the subclasses are not accessible. ( late-generation functionality cannot be used in the prophase, that is, access limitations )

Prerequisites for polymorphism:

1: Must have a relationship, such as inheritance, or implementation.

2 : There is usually a overwrite operation.

the appearance of polymorphism has also changed: it used to be creating objects and directing objects to do things. With polymorphism, we can find the common types of objects, directly manipulate common types to do things, so you can direct a group of objects to do things, that is, by manipulating the parent class or interface implementation.

--------------------------------------------------------------

Class grandfather {

void Lecture () {

System.out.println ("Enterprise Management");

 }

void Fishing () {

System.out.println ("fishing");

 }

}

Class teacher extends

void Lecture () {

System.out.println ("JAVA");

 }

void See Movie () {

System.out.println ("See the film");

 }

}

class {

Public static void Main (string[] args) {

Uncle bi x = new teacher (); // Mr. Bi's object was promoted to the grandfather type.

x. Lectures ();

x. Watch a movie (); // error.

bi teacher y = (bi) x; // convert the Chengbi type to the teacher type.

y. See the movie ();//In polymorphic, from the beginning to the end, the subclass object is changing the type.

 }

}

---------------------------------------------------------------

If you want to use the subclass object's unique method, how to determine which object is the specific subclass of the class?

Can be instanceof by a keyword ;//Determines whether the object implements the specified interface or inherits the specified class

Format:<</span> object instanceof type > Determines whether an object belongs to the specified type.

Student instanceof person = true;//student inherits the Person class

The characteristics of polymorphism in the members of the Child parent class:

1 , member variable: in polymorphism, the child parent class member variable has the same name.

at compile time: Refer to whether the referenced variable belongs to a class that has a calling member. ( no objects are generated at compile time, only syntax errors are checked )

run time: is also a member of a class that refers to a reference -type variable that has a call.

A simple sentence: Regardless of compile and run, member variables refer to member variables in the class to which the reference variable belongs.

Besides, it's easier to remember some: member variables --- compile and run all look = left.

2 , the member function.

compile time: refers to whether the referenced variable belongs to a class that has a calling method.

Run Things: Refer to whether the object belongs to a class that has a calling method.

why is that so? because in the child parent class, there is one attribute for the identical member function: Overwrite.

simple sentence: The member function, compiled to see the reference type variable belongs to the class, run to see the object belongs to the class.

Simpler: the member function --- compiled see = left, run see = right.

3 , a static function.

compile time: Refer to whether the referenced variable belongs to a class that has a calling member.

run time: is also a member of a class that refers to a reference-type variable that has a call.

why is that so? because the static method does not belong to the object, it belongs to the class where the method resides.

calling a static method reference is the static method in which class the reference is called.

Simply put: static functions --- compile and run all see = left.

--------------------------------------------------------------------------

------Java.lang.Object

Object : the direct or indirect parent of all classes, Java believes that all objects have some basic common content, which can be extracted continuously upward, and finally extracted to a top-level class, defined in this class is the functionality of all objects.

Specific methods:

1 , Boolean equals (Object obj): used to compare two objects for equality, in fact two object addresses are compared internally.

Depending on the properties of the object, it is different to judge whether the object is the same or not. Therefore, when defining a class, it is common to replicate the Equals method, and to establish the basis for determining whether the object is the same as this particular class.

Public Boolean equals (Object obj) {

if (! ( obj instanceof person))

return false;

Person P = (person) obj;

return this.age = = P.age;

 }

2 , String toString (): turns the object into a string; The format returned by default: class name @ hash value = GetClass (). GetName () + ' @ ' + integer.tohexstring (hashcode () )

in order to make sense of the object's corresponding string content, it is possible to create a unique string representation of the class object by means of replication.

Public String toString () {

return "person:" +age;

 }

3 , Class getclass (): Gets the byte-code file object to which any object runtime belongs.

4 , int hashcode (): Returns the hash code value of the object. This method is supported to improve the performance of the hash table.

Usually Equals,tostring,hashcode, in the application will be replicated, to establish specific objects of the unique content.

The multi-state of the-java Foundation of Dark Horse programmer

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.