Java object Transformation, dynamic binding, interface

Source: Internet
Author: User

First, the object transformation Casting

//Object Transformation Castingclassanimal{ PublicString name; Animal (String name) { This. Name =name; }}classCatextendsanimal{ PublicString Eyescolor; Cat (string n, string c) {Super(n); Eyescolor=C; }}classDogextendsanimal{ PublicString Furcolor; Dog (string n, string c) {Super(n); Furcolor=C; }} Public classcasting{ Public Static voidMain (String[]args) {Animal a=NewAnimal ("name"); Cat C=NewCat ("CatName", "Blue"); Dog D=NewDog ("Dogname", "BLACK"); System.out.println (AinstanceofAnimal); System.out.println (cinstanceofAnimal); System.out.println (dinstanceofAnimal); System.out.println (AinstanceofCat); A=NewDog ("Bigyellow", "Yellow");        System.out.println (A.name); //System.out.println (A.furcolor); WrongSystem.out.println (AinstanceofAnimal); System.out.println (AinstanceofDog); Dog D1=(Dog) A;    System.out.println (D1.furcolor); }}
true true true false Bigyellow true true Yellow
View Code

1, Object transformation: The parent class refers to the subclass object, as in the above code, Animal a = new Dog ("Bigyellow", "yellow"); The Animal reference type variable a points to the child class object, dog.

P.s. Mentions inheritance, when a subclass inherits the parent class, the child class object contains the corresponding parent class object. As shown, but a can only access member variables within its own object. (Although the reference points to dog obj, you can only see members of the animal obj) cannot access Furcolor (the error is in the figure.) =) to access a member variable within a subclass object, force type conversion. Dog D1 = (dog) A;

            

2, Advantage: The introduction of object transformation, so that the scalability of the increase. As in a method, only the type of the parameter is different (and the argument inherits the same parent), the other is consistent. Avoid repeating the structure, just pass different parameters of the code, and add new subclasses, just define the new subclass of the properties and methods, so that the parent class object to the child class.  If you want to execute the inner content of the subclass, make a forced type conversion and then call. Casting is the premise and foundation of Dynamic Binding!

Disadvantage: Only members of the parent class can be accessed, and access to members of the subclass requires coercion of type conversions.

Ii. Dynamic binding/polymorphism Binding/polymorph

Abstract classanimal{ PublicString name; Animal (String name) { This. Name =name; }        /*Public void Enjoy () {System.out.println ("yelling ..."); }    */     Public Abstract voidenjoy ();}classCatextendsanimal{PrivateString Eyescolor; Cat (string n, string c) {Super(n); Eyescolor=C; }         Public voidenjoy () {System.out.println ("Meow ..."); }}classDogextendsanimal{PrivateString Furcolor; Dog (string n, string c) {Super(n); Furcolor=C; }         Public voidenjoy () {System.out.println ("Dog barking ..."); }}classBirdextendsanimal{Bird () {Super("Bird"); }         Public voidenjoy () {System.out.println ("Bird screaming ..."); }}classlady{PrivateString name; PrivateAnimal Pet;//Define Superclass ReferenceLady (String name, Animal Pet) { This. Name =name;  This. Pet =Pet; }         Public voidMypetenjoy () {Pet.enjoy (); }} Public classtestpolymorph{ Public Static voidmain (String [] args) {Cat C=NewCat ("CatName", "Blue"); Dog D=NewDog ("Dogname", "BLACK"); Bird b=NewBird (); Lady L1=NewLady ("L1", c); Lady L2=NewLady ("L2", D); Lady L3=NewLady ("L3", B);        L1.mypetenjoy ();        C.enjoy ();        L2.mypetenjoy ();    L3.mypetenjoy (); }}
Meow...meow ... Dog Barking...bird screaming ....
View Code

1, dynamic binding: According to the actual incoming object calls its corresponding method.

Lady L1 = new Lady ("L1", c); The Lady class parameter is animal, but the actual incoming cat (casting implementation) is called as an overriding method of the incoming Cat C based on the final result.

Dynamic: Java, during execution (Java Directives), recognizes the type of the actual incoming obj.  Binding: The original animal obj internal method pointer (like a C + + function pointer, stored in the animal method in the code Segment address, through which the pointer can find the method, execution method), changed to the incoming OBJ (that is, the method defined in cat type) A call that causes the last rendered result to be a method of cat. That is enjoy () changed from the original animal enjoy () to cat enjoy ()

            

Java object Transformation, dynamic binding, interface

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.