输出
!
?
$%$^#$@#%^$^#%*&$%&$%:D
!
?
$%$^#$@#%^$^#%*&$%&$%:D
解决方案 »
- 求助:C/S之----实现客户端界面右键菜单功能
- java 改代码写方法
- 在ubuntu中用java调用shell脚本生成的PS格式文件不能打开
- String +和concat和区别?
- ===我的程序在Jbuilder 2006下运行正常,打包成jar后,运行就不正常了。为什么呀。===
- 各位,谁会使用TextPad的吗?教教小弟。先谢谢了。
- 请问大虾们,如何在jBuilder下将java编译成本地代码
- 一个令我寝食难安的问题!!!
- 我是新手,推荐初学的书籍。
- jbuilder6.0里面的jtree使用程序控制生成的吗?
- 恳请 对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);