请教一个java程序,比较基础的问题

来源:百度知道 编辑:UC知道 时间:2024/05/21 17:31:36
书上的一道题,下面的方法void meth(test1 o)中的 test1为什么后面要跟一个o? 这个test1 o还能写成什么样的内容?
class test1
{
int a,b;
test1(int i,int j)
{
a=i;
b=j;
}

void meth(test1 o)
{
System.out.println(a+" "+b);
o.a*=2;
o.b/=2;
System.out.println(a+" "+b);
}
}
class callbyref
{
public static void main(String[]args)
{
test1 ob=new test1(15,20);

System.out.println("ob.j and ob.b before call:"+ob.a+" "+ob.b);
ob.meth(ob);
System.out.println("ob.a and ob.b after call:"+ob.a+" "+ob.b);
}
}

参数test1 o 的意思是一个test1类型的对象o,说个等同的例子吧:Sting s
String 就相当于test1,s 就相当于 o。这么说明白了吧

class test1
不符合java规范!
所以看起来别扭
类得名称大写!

void meth(test1 o)中的 test1为什么后面要跟一个o? 这个test1 o还能
这个o 是test1类的实例,也就是在meth方法的参数是test1的实例,那个o就是参数的名称,你可以换成任何JAVA容许的名字。
例如 o.a*=2; test1的实例o的参数a进行的运算

不是零,是字母O可以是其它合法的标识符