Topic: java新手遇到构造函数的问题,望指教 |
Print this page |
1.java新手遇到构造函数的问题,望指教 | Copy to clipboard |
Posted by: javaapihelp Posted on: 2005-02-28 22:48 class test{ int x; int y; test(){ System.out.println("this is a test"); } test(int x,int y){ this.x=x; this.y=y; System.out.println("This is a test"); } } class testzl extends test{ public static void main(String args[]){ testzl test1=new testzl(); } } 这个是编译成功 能运行的,我知道当对象test1 在实例话后test( )被引用了,可我怎么才能在mian 方法中引用test父类 带有参数的test(int x,inty)构造函数,刚开始学java,很多概念性的东西都不输,买了本《Java Primer Plus》觉得看上去很晦涩,很多地方看不明白,还望大家今后多多指教! |
2.Re:java新手遇到构造函数的问题,望指教 [Re: javaapihelp] | Copy to clipboard |
Posted by: simbas Posted on: 2005-03-01 00:54 class test{ int x; int y; test(){ System.out.println("this is a test"); } test(int x,int y){ this.x=x; this.y=y; System.out.println("This is another test"); } } class testzl extends test{ testzl(int x,int y){ super(x,y); } public static void main(String args[]){ int x=0,y=0; testzl test1=new testzl(x,y); } } |
3.Re:java新手遇到构造函数的问题,望指教 [Re: javaapihelp] | Copy to clipboard |
Posted by: 孤独王子 Posted on: 2005-03-01 10:41 super就行了 默认的是 super()调用无参的 |
4.Re:java新手遇到构造函数的问题,望指教 [Re: javaapihelp] | Copy to clipboard |
Posted by: 够了 Posted on: 2005-03-01 12:34 不是构造函数的话 也可以super.)直接调用吧 |
5.Re:java新手遇到构造函数的问题,望指教 [Re: javaapihelp] | Copy to clipboard |
Posted by: simule Posted on: 2005-04-07 11:58 楼主,这段代码能编译,但不能运行! 运行时出现:Exception in thread "main" java.lang.NoSuchMethodError: main |
6.Re:java新手遇到构造函数的问题,望指教 [Re: simule] | Copy to clipboard |
Posted by: why Posted on: 2005-04-07 12:45 simule wrote: come on, it is trivial: public class testzl extends test{ |
7.Re:java新手遇到构造函数的问题,望指教 [Re: javaapihelp] | Copy to clipboard |
Posted by: undefined Posted on: 2005-04-14 09:53 class test1 { int x; int y; test1() { System.out.println("this is a test1"); } test1(int x, int y) { this.x = x; this.y = y; System.out.println("This is a test2"); } } public class testzl extends test1 { testzl() { super(1,2); } public static void main(String args[]) { testzl test1 = new testzl(); } } |
8.Re:java新手遇到构造函数的问题,望指教 [Re: javaapihelp] | Copy to clipboard |
Posted by: ljw8080 Posted on: 2005-05-28 18:20 加不加PUBLIC是都是一样的吗、因为是如果不加的话 它不是默认为PUBLICK吗?请元老指点。 |
9.Re:java新手遇到构造函数的问题,望指教 [Re: javaapihelp] | Copy to clipboard |
Posted by: liuyan Posted on: 2005-05-29 02:54 为什么我运行这段程序,系统提示我说是“Exception in thread 'main' java.lang.NoSuchMethodError:main”啊? 是不是本人的配置有什么问题啊? |
10.Re:java新手遇到构造函数的问题,望指教 [Re: javaapihelp] | Copy to clipboard |
Posted by: 独孤孤鹄 Posted on: 2005-06-03 09:50 请问你 在保存文件的时候是用什么名称保存的,public修饰的主类保存的文件名必须和类名相同! |
11.Re:java新手遇到构造函数的问题,望指教 [Re: liuyan] | Copy to clipboard |
Posted by: 阳光地带 Posted on: 2005-07-26 10:23 liuyan wrote: 应该运行的是:testzl,不是运行test. |
12.Re:java新手遇到构造函数的问题,望指教 [Re: javaapihelp] | Copy to clipboard |
Posted by: lvmingtao Posted on: 2005-07-28 14:59 text textz1= new textz1(x,y) 在句首 用SUPER 也可以 SUPER(X,Y) |
13.Re:java新手遇到构造函数的问题,望指教 [Re: javaapihelp] | Copy to clipboard |
Posted by: 1047 Posted on: 2005-08-05 10:01 如果是用一个没有参数的实例初始化那值不是乱了吗? 无参数的也应该给值一个初始值吧 |
14.Re:java新手遇到构造函数的问题,望指教 [Re: javaapihelp] | Copy to clipboard |
Posted by: truthawp Posted on: 2005-08-05 13:56 应该是有初值的,书上只说NEW建INT数组时的默认值是0,那这里创建对象的时候应该是也是赋了这个值吧?对吗? 还想请问有关THIS的问题 这一段
这里的THIS应该指的是CLASS类中TEST中INT X,Y,如下
那是不是可以把THIS改写成TEST,就是这样:test.x=x;test.y=y;???这样改和原来有没有区别? 请问各位我概念上是不是有错误,如果没错,那这样改是否正确呢? 谢谢了 |
15.Re:java新手遇到构造函数的问题,望指教 [Re: javaapihelp] | Copy to clipboard |
Posted by: javalean Posted on: 2005-08-05 20:04 // testzl2.java class test{ int x; int y; //non args constructor test(){ System.out.println("this is a test"); } //two args constructor test(int x,int y){ this.x=x; this.y=y; System.out.println("This is a test"); System.out.println("x:"+x+",y:"+y); } } public class testzl2 extends test{ testzl2(int x,int y){ super(x,y); } public static void main(String args[]){ testzl2 test1=new testzl2(1,2); } } |
16.Re:java新手遇到构造函数的问题,望指教 [Re: javaapihelp] | Copy to clipboard |
Posted by: whyuaou Posted on: 2005-08-15 10:50 |
17.Re:java新手遇到构造函数的问题,望指教 [Re: javaapihelp] | Copy to clipboard |
Posted by: whyuaou Posted on: 2005-08-15 10:51 如果在子类中没有定义子类的构造函数,系统自动调用父类的无参函数; 如果在子类中定义了带参数的构造函数,则不能调用父类的无参构造函数,这时只能调用父类中签名相同的构造函数。 再则,与文件名同名的类必须要有 main 函数。 |
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 |