当前位置: 首页 > 学习 > 电脑学习 > 程序设计 > JAVA > 基础教程 > 正文

把对象作为参数

http://www.zk168.com.cn  招考学习网 2006-4-11 0:54:32
-----------------------------------------------------------[交流]-[打印]-[发送]-[收藏]--

到目前为止,我们都使用简单类型作为方法的参数。但是,给方法传递对象是正确的,也是常用的。例如,考虑下面的简单程序:

// Objects may be passed to methods.class Test { int a,b;

Test(int i,int j) {a = i; b = j;

}

// return true if o is equal to the invoking object

boolean equals(Test o) {
if(o.a == a && o.b == b) return true;
else return false;

}
}

class PassOb {

public static void main(String args[]) { Test ob1 = new Test(100,22);Test ob2 = new Test(100,22);Test ob3 = new Test(-1,-1);

System.out.println("ob1 == ob2: " + ob1.equals(ob2));

System.out.println("ob1 == ob3: " + ob1.equals(ob3));
}
}

该程序产生如下输出:

ob1 == ob2: true
ob1 == ob3: false

在本程序中,在Test 中的equals() 方法比较两个对象的相等性,并返回比较的结果。也就是,它把调用的对象与被传递的对象作比较。如果它们包含相同的值,则该方法返回值为真,否则返回值为假。注意equals 中的自变量o指定Test 作为它的类型。尽管Test 是程序中创建的类的类型,但是它的使用与Java 的内置类型相同。

对象参数的最普通的使用涉及到构造函数。你经常想要构造一个新对象,并且使它的初始状态与一些已经存在的对象一样。为了做到这一点,你必须定义一个构造函数,该构造函数将一个对象作为它的类的一个参数。例如,下面版本的Box 允许一个对象初始化另外一个对象:

// Here,Box allows one object to initialize another.

class Box { double width; double height; double depth;

// construct clone of an object

Box(Box ob) { // pass object to constructor
width = ob.width;
height = ob.height;
depth = ob.depth;

}
// constructor used when all dimensions specified

Box(double w,double h,double d) {width = w; height = h;depth = d;

}

// constructor used when no dimensions specified

Box() { width = -1; // use -1 to indicate height = -1; // an uninitializeddepth = -1; // box

}

// constructor used when cube is created Box(double len) { width = height = depth = len;}

// compute and return volume double volume() { return width * height * depth;}}

class OverloadCons2 {

public static void main(String args[]) { // create boxes using the various constructorsBox mybox1 = new Box(10,20,15);Box mybox2 = new Box();Box mycube = new Box(7);

Box myclone = new Box(mybox1);

double vol;
// get volume of first box
vol = mybox1.volume();
System.out.println("Volume of mybox1 is " + vol);
// get volume of second box
vol = mybox2.volume();
System.out.println("Volume of mybox2 is " + vol);

// get volume of cube
vol = mycube.volume();
System.out.println("Volume of cube is " + vol);

// get volume of clone
vol = myclone.volume();
System.out.println("Volume of clone is " + vol);

}
}

在本程序中你能看到,当你开始创建你自己的类的时候,为了方便高效的构造对象,必须为同一构造函数方法提供多种形式。

-----------------------------------------------------------[交流]-[打印]-[发送]-[收藏]--
最新入库:
 
·实质、过程及意义——阿多尔诺“否定的辩证法”探微
·从Ontology的译名之争看哲学术语的翻译原则
·论马克思主义哲学经典的解释——解释学方法及其在马克
·中国哲学当前的核心与周边问题
·和合学与21世纪文化价值和科技
·中国文化的和合精神与21世纪
·宗教之间理当相互宽容
·上半个世纪的自由主义
·殷周至春秋时期神人关系之演进
·大学之道:构建以“三纲八目”为核心的道德修养体系
相关内容:
 
网友点评:
 
会员名称:
密码:匿名 ·注册·忘记密码?
评论内容:
(最多300个字符)
  查看评论
友情提醒:
 1.库中的资料大都来自互联网、网友上传、各类书籍,在录入的过程中难免会出现错误,恳请网
 友来信指正!
 2.如果网友在本库中未能找到所需要的材料,请登陆到我们的论坛《招考学习网》版块!
 3.考友想加入招考学习网的编辑部,请发信到XueXiWang#Gmail.com(#改为@)附带个人简历
 4.如需转载请注明出处及作者,谢谢合作!
 5.如果您有更好的建议或意见请EMAIL:XueXiWang#Gmail.com (#改为@)
 6.凡标题中有注有“[NO]”字样均不含答案且答案整理中.
 7.如本库中转载文章涉及版权等问题,请相关网站或作者在两周内发邮件通知(EMAIL:  XueXiWang#Gmail.com (#改为@))我们,我们接到通知后立即删除该文章及链接!
你问我答 更多>>