Java object Transformation

Source: Internet
Author: User

Definition

We will also encounter this problem before using c #. Here we will review it again. With memory analysis, you can get a better understanding. First, let's talk about the meaning of this object transformation. And the benefits of object transformation for extended code.

Background: When a subclass inherits a parent class object, the subclass has attributes and methods that do not exist in the parent class. at this time, the object of the parent class cannot see which attributes of the Child class are not public to the parent class.

Instance I

// Public class TestConvertObject {public static void main (String [] args) {Animal d = new Dog ("dahuang", "black "); // indicates that d is a member of the parent class System. out. println (d instanceof Animal); System. out. println (d instanceof Dog); d. superMethod (); Dog d1 = (Dog) d; d1.ChildMethod () ;}} class Animal {private String name; Animal (String name) {this. name = name;} protected void SuperMethod () {System. out. println ("parent class method") ;}} class Dog extends Animal {private String color; Dog (String name, String color) {super (name); this. color = color;} protected void ChildMethod () {System. out. println ("subclass method ");}}


Result


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + logs/LX08DgtcS21M/zoaPX7rrzzai5/logs/e1xNK7teO + zcrHo6y4uMDg0v3Tw9a4z/logs + zcrH1eK49mS21M/logs Yse21M/release + cjxwp1_vcd4kpha + PGJyPgo8L3A + release "brush: java;"> public class TestConvertObjectDouble {public static void main (String [] args) {Person p = new Person ("cfl"); // instantiate the TestConvertObjectDouble class to call the following Test method TestConvertObjectDouble t = new TestConvertOb JectDouble (); // instantiate the student object of the subclass Student s = new Student ("sname", "23 "); // instantiate the subclass Teacher object Teacher te = new Teacher ("milaoshi", "jiaoshou"); // by calling a unified method, you do not need to add one by one to better expand the class, you only need to upload different subclass objects. You do not need to pay attention to how to implement t. test (s); t. test (te);} public void Test (Person p) {System. out. println ("name:" + p. name); // when the subclass is studnet, it directly calls the attributes and methods of student if (p instanceof Student) {Student s1 = (Student) p; System. out. println (s1.name + s1.age);} else {Teacher t1 = (Teacher) p; System. out. println (t1.name + t1.position) ;}} class Person {protected String name; Person (String name) {this. name = name ;}} class Student extends Person {protected String age; Student (String name, String age) {super (name); this. age = age ;}} class Teacher extends Person {public String name; protected String position; Teacher (String name, String position) {super (name); this. position = position ;}}

Result



In this example, the Test method is constructed by using the parent class reference pointing to the subclass object and object transformation. Imagine that if an object creates a new method, there will be a lot of redundancy, and each time you add a method, you need to add it again. For example, using transformation and parent class references to point to sub-class objects is like an excellent architecture designer. In the early stage of product development, various interfaces have been abstracted and encapsulated, when you need to expand other functions again, you only need to add a small part. The next one is similar to the current level of cainiao. Every time a new function is added, all interfaces and methods are written again. In this way, both efficiency and flexibility are greatly reduced. It is object-oriented, but it is actually process-oriented development. It has done a lot of repetition and is not conducive to personnel maintenance in the later stage.


Memory Analysis

The following figure shows the memory Analysis for the transformation of parent class references to subclass objects and objects.




In the figure, the reference of the parent class is stored in the stack, and the Child class object is stored in the heap. Through the figure, we can find that even if the object pointing to the subclass, the parent class cannot see the difference between the subclass and the attributes and methods of the parent class. What does it mean? For example, dog can have a nursing home. But there are not necessarily animals. Can a cat go to the home care center? It is estimated that things in the House have long been gone. This is also the meaning. Let's look at the above. After the transformation of the Object d to the dog class object, we can directly access the dog attributes and methods. The execution result is displayed in the instance.



Summary

The above is my summary and experience through learning java object transformation. I have been in touch with c # before, but I do not have a deep understanding or feeling. After talking about "the eternal java", Jack Ma understood it clearly. As the teacher often said, the blind person also experienced it. In addition to the recent experience in ksxt maintenance, there are indeed many problems in abstraction and encapsulation, as well as design concepts. There is still a lot of work to be done.


A gentleman does not need to think about code, design concepts, and code reuse. In this way, the road is long and the distance is long.

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.