This is an article about how to understand correctly Java Articles on transformation mechanism
In memory of the words to be silent tomorrow, I am still reviewing Java Who asked him to take the exam tomorrow Java Well
What is transformation
What can we say about transformation ! Subclass to parent , Superclass trochanteric balabalabala . Attention should be paid to the parent class rotor class
What’s so hard to understand here , There are mainly these points :
- Let’s say that a subclass rewrites a function of the parent class , So whether the subclass changes to the parent or the parent rotor class , Call this function after turning it off. , So is it the function of the parent class or that of the subclass ?
- Who can turn into who ? Or can you turn to each other at will ?
( Of course not. )
- What is casting ?
This article will be explained in the following two classes , The first is the parent class Person, There is only one way WhatAreYou:
class People
{
public People() {
// TODO Auto-generated constructor stub
System.out.println("Father");
}
public void WhatAreYou()
{
System.out.println(" I am a human being ");
}
}
The first subclass , Man , Inherited the parent class People Of WhatAreYou, At the same time, it has its own unique method Hunt:
class Men extends People
{
public Men() {
// TODO Auto-generated constructor stub
System.out.println("Men");
}
public void Hunt() {
// Man's unique hunting skills
System.out.println("Men hunting");
}
public void WhatAreYou()
{
System.out.println("I am A Man");
}
}
The second subclass , A woman , Inherited the parent class People Of WhatAreYou, At the same time, it has its own unique method Breed( Child rearing ):
class Women extends People
{
public Women() {
// TODO Auto-generated constructor stub
System.out.println("Women");
}
public void WhatAreYou()
{
System.out.println("I am a Woman");
}
public void Breed()
{
// Only women can feed their children
System.out.println("Women Breeding child");
}
}
Subclass to parent
Let’s start with a piece of code :
public static void main(String[] args)
{
People people = new Men();
people.WhatAreYou();
people = new Women();
people.WhatAreYou();
}
The output is :
The first line outputs Father Is the parent class constructor , They all execute the parent class constructor first and then the subclass construction , You can display the call parent constructor ( keyword Super)
The second line is the subclass Men The output of the constructor for .
The climax is coming :
people yes People
Type reference , But give him one Men After the object of , Call its WhatAreYou Function is called Subclass
Function of .
Next line people assignment Women It’s the same thing .
Parent rotor class
Can this work … Look at the code below ( It’s just a screenshot )
Error is reported when the parent class is transferred to the child class
Take a look at the solution given by the compiler :
-
The first is to put
Men men = new People()
Change it toMen men = (Men)new People()
Let’s see what happens :
Oh, the compiler does not report errors , however
The runtime throws an exception
in other words ,
A parent class cannot be converted to a child class
.But there are exceptions ,
If the parent class reference is transformed from a subclass , Then it can be subclassed .
Look at the following code :
public static void main(String[] args)
{
People people = new Men();
Men men = (Men)people;
men.WhatAreYou();
}
This code skilfully turns a subclass into a parent class , From to subclass . So will it be wrong ? Look at the operation :
Perfect operation .
This actually involves the issue of whether it can be cast . For example, a Integer object , Can’t be converted to one string object , Who makes him not string What about the subclasses? ?
java Provides keywords for judging whether the mandatory transformation can be completed :instanceof, Usage is as follows :
if ( A instanceof ClassB ) { ClassB b = (ClassB)A ;}
summary
After testing , Come to the conclusion : No pressure transfer from subclass to parent , Conditional parent class and rotor class , The parent is the parent class .
Maybe so :
Child to parent is actually a reference of a parent type to an object of a subtype , And the parent type refers to this thing , Is created specifically to serve objects of the parent class , It can be said that there is a watch in it , One of the entries is the variables and functions in the parent class , The other is the corresponding variables and functions in the entity object it points to . Because the subclass is an extension of the parent class , So there must be variables and functions in the parent class in the subclass , So the table referenced by the parent class can be filled , Normal use ; And vice versa , Some of the tables referenced by subtypes are not available to the parent class , Table entry cannot be filled , So it went wrong . And the special case of the parent rotor class , May be JVM I called an object tag.
It is generally shown in the figure below :
This is the upward transformation :
Parent rotor class :
It doesn’t matter if there are functions in the entity object that are not pointed to by the table item , The big deal is not to use this function ;( That's exactly what we did )
But if there are items in the table, it is not possible to have no goals , The items in the list are registered for human use , The project has no goals. Use a hammer ?
The method of storing table items in references is one that I came up with to simplify my understanding , This may not be the case , Readers are advised to refer to the introduction JVM Self understanding of books
!!!!! One thing to remember , The function table is called according to the type of reference , But what is actually running is the function body of the entity object
Marked !!!!! It’s extremely important
Marked !!!!! It's extremely important
Or a little skin , Sons want to be Laozi
, You let him be my father, and he will go ;( Upward transformation )
I don’t want to be a son , Unless he turns out to be a son , I became Laozi for a while
, Now we’re going to turn him back into a son , You’re going to be forced to become a son ( Move down , Only cast , And there are conditions )