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. https://blog.csdn.net/huyuyang6688/article/details/42566043

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){
		this.name=name;
	}
	public void SetName(String name){
		this.name=name;
	}
}

 class Cat extends Animal{ 
	public String furcolor;
	Cat(String name,String furcolor){
        super(name);
		this.furcolor=furcolor;
	}
	public void SetFurcolor(String color){
		this.furcolor=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");
		System.out.println(a.name);
		//a.SetFurcolor(" pornographic");
		//System.out.println(a.furcolor);				
	}
}

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


Recommended>>
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字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号