Transformation (Java) (. net)

Source: Internet
Author: User

/** * Parent class*/classAnimal { Public voideat () {//output parent class eat ....     }}classBird extends Animal { Public voideat () {//The bird eats ...     }     Public voidFly () {//Birds Fly    }}/** * Parent-Class human*/ Public classHuman { Public voidsleep () {//Human    }}classMale extends Human { Public voidsleep () {//Men    }}classFemale extends Human { Public voidsleep () {//Women    }} Public classSys { Public Static voidMain (string[] args) {Animal b=NewBird ();//Upward Transformationb.eat (); //b.fly (); B points to the subclass object, but at this point the subclass is lost as an upward cost and the fly () method is different from the parent classSleepNewMale ()); Sleep (NewFemale ());//the passed in parameter is subclass-----!!     }     Public Static voidSleep (Human h)//the parameter of the method is the parent class------!!! {h.sleep (); }}

Subclass overrides the eatting of the parent class ...
Man sleep.
Woman sleep.

Detailed

1. The realization of upward transformation

Animal b=new Bird (); Upward transformation
B.eat (); The Eat () method of the subclass is called
B.fly (); Error!!!!! -------B points to a subclass object, but at this point the subclass is lost as the cost of upward transformation and the fly () method differs from the parent class------
2. Why not direct Bird b=new Bird (); B.eat ()?

This does not reflect the object-oriented abstract programming ideas, and reduce the scalability of the code.

3. What are the benefits of upward transformation?

Sleep (new Male ());//The parameter passed in when the method is called is a subclass
Sleep (new Female ());

public static void Sleep (Human h)//method parameter is parent class

{

H.sleep ();

}

If the above code is to use the upward transformation, if not the upward transformation, then how many subclasses have to write here how many different ways of sleeping ~~~~~~

 classFruit { Public voidMyName () {//Fruit     } } classApple extends Fruit { Public voidMyName () {//Apple     }      Public voidMymore () {Apple 1233333}} Public classSys { Public Static voidMain (string[] args) {Fruit a=NewApple ();//Upward TransformationA.myname (); Apple AA= (Apple) A;//down transformation, compile and run without error (correct)Aa.myname ();//The sub-class is called when the downward transition isAa.mymore (); Fruit F=NewFruit (); Apple AAA= (Apple) F;//-Insecure---downward transition, compile error-free but will run incorrectlyAaa.myname ();     Aaa.mymore (); } }

I am a sub-class apple ...
I am a sub-class apple ...
I'm your little Apple ~~~~~~.
Error........

Detailed

1. Correct downward transformation

Fruit a=new Apple (); Upward transformation
A.myname ();
Apple Aa= (Apple) A; Down transformation, compile and run without error (correct)
Aa.myname ();
Aa.mymore ();

A points to a subclass of an object , so an instance of a subclass AA can also point to a AH ~ ~

After the downward transformation because all points to the child class object, so the call is of course the subclass of method ~ ~

2. Unsafe downward transition

Fruit f=new Fruit ();
Apple aaa= (Apple) F; -Insecure---downward transition, compile error-free but will run incorrectly
Aaa.myname ();
Aaa.mymore ();

F is the parent class object, the subclass of instance AAA must not point to the parent class F AH ~ ~ ~

3.Java introduction of the concept of generics in order to solve the problem of unsafe downward transformation

4. For safe type conversion, it is best to use if (A instanceof B) to determine the next ~ ~

Transformation (Java) (. net)

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.