00000008.htm
来自「水木清华BBS」· HTM 代码 · 共 287 行 · 第 1/2 页
HTM
287 行
y = 0; <BR> } <BR> Point( int x, int y, String name ){ <BR> this.x = x; <BR> this.y = y; <BR> this.name = name; <BR> } <BR> int getX(){ <BR> return x; <BR> } <BR> int getY(){ <BR> return y; <BR> } <BR> void move( int newX, int newY ){ <BR> x = newX; <BR> y = newY; <BR> } <BR> Point newPoint( String name ){ <BR> Point newP = new Point( -x, -y, name ); <BR> return newP; <BR> } <BR> boolean equal( int x, int y ){ <BR> if( this.x==x && this.y==y ) <BR> return true; <BR> else <BR> return false; <BR> } <BR> void print(){ <BR> System.out.println(name+" : x = "+x+" y = "+y); <BR> } <BR> } <BR> public class UsingObject{ <BR> public static void main( String args[] ){ <BR> Point p = new Point(); <BR> p.print(); //call method of an object <BR> p.move( 50, 50 ); <BR> System.out.println("** after moving **"); <BR> System.out.println("Get x and y directly"); <BR> System.out.println("x = "+p.x+" y = "+p.y); //access variabl <BR> <BR>es of an object <BR> System.out.println("or Get x and y by calling method"); <BR> System.out.println("x = "+p.getY()+" y = "+p.getY()); <BR> if( p.equal(50,50) ) <BR> System.out.println("I like this point!!!! "); <BR> else <BR> System.out.println("I hate it!!!!! "); <BR> p.newPoint( "a new point" ).print(); <BR> new Point( 10, 15, "another new point" ).print(); <BR> } <BR> } <BR> 运行结果为: <BR> C:\java UsingObject <BR> a point : x = 0 y = 0 <BR> **** after moving ***** <BR> Get x and y directly <BR> x = 50 y = 50 <BR> or Get x and y by calling method <BR> x = 50 y = 50 <BR> I like this point!!!! <BR> a new point : x = -50 y = -50 <BR> another new point : x = 10 y = 15 <BR> <BR> <BR> 1. 引 用 对 象 的 变 量 <BR> <BR> 要 访 问 对 象 的 某 个 变 量 ,其 格 式 为 : <BR> <BR> objectReference.variable <BR> <BR> 其 中 objectReference是 对 象 的 一 个 引 用 ,它 可 以 是 一 个 已 生 成 的 <BR>对 象 ,也 可 以 是 能 够 生 成 对 <BR> <BR> 例 如 :我 们 用 Point p=new Point();生 成 了 类 Point的 对 象 p后 ,可 以 用 <BR>p.x,p.y来 访 问 该 点 的 x、 y坐 <BR> <BR> p.x = 10; p.y = 20; <BR> <BR> <BR> 或 者 用 new生 成 对 象 的 引 用 ,然 后 直 接 访 问 ,如 : <BR> <BR> tx = new point().x; <BR> <BR> 2. 调 用 对 象 的 方 法 <BR> 要 调 用 对 象 的 某 个 方 法 ,其 格 式 为 : <BR> <BR> <BR> <BR> <BR> <BR> <BR> <BR> <BR> <BR>,但 是 通 过 方 法 调 用 的 方 <BR>式 来 实 现 能 更 好 地 体 现 面 向 对 象 的 特 点 ,建 议 在 可 能 的 情 况 下 尽 <BR> <BR>? <BR> <BR> <BR> <BR> <BR> <BR>型 <BR> 的 值 ,我 们 可 以 合 法 地 使 <BR>用 这 个 值 ,如 :例 6.6中 类 Point的 方 法 equal返 回 布 尔 值 ,我 们 可 以 用 它 <BR> 来 作 为 判 断 条 件 分 别 执 行 <BR> <BR> if (p.equal (20,30)){ <BR> …… //statements when equal <BR> }else { <BR> …… //statements when unequal <BR> } <BR> <BR> <BR> 另 外 ,类 Point的 方 法 newPoint返 回 该 点 关 于 原 点 的 对 称 点 ,返 回 <BR>? 也 是 一 个 Point类 型 , 我 们 <BR>可 以 访 问 它 的 变 量 或 调 用 它 的 方 法 ,如 : <BR> <BR> px = p.newPoint().x 或 <BR> px = p.newPoint(). getX(); <BR> <BR> <BR> (未 完 待 续 ) <BR> <BR> <BR> <BR> <BR>-- <BR> ^_^ ^_^ ^_^ ^_^ ^_^ ^_^^_^ ^_^ ^_^ ^_^ ^_^ ^_^^_^ ^_^ ^_^ ^_^ ^_^ ^_^^_^ ^_^ ^_^ ^_^ ^_^ ^_^^_^ ^_^ ^_^ ^_^ ^_^ ^_^^_^ ^_^ ^_^ ^_^ ^_^ ^_^ <BR>^_^ ^_^ ^_^ ^_^ ^_^ ^_^ [s[4;65HO[3;66HO[3;67HO[3;68HO[4;69HO[5;69HO[6;68HO[7;67HO[8;65HO[7;63HO[6;62HO[5;61HO[4;61HO[3;62HO[3;63HO[3;64HO[u <BR> Welcome EveryOne! <BR> <BR>※ 来源:·BBS 水木清华站 bbs.net.tsinghua.edu.cn·[FROM: 166.111.74.158] <BR><CENTER><H1>BBS水木清华站∶精华区</H1></CENTER></BODY></HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?