当前位置: 代码迷 >> J2SE >> 自己试验性的代码,运行结果居然不是小弟我所想要到的,到底哪错了
  详细解决方案

自己试验性的代码,运行结果居然不是小弟我所想要到的,到底哪错了

热度:63   发布时间:2016-04-24 12:56:52.0
自己试验性的代码,运行结果居然不是我所想要到的,到底哪错了?
以下代码我希望的结果是100,但为什么运行后结果确实10??
Java code
class Test1{    private int i = 0;    Test1(int i){        this.i = i;    }    public int value(){return i;}    public void readi(){System.out.println(Integer.toString(i));}}public class Test extends Test1{    Test(){        super(10);    }    public int value(){return super.value()*10;}    public static void main(String[] args){        Test test = new Test();        test.value();        test.readi();    }}


------解决方案--------------------

------解决方案--------------------
很简单的问题....因为最后输出的是I,而不是 I*100
------解决方案--------------------
System.out.println(test.value());
这样就是100了.
你没有输出test.value()的结果.
------解决方案--------------------
因为你的子类没有重载readi()
------解决方案--------------------
那当然,你打印的是i,父类Test1的i在实例时初始化为10,以后再也没有修改过.
------解决方案--------------------
Java code
//注意代码格式class Test1 {    private int i = 0;    Test1(int i) {        this.i = i;    }    public int value() {        return this.i;    }    public void readi() {        System.out.println(Integer.toString(this.i));    }}public class Test extends Test1 {    Test() {        super(10);    }    public int value() {        return super.value() * 10;    }    public static void main(String[] args) {        Test test = new Test();        System.out.println(test.value());  //看看这行输出多少        test.readi();  //i 当然等于 10,因为你 super(10); 了    }}
  相关解决方案