Topic: 请帮我看看这个很简单的程序

  Print this page

1.请帮我看看这个很简单的程序 Copy to clipboard
Posted by: 地主
Posted on: 2006-07-14 10:52

class Example
{
private int i=10;
public Example()
{
System.out.println("this is a test");

}

public static void shut(int i)
{
System.out.println("I Is:"+i);
}
}
class test
{
public static void main(String[] args)
{
int i=3;
Example myexample=new Example();
myexample.shutLight Bulb;
}
}

书上说构造函数可以用来进行一些数据初始化的工作,可是程序完成后输出结果
I 的值依旧为3。请教各位高手,看看怎么回事

2.Re:请帮我看看这个很简单的程序 [Re: 地主] Copy to clipboard
Posted by: daryl
Posted on: 2006-07-14 13:44

没看懂

3.Re:请帮我看看这个很简单的程序 [Re: 地主] Copy to clipboard
Posted by: InTheTerRan
Posted on: 2006-07-14 14:42

楼主认为不是3应该是多少?

4.Re:请帮我看看这个很简单的程序 [Re: 地主] Copy to clipboard
Posted by: 地主
Posted on: 2006-07-14 14:55

感觉应该是10

5.Re:请帮我看看这个很简单的程序 [Re: 地主] Copy to clipboard
Posted by: myohmy
Posted on: 2006-07-14 16:42

地主 wrote:
class Example
{
private int i=10;
public Example()
{
System.out.println("this is a test");

}

public static void shut(int i)
{
System.out.println("I Is:"+i);
}
}
class test
{
public static void main(String[] args)
{
int i=3;
Example myexample=new Example();
myexample.shutLight Bulb;
}
}

书上说构造函数可以用来进行一些数据初始化的工作,可是程序完成后输出结果
I 的值依旧为3。请教各位高手,看看怎么回事


class Example
{
private int i=10;
public Example()//这个才是构造函数
{
System.out.println("this is a test");
System.out.println("I Is:"+i);
}

public static void shut(int i)
{

}
}
class test
{
public static void main(String[] args)
{
int i=3;
Example myexample=new Example();
myexample.shutLight Bulb;
}
}

6.Re:请帮我看看这个很简单的程序 [Re: 地主] Copy to clipboard
Posted by: myohmy
Posted on: 2006-07-14 16:44

想要10 或者
class Example
{
private int i=10;
public Example()
{
System.out.println("this is a test");

}

public void shut(int i)
{
System.out.println("I Is:"+this.i);
}
}
class test
{
public static void main(String[] args)
{
int i=3;
Example myexample=new Example();
myexample.shut(i);
}
}

7.Re:请帮我看看这个很简单的程序 [Re: 地主] Copy to clipboard
Posted by: zzhdi
Posted on: 2006-07-14 17:38

在main函数中定义的变量i 和类Example中定义的变量不是一回事,前一个是main函数的局部变量,而后者是实例变量

8.Re:请帮我看看这个很简单的程序 [Re: 地主] Copy to clipboard
Posted by: 地主
Posted on: 2006-07-15 14:12

多谢各位兄弟,小弟是初学者,以后请多多指教

9.Re:请帮我看看这个很简单的程序 [Re: 地主] Copy to clipboard
Posted by: oldb
Posted on: 2006-07-18 09:54

这个程序中构造函数没用吧

10.Re:请帮我看看这个很简单的程序 [Re: 地主] Copy to clipboard
Posted by: sunjavaduke
Posted on: 2006-07-18 12:39

构造函数只输出了一句话,构造了一个对象(既然你提供构造函数,JVM不在提供无参数的构造器)
其他的作用就没有了
输出的私有数据成员当然是对象的一个属性,并没有在成员函数中进行修改,当然还是10了

11.Re:请帮我看看这个很简单的程序 [Re: 地主] Copy to clipboard
Posted by: ynwso
Posted on: 2006-07-19 13:30

你那个shut没提供参数,编译不过的,结果是3,因为构造函数只输出一句话,不执行 i=10;


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923