为什么 animal 看不见 c ?

为什么 animal 看不见 c ?

|
JasonLawApr 25, 2023
1
这种问题问 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 |
|
frescoApr 26, 2023 via iPhone
2
自从有了 chatgpt 编程学起来也更简单了
|
|
yungo8Apr 26, 2023 via Android
3
很细节。第二张图实际是在不同包下面访问,所以访问不到。第三图是在 cat 的包下面访问,所以访问的到。你在第一个图里加个 main 一样可以用 animal 访问到 c
|
|
darkengineApr 26, 2023
6
可以这么考虑,animal.c 是要访问 test.a1.Animal 的 c ,所以在 test.a2.Cat 里是访问不了的。new Cat().c 访问的是 Cat 类的成员 c (虽然它是从父类继承的)。
|