输出
!
?
$%$^#$@#%^$^#%*&$%&$%:D
!
?
$%$^#$@#%^$^#%*&$%&$%:D
解决方案 »
- 初学JAVA小白请教大神这个该则么写(出现异常返回)
- ibatis连接mysql出的异常,超大侠请进。
- 编程题 求救
- Hibernate取Session需要用单例吗?
- 连接如何重新打开?
- 请问用Calendar 或Date设置的时间怎么样让它写入bios,在重启后也能生效
- 如何能够使用Java操作控制面板中的[Scheduled Tasks]?
- 请来讨论:Java中的垃圾收集与C++的直接内存管理那个更好?
- 一百分求解 :一个关于句柄的问题!!!
- 可不可以发个javamail的源程序给我,最好有注释。呵呵,时间紧迫
- 恳请 对awt,swing,applet熟悉的朋友留下联系方式
- 请问如何捕捉键盘Tab键事件?
class Sub extends Super {
protected int i = 2;
}
呢?:)
1.四个打印语句,只打印三个结果出来(如果调试的话可以看见第三个,在jb9中)
2.第四个结果竟然是10!
--------------------------------------
public class test { public static void main(String[] args) {
Sub sub = new Sub();
Super subSuper = new Sub();
System.out.println(subSuper.i);
System.out.println(sub.i);
sub.plus(1);
System.out.print(sub.i);
System.out.println(subSuper.i);
}
}class Super {
protected int i; void plus(int i) {
this.i += 1;
}
}class Sub extends Super {
protected int i = 1;
}
第二个1很正常
我不明白第三个为什么也是1???
更不明白为什么还会有10??
Sub sub = new Sub();
Super subSuper = new Sub();
System.out.println(subSuper.i);
System.out.println(sub.i);
sub.plus(8);
System.out.println(subSuper.i);
System.out.println(sub.i);
System.out.println(subSuper.i);
}
}class Super {
protected int i=0; void plus(int i) {
System.out.println("this.i "+this.i);
this.i += 1;
System.out.println("this.i "+this.i);
}
}class Sub extends Super {
protected int i = 3;
}
------------------------------------
我将上面程序改了一下,楼上的兄弟应该能看明白了吧?其实那个plus方法只对超类里的i有用,
this指的是其存在类Super的当前对象,而不是调用类test的当前对象,且基本类型int的变量作为入参传入等于是将其副本传入,原值是不变的。
我可以告诉你这个程序的运行结果和test类是没有关系的。“且基本类型int的变量作为入参传入等于是将其副本传入”
说得虽然没错,确实是按值传递,但是这个程序的运行结果和这方面没有关系。“其实那个plus方法只对超类里的i有用”
哎,有点对了,但是没有说清楚。大家请注意生成的两个对象都是Sub型别的,只不过一个被声明为Super而已,但它仍然是Sub类的对象。建议大家用Debug看一下,就清楚了。请善用开发工具。
this.i += 1;
}
这个方法怎么看怎么不顺眼,这个i,和+=1有什么用???
你用
void plus(int noti) {
this.i += 100;
}
对结果有什么影响??
System.out.println(sub.i);
sub.plus(77);
System.out.println(sub.i);
System.out.println(((Super)sub).i);