java - 超级关键字歧义
问题描述
以下程序
class Feline {
public String type = "f ";
public Feline() {
System.out.print("feline ");
}
}
public class Cougar extends Feline {
public Cougar() {
System.out.print("cougar ");
}
void go() {
type = "d ";
System.out.print(this.type + super.type);
}
public static void main(String[] args) {
new Cougar().go();
}
}
产生以下输出。
feline cougar d d
我无法理解为什么super.type
在生成输出时没有从超类中获取 的值并打印 anf
而不是打印本地值d
。有人可以在这里提供一些帮助吗?
解决方案
它是相反的,现在你type
被合并在一起,当你改变 的值时type
,它会覆盖super.type
。
这里有一个简短的例子作为解释:
void go() {
System.out.print(this.type);
type = "d ";
System.out.print(this.type + super.type);
}
输出:
f d d
它实际上是将super.type
值更改f
为d
推荐阅读
- haskell - Haskell缩进问题
- c# - 数据表大容量复制到 SQL Server 合并行 ID
- python - %: 'tuple' 和 'dict' 的 Django TypeError 不支持的操作数类型
- javascript - Create-React-App Auto 处理 GET 到路由
- foreach - Laravel 6 如何从列中获取计数
- disqus - 如何从 Disqus 中删除“Also on”?
- google-chrome - WebKit 537.36 - 大多数浏览器中的相同版本
- here-api - HERE API 中的流量请求返回邻近范围之外的结果
- module - 如何在 Fortran 中实现带有子模块的通用接口?
- laravel - 如何在 Laravel Query Builder 中转换 SQL 子查询?