为什么 animal 看不见 c ?
1
JasonLaw 2023-04-25 22:33:05 +08:00
这种问题问 ChatGPT 最好了。
In Java, the protected keyword is a visibility modifier that can be applied to class members (fields, methods, and nested classes) to restrict their access to the class itself, its subclasses, and other classes in the same package. More specifically, when a class member is declared as protected, it can be accessed by: The class itself Any subclass of the class, even if the subclass is in a different package Any other class in the same package as the class |
2
fresco 2023-04-26 00:08:20 +08:00 via iPhone
自从有了 chatgpt 编程学起来也更简单了
|
3
yeqizhang 2023-04-26 00:37:37 +08:00 via Android
很细节。第二张图实际是在不同包下面访问,所以访问不到。第三图是在 cat 的包下面访问,所以访问的到。你在第一个图里加个 main 一样可以用 animal 访问到 c
|
4
n2l OP |
6
darkengine 2023-04-26 08:34:43 +08:00
可以这么考虑,animal.c 是要访问 test.a1.Animal 的 c ,所以在 test.a2.Cat 里是访问不了的。new Cat().c 访问的是 Cat 类的成员 c (虽然它是从父类继承的)。
|