航海王激战新手攻略:JAVA方法

来源:百度文库 编辑:神马品牌网 时间:2024/04/29 23:04:23
class Test{
int a;
Test(int i){
a=i;
}
Test incrByTen(){
Test temp=new Test(a+10);
return temp;
}
}
class RetOB{
public static void main(String args[]){
Test ob1=new Test(2);
Test ob2=ob1.incrByTen();
System.out.println(ob1.a);
System.out.println(ob2.a);
ob2=ob2.incrByTen();
System.out.println(ob2.a);
}
}
这里incrByTen()方法前为什么用Test,而不用double 或int呢,为什么用类名呢,请高手解释一下。

Test是类型,incrByTen()的返回类型为Test
这里的Test不是类名,是类的实例:
Test(int i){
a=i;
}

Test(int i)

前面定义的这个吧..