| |

VerySource

 Forgot password?
 Register
Search
Author: xujs0202

Little problems when little girls begin to learn ~~~ !!!

[Copy link]

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-2-23 21:15:01
| Show all posts
Think about it carefully:

You have a problem with this sentence
b = new de ();
There is a problem with this sentence
de b = new de (); ---------------- show de.show
da b = new de (); ---------------- show ba.show

to sum up:
The point is
1. What references go to the "call" method
2. What is the implementation of the interface
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-3-1 01:00:01
| Show all posts
It's not difficult to engage with objects, huh, huh
Reply

Use magic Report

0

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-3-1 10:00:01
| Show all posts
da b = new de (); ---------------- show ba.show


why?
Reply

Use magic Report

0

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 United States

Post time: 2020-3-1 16:15:02
| Show all posts
In fact, using new in a subclass is used when you don't know that the parent class has this method. If you know that the parent class has this method, you can choose another name if you don't want to override it in the subclass.
Reply

Use magic Report

0

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-3-2 16:30:01
| Show all posts
da b = new de (); ---------------- show ba.show


why?
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-3-2 17:15:01
| Show all posts
Because de inherits the interface, so
((Iaa) b) .show (); // call yourself de.show

in case
ba b = new de (); // b is still the base class
b.show (); // call ba.show of base class
((de) b) .show (); // will call de.show
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-3-3 09:45:01
| Show all posts
The base class da b is declared. Of course, the base class method is called first. No matter what runtime type you are

da's show method is not declared as abstract or virtual

((Iaa) b) .show ();
The actual type of b inherits from the Iaa interface, and the conversion is implemented at compile time, so the runtime b is the type of de
Reply

Use magic Report

0

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-3-6 14:30:01
| Show all posts
da b = new de (); ---------------- show ba.show


why?

I understand it this way:

1. Method belongs to class
2. Objects access methods through classes
3. There is a reference table for all methods in each class + virtual, parent method reference table for the parent class
4. If the method reference is not found in its own method table, it will go to the parent class to find
5.new keyword will create a new method in the subclass method table
6.The keyword of override will overwrite the parent virtual, abstract method table to point to the methods in the child class (even if the parent class is accessed, the method in the child class is accessed)
7. The reference of the parent object can point to the child object, but the method table of the parent class is used

ba b = new de ();
The method table we use is the method table of ba, then the method found is the method ba.show in ba
same:
If virtual show in ba, new show () in de --------------------- ba.show ()
If virtual show in ba, override show () in de ----------------- de.show ()
If show in ba, new show () in ------------------------------ ba.show () (our kind Happening)
If show in ba, override show () in de ------------------------- Compile fails

------------------------------------------- In confusion of thinking, the master is correcting- -------- -_- "----
Reply

Use magic Report

0

Threads

18

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-3-27 19:00:02
| Show all posts
I understand: because the show method in ba is not declared as virtual, so the b object declared as type ba is called its own implementation of show.
The interface must call the show method of the class that implements the interface.
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-5-16 14:00:02
| Show all posts
da's show method is declared as abstract or virtual
de's show method override
At this time, the show method called is dynamic, which is a run-time type method
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list