| |

VerySource

 Forgot password?
 Register
Search
View: 1054|Reply: 9

About class inheritance

[Copy link]

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-2-4 13:30:01
| Show all posts |Read mode
There is a parent class with method p1 () and method p2 ().
There is also a class son son inheriting parent. Now I only want son to inherit p1 () method, but not p2 () method. What should I do?
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-3-22 21:30:02
| Show all posts
Can't I change the parent method permissions?
Reply

Use magic Report

0

Threads

37

Posts

28.00

Credits

Newbie

Rank: 1

Credits
28.00

 China

Post time: 2020-3-23 09:00:01
| Show all posts
Don't want it to inherit the P2 () method. What does it mean?
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-3-23 09:15:02
| Show all posts
No, because the parent object also uses the p1 () method
Reply

Use magic Report

0

Threads

73

Posts

46.00

Credits

Newbie

Rank: 1

Credits
46.00

 Invalid IP Address

Post time: 2020-3-23 11:00:02
| Show all posts
No explanation.
If you want to prevent son from accessing p2, you can set p2 to private.
If you want to adjust the p2 of the parent class from the object of the son class, but it is still accessible from the parent type, you can hide the name p2 in the son class by providing another name with the same name but different parameters A p2.
Reply

Use magic Report

0

Threads

17

Posts

16.00

Credits

Newbie

Rank: 1

Credits
16.00

 China

Post time: 2020-3-23 11:15:02
| Show all posts
Use private inheritance, and then declare the method p1 as public.
Reply

Use magic Report

1

Threads

4

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-3-23 11:30:01
| Show all posts
Don't want it to inherit the P2 () method. What does it mean?

Is that son cannot use p2 () method
Reply

Use magic Report

0

Threads

17

Posts

16.00

Credits

Newbie

Rank: 1

Credits
16.00

 China

Post time: 2020-3-23 12:00:02
| Show all posts
Of course, being able to modify the parent class is the best way.
Reply

Use magic Report

0

Threads

55

Posts

44.00

Credits

Newbie

Rank: 1

Credits
44.00

 Invalid IP Address

Post time: 2020-3-23 13:00:01
| Show all posts
Declare p2 as private
Reply

Use magic Report

0

Threads

37

Posts

28.00

Credits

Newbie

Rank: 1

Credits
28.00

 China

Post time: 2020-3-23 18:15:02
| Show all posts
Declare P2 as private.
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