cool hit counter [J2SE Fast Forward] - Upward Transformation and its Memory Analysis_Intefrankly

[J2SE Fast Forward] - Upward Transformation and its Memory Analysis

Copyright: This is an original post by the blogger and may not be reproduced without the blogger's permission.

I remember when I first learned object-oriented through C#, I was exposed to object transformation, and I recently learned Java and ran into it again. Here again, Java is used to analyze the transformation of objects, and memory analysis is used as appropriate to deepen understanding.

Upward transition

We all know that a dog is one of the animals, so if there is now a Dog class and an Animal class, then their relationship is that the Dog class inherits from the Animal class.

We often say that dogs are animals, but in fact the phrase "dogs are animals" is an example of upward transformation, i.e. treating dogs (subclasses) as if they were animals (parents). To sum it up in one sentence. A transition up is a reference from the parent class to an instance of the child class

Illustrative examples&& Memory Analysis

Here's a simple little example plus memory analysis to understand the upward transition.

Define the parent class Animal and the subclass Dog

 class Animal{
	public String name;
	Animal(String name){;
	public void SetName(String name){;

 class Cat extends Animal{ 
	public String furcolor;
	Cat(String name,String furcolor){
	public void SetFurcolor(String color){

Defining the UpCastingTest class

public class UpCastingTest {
	public static void main(String[] args) {		
		Animal a=new Cat(" (dialect) dragonfly"," blue (color)");		
		a.SetName(" big-skinned cat");
		//a.SetFurcolor(" pornographic");

Although the variable a of type Animal is defined in the main method, it points to an instance of type Cat, as follows.

In the memory diagram above, instantiatedDog chronological, in the heap memory of theDog The class instance will contain its parent class( That is, in the above figure pornographic part), The following section is just for yourself, References in stack memorya While pointing to the heap memory in theDog class instance, But it is, after all, aAnimal References of type, So inDog in the example, In addition to being inherited fromAnimal that part of the, The rest it doesn't see.。 So we can look at it asa essentially points to the heap memory belonging toAnimal The part of the class( i.e. pornographic that part of the)。 So inmain method, If you execute the two lines of code that are commented out “a.SetFurcolor(" pornographic");” perhaps “System.out.println(a.furcolor);” When the error is reported。

Therefore.When a reference to a parent class points to a child object, the reference to the parent class can only access that part of the properties and methods that the child class inherits from the parent class and the methods of the parent class that the child class overrides

1、Office 2016 Software Crack
2、Can robots be the same as humans Is there a reciprocal position for the future rise of artificial intelligence robots
3、This is not an artificial intelligence its an artificial retard
4、Read your faceAn Australian cafe launches face swipe ordering
5、Microsoft develops smart paintingAI henceforthPS May be cold Painters will no longer be the preserve of mankind

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送