java - 为什么子类无法访问超类受保护变量?
问题描述
我试图理解为什么超类中的受保护变量在子类中无法访问?
package pack1;
public class Parent {
protected int r;
public int s;
}
package pack2;
import pack1.Parent;
public class Child extends Parent {
public static void main(String[] args) {
Parent obj = new Child();
obj.r = 2; //r is not accessible here. It becomes accessible when I make it static.
obj.s = 3; //s is accessible.
}
}
解决方案
推荐阅读
- javascript - 间隔太慢
- python - 尝试使用 Selenium 从网站上的所有产品生成链接
- java - How to add a value in object in catch part?
- three.js - 将物理学应用于复杂形状的方法
- sql - DB2 使用标记的持续时间对多个日期间隔无效
- hyperledger-fabric - Hyperledger Fabric 如何执行背书,谁是 Hyperledger Fabric 的客户
- r - 在R中对齐下标轴标签
- java - 静态字段是如何通过对象引用的?
- intershop - 在 Intershop 7.10 中覆盖系统“找不到 URL”模板?
- mongodb - Mongodb求和、排序和排名