【其它话题】求助一段java code
登录 | 论坛导航 -> 华新鲜事 -> 心情闲聊 | 本帖共有 4 楼,分 1 页, 当前显示第 1 页 : 本帖树形列表 : 刷新 : 返回上一页
<<始页  [1]  末页>>
作者:about:home (等级:2 - 初出茅庐,发帖:52) 发表:2012-08-16 14:53:04  楼主  关注此帖
【其它话题】求助一段java code
<!--[if gte mso 9]><xml> <o:documentproperties> <o:author>Bruce Eckel</o:author> <o:version>14.00</o:version> </o:documentproperties> <o:officedocumentsettings> <o:allowpng /> </o:officedocumentsettings> </xml><![endif]--><!--[if gte mso 9]><xml> <w:worddocument> <w:view>Normal</w:view> <w:zoom>0</w:zoom> <w:trackmoves /> <w:trackformatting /> <w:punctuationkerning /> <w:validateagainstschemas /> <w:saveifxmlinvalid>false</w:saveifxmlinvalid> <w:ignoremixedcontent>false</w:ignoremixedcontent> <w:alwaysshowplaceholdertext>false</w:alwaysshowplaceholdertext> <w:donotpromoteqf /> <w:lidthemeother>EN-SG</w:lidthemeother> <w:lidthemeasian>ZH-CN</w:lidthemeasian> <w:lidthemecomplexscript>X-NONE</w:lidthemecomplexscript> <w:compatibility> <w:breakwrappedtables /> <w:snaptogridincell /> <w:wraptextwithpunct /> <w:useasianbreakrules /> <w:dontgrowautofit /> <w:splitpgbreakandparamark /> <w:enableopentypekerning /> <w:dontflipmirrorindents /> <w:overridetablestylehps /> <w:usefelayout /> </w:compatibility> <m:mathpr> <m:mathfont m:val="Cambria Math" /> <m:brkbin m:val="before" /> <m:brkbinsub m:val="-" /> <m:smallfrac m:val="off" /> <m:dispdef /> <m:lmargin m:val="0" /> <m:rmargin m:val="0" /> <m:defjc m:val="centerGroup" /> <m:wrapindent m:val="1440" /> <m:intlim m:val="subSup" /> <m:narylim m:val="undOvr" /> </m:mathpr></w:worddocument> </xml><![endif]--><!--[if gte mso 9]><xml> <w:latentstyles deflockedstate="false" defunhidewhenused="true" defsemihidden="true" defqformat="false" defpriority="99" latentstylecount="267"> <w:lsdexception locked="false" priority="0" semihidden="false" unhidewhenused="false" qformat="true" name="Normal" /> <w:lsdexception locked="false" priority="9" semihidden="false" unhidewhenused="false" qformat="true" name="heading 1" /> <w:lsdexception locked="false" priority="9" qformat="true" name="heading 2" /> <w:lsdexception locked="false" priority="9" qformat="true" name="heading 3" /> <w:lsdexception locked="false" priority="9" qformat="true" name="heading 4" /> <w:lsdexception locked="false" priority="9" qformat="true" name="heading 5" /> <w:lsdexception locked="false" priority="9" qformat="true" name="heading 6" /> <w:lsdexception locked="false" priority="9" qformat="true" name="heading 7" /> <w:lsdexception locked="false" priority="9" qformat="true" name="heading 8" /> <w:lsdexception locked="false" priority="9" qformat="true" name="heading 9" /> <w:lsdexception locked="false" priority="39" name="toc 1" /> <w:lsdexception locked="false" priority="39" name="toc 2" /> <w:lsdexception locked="false" priority="39" name="toc 3" /> <w:lsdexception locked="false" priority="39" name="toc 4" /> <w:lsdexception locked="false" priority="39" name="toc 5" /> <w:lsdexception locked="false" priority="39" name="toc 6" /> <w:lsdexception locked="false" priority="39" name="toc 7" /> <w:lsdexception locked="false" priority="39" name="toc 8" /> <w:lsdexception locked="false" priority="39" name="toc 9" /> <w:lsdexception locked="false" priority="35" qformat="true" name="caption" /> <w:lsdexception locked="false" priority="10" semihidden="false" unhidewhenused="false" qformat="true" name="Title" /> <w:lsdexception locked="false" priority="1" name="Default Paragraph Font" /> <w:lsdexception locked="false" priority="11" semihidden="false" unhidewhenused="false" qformat="true" name="Subtitle" /> <w:lsdexception locked="false" priority="22" semihidden="false" unhidewhenused="false" qformat="true" name="Strong" /> <w:lsdexception locked="false" priority="20" semihidden="false" unhidewhenused="false" qformat="true" name="Emphasis" /> <w:lsdexception locked="false" priority="59" semihidden="false" unhidewhenused="false" name="Table Grid" /> <w:lsdexception locked="false" unhidewhenused="false" name="Placeholder Text" /> <w:lsdexception locked="false" priority="1" semihidden="false" unhidewhenused="false" qformat="true" name="No Spacing" /> <w:lsdexception locked="false" priority="60" semihidden="false" unhidewhenused="false" name="Light Shading" /> <w:lsdexception locked="false" priority="61" semihidden="false" unhidewhenused="false" name="Light List" /> <w:lsdexception locked="false" priority="62" semihidden="false" unhidewhenused="false" name="Light Grid" /> <w:lsdexception locked="false" priority="63" semihidden="false" unhidewhenused="false" name="Medium Shading 1" /> <w:lsdexception locked="false" priority="64" semihidden="false" unhidewhenused="false" name="Medium Shading 2" /> <w:lsdexception locked="false" priority="65" semihidden="false" unhidewhenused="false" name="Medium List 1" /> <w:lsdexception locked="false" priority="66" semihidden="false" unhidewhenused="false" name="Medium List 2" /> <w:lsdexception locked="false" priority="67" semihidden="false" unhidewhenused="false" name="Medium Grid 1" /> <w:lsdexception locked="false" priority="68" semihidden="false" unhidewhenused="false" name="Medium Grid 2" /> <w:lsdexception locked="false" priority="69" semihidden="false" unhidewhenused="false" name="Medium Grid 3" /> <w:lsdexception locked="false" priority="70" semihidden="false" unhidewhenused="false" name="Dark List" /> <w:lsdexception locked="false" priority="71" semihidden="false" unhidewhenused="false" name="Colorful Shading" /> <w:lsdexception locked="false" priority="72" semihidden="false" unhidewhenused="false" name="Colorful List" /> <w:lsdexception locked="false" priority="73" semihidden="false" unhidewhenused="false" name="Colorful Grid" /> <w:lsdexception locked="false" priority="60" semihidden="false" unhidewhenused="false" name="Light Shading Accent 1" /> <w:lsdexception locked="false" priority="61" semihidden="false" unhidewhenused="false" name="Light List Accent 1" /> <w:lsdexception locked="false" priority="62" semihidden="false" unhidewhenused="false" name="Light Grid Accent 1" /> <w:lsdexception locked="false" priority="63" semihidden="false" unhidewhenused="false" name="Medium Shading 1 Accent 1" /> <w:lsdexception locked="false" priority="64" semihidden="false" unhidewhenused="false" name="Medium Shading 2 Accent 1" /> <w:lsdexception locked="false" priority="65" semihidden="false" unhidewhenused="false" name="Medium List 1 Accent 1" /> <w:lsdexception locked="false" unhidewhenused="false" name="Revision" /> <w:lsdexception locked="false" priority="34" semihidden="false" unhidewhenused="false" qformat="true" name="List Paragraph" /> <w:lsdexception locked="false" priority="29" semihidden="false" unhidewhenused="false" qformat="true" name="Quote" /> <w:lsdexception locked="false" priority="30" semihidden="false" unhidewhenused="false" qformat="true" name="Intense Quote" /> <w:lsdexception locked="false" priority="66" semihidden="false" unhidewhenused="false" name="Medium List 2 Accent 1" /> <w:lsdexception locked="false" priority="67" semihidden="false" unhidewhenused="false" name="Medium Grid 1 Accent 1" /> <w:lsdexception locked="false" priority="68" semihidden="false" unhidewhenused="false" name="Medium Grid 2 Accent 1" /> <w:lsdexception locked="false" priority="69" semihidden="false" unhidewhenused="false" name="Medium Grid 3 Accent 1" /> <w:lsdexception locked="false" priority="70" semihidden="false" unhidewhenused="false" name="Dark List Accent 1" /> <w:lsdexception locked="false" priority="71" semihidden="false" unhidewhenused="false" name="Colorful Shading Accent 1" /> <w:lsdexception locked="false" priority="72" semihidden="false" unhidewhenused="false" name="Colorful List Accent 1" /> <w:lsdexception locked="false" priority="73" semihidden="false" unhidewhenused="false" name="Colorful Grid Accent 1" /> <w:lsdexception locked="false" priority="60" semihidden="false" unhidewhenused="false" name="Light Shading Accent 2" /> <w:lsdexception locked="false" priority="61" semihidden="false" unhidewhenused="false" name="Light List Accent 2" /> <w:lsdexception locked="false" priority="62" semihidden="false" unhidewhenused="false" name="Light Grid Accent 2" /> <w:lsdexception locked="false" priority="63" semihidden="false" unhidewhenused="false" name="Medium Shading 1 Accent 2" /> <w:lsdexception locked="false" priority="64" semihidden="false" unhidewhenused="false" name="Medium Shading 2 Accent 2" /> <w:lsdexception locked="false" priority="65" semihidden="false" unhidewhenused="false" name="Medium List 1 Accent 2" /> <w:lsdexception locked="false" priority="66" semihidden="false" unhidewhenused="false" name="Medium List 2 Accent 2" /> <w:lsdexception locked="false" priority="67" semihidden="false" unhidewhenused="false" name="Medium Grid 1 Accent 2" /> <w:lsdexception locked="false" priority="68" semihidden="false" unhidewhenused="false" name="Medium Grid 2 Accent 2" /> <w:lsdexception locked="false" priority="69" semihidden="false" unhidewhenused="false" name="Medium Grid 3 Accent 2" /> <w:lsdexception locked="false" priority="70" semihidden="false" unhidewhenused="false" name="Dark List Accent 2" /> <w:lsdexception locked="false" priority="71" semihidden="false" unhidewhenused="false" name="Colorful Shading Accent 2" /> <w:lsdexception locked="false" priority="72" semihidden="false" unhidewhenused="false" name="Colorful List Accent 2" /> <w:lsdexception locked="false" priority="73" semihidden="false" unhidewhenused="false" name="Colorful Grid Accent 2" /> <w:lsdexception locked="false" priority="60" semihidden="false" unhidewhenused="false" name="Light Shading Accent 3" /> <w:lsdexception locked="false" priority="61" semihidden="false" unhidewhenused="false" name="Light List Accent 3" /> <w:lsdexception locked="false" priority="62" semihidden="false" unhidewhenused="false" name="Light Grid Accent 3" /> <w:lsdexception locked="false" priority="63" semihidden="false" unhidewhenused="false" name="Medium Shading 1 Accent 3" /> <w:lsdexception locked="false" priority="64" semihidden="false" unhidewhenused="false" name="Medium Shading 2 Accent 3" /> <w:lsdexception locked="false" priority="65" semihidden="false" unhidewhenused="false" name="Medium List 1 Accent 3" /> <w:lsdexception locked="false" priority="66" semihidden="false" unhidewhenused="false" name="Medium List 2 Accent 3" /> <w:lsdexception locked="false" priority="67" semihidden="false" unhidewhenused="false" name="Medium Grid 1 Accent 3" /> <w:lsdexception locked="false" priority="68" semihidden="false" unhidewhenused="false" name="Medium Grid 2 Accent 3" /> <w:lsdexception locked="false" priority="69" semihidden="false" unhidewhenused="false" name="Medium Grid 3 Accent 3" /> <w:lsdexception locked="false" priority="70" semihidden="false" unhidewhenused="false" name="Dark List Accent 3" /> <w:lsdexception locked="false" priority="71" semihidden="false" unhidewhenused="false" name="Colorful Shading Accent 3" /> <w:lsdexception locked="false" priority="72" semihidden="false" unhidewhenused="false" name="Colorful List Accent 3" /> <w:lsdexception locked="false" priority="73" semihidden="false" unhidewhenused="false" name="Colorful Grid Accent 3" /> <w:lsdexception locked="false" priority="60" semihidden="false" unhidewhenused="false" name="Light Shading Accent 4" /> <w:lsdexception locked="false" priority="61" semihidden="false" unhidewhenused="false" name="Light List Accent 4" /> <w:lsdexception locked="false" priority="62" semihidden="false" unhidewhenused="false" name="Light Grid Accent 4" /> <w:lsdexception locked="false" priority="63" semihidden="false" unhidewhenused="false" name="Medium Shading 1 Accent 4" /> <w:lsdexception locked="false" priority="64" semihidden="false" unhidewhenused="false" name="Medium Shading 2 Accent 4" /> <w:lsdexception locked="false" priority="65" semihidden="false" unhidewhenused="false" name="Medium List 1 Accent 4" /> <w:lsdexception locked="false" priority="66" semihidden="false" unhidewhenused="false" name="Medium List 2 Accent 4" /> <w:lsdexception locked="false" priority="67" semihidden="false" unhidewhenused="false" name="Medium Grid 1 Accent 4" /> <w:lsdexception locked="false" priority="68" semihidden="false" unhidewhenused="false" name="Medium Grid 2 Accent 4" /> <w:lsdexception locked="false" priority="69" semihidden="false" unhidewhenused="false" name="Medium Grid 3 Accent 4" /> <w:lsdexception locked="false" priority="70" semihidden="false" unhidewhenused="false" name="Dark List Accent 4" /> <w:lsdexception locked="false" priority="71" semihidden="false" unhidewhenused="false" name="Colorful Shading Accent 4" /> <w:lsdexception locked="false" priority="72" semihidden="false" unhidewhenused="false" name="Colorful List Accent 4" /> <w:lsdexception locked="false" priority="73" semihidden="false" unhidewhenused="false" name="Colorful Grid Accent 4" /> <w:lsdexception locked="false" priority="60" semihidden="false" unhidewhenused="false" name="Light Shading Accent 5" /> <w:lsdexception locked="false" priority="61" semihidden="false" unhidewhenused="false" name="Light List Accent 5" /> <w:lsdexception locked="false" priority="62" semihidden="false" unhidewhenused="false" name="Light Grid Accent 5" /> <w:lsdexception locked="false" priority="63" semihidden="false" unhidewhenused="false" name="Medium Shading 1 Accent 5" /> <w:lsdexception locked="false" priority="64" semihidden="false" unhidewhenused="false" name="Medium Shading 2 Accent 5" /> <w:lsdexception locked="false" priority="65" semihidden="false" unhidewhenused="false" name="Medium List 1 Accent 5" /> <w:lsdexception locked="false" priority="66" semihidden="false" unhidewhenused="false" name="Medium List 2 Accent 5" /> <w:lsdexception locked="false" priority="67" semihidden="false" unhidewhenused="false" name="Medium Grid 1 Accent 5" /> <w:lsdexception locked="false" priority="68" semihidden="false" unhidewhenused="false" name="Medium Grid 2 Accent 5" /> <w:lsdexception locked="false" priority="69" semihidden="false" unhidewhenused="false" name="Medium Grid 3 Accent 5" /> <w:lsdexception locked="false" priority="70" semihidden="false" unhidewhenused="false" name="Dark List Accent 5" /> <w:lsdexception locked="false" priority="71" semihidden="false" unhidewhenused="false" name="Colorful Shading Accent 5" /> <w:lsdexception locked="false" priority="72" semihidden="false" unhidewhenused="false" name="Colorful List Accent 5" /> <w:lsdexception locked="false" priority="73" semihidden="false" unhidewhenused="false" name="Colorful Grid Accent 5" /> <w:lsdexception locked="false" priority="60" semihidden="false" unhidewhenused="false" name="Light Shading Accent 6" /> <w:lsdexception locked="false" priority="61" semihidden="false" unhidewhenused="false" name="Light List Accent 6" /> <w:lsdexception locked="false" priority="62" semihidden="false" unhidewhenused="false" name="Light Grid Accent 6" /> <w:lsdexception locked="false" priority="63" semihidden="false" unhidewhenused="false" name="Medium Shading 1 Accent 6" /> <w:lsdexception locked="false" priority="64" semihidden="false" unhidewhenused="false" name="Medium Shading 2 Accent 6" /> <w:lsdexception locked="false" priority="65" semihidden="false" unhidewhenused="false" name="Medium List 1 Accent 6" /> <w:lsdexception locked="false" priority="66" semihidden="false" unhidewhenused="false" name="Medium List 2 Accent 6" /> <w:lsdexception locked="false" priority="67" semihidden="false" unhidewhenused="false" name="Medium Grid 1 Accent 6" /> <w:lsdexception locked="false" priority="68" semihidden="false" unhidewhenused="false" name="Medium Grid 2 Accent 6" /> <w:lsdexception locked="false" priority="69" semihidden="false" unhidewhenused="false" name="Medium Grid 3 Accent 6" /> <w:lsdexception locked="false" priority="70" semihidden="false" unhidewhenused="false" name="Dark List Accent 6" /> <w:lsdexception locked="false" priority="71" semihidden="false" unhidewhenused="false" name="Colorful Shading Accent 6" /> <w:lsdexception locked="false" priority="72" semihidden="false" unhidewhenused="false" name="Colorful List Accent 6" /> <w:lsdexception locked="false" priority="73" semihidden="false" unhidewhenused="false" name="Colorful Grid Accent 6" /> <w:lsdexception locked="false" priority="19" semihidden="false" unhidewhenused="false" qformat="true" name="Subtle Emphasis" /> <w:lsdexception locked="false" priority="21" semihidden="false" unhidewhenused="false" qformat="true" name="Intense Emphasis" /> <w:lsdexception locked="false" priority="31" semihidden="false" unhidewhenused="false" qformat="true" name="Subtle Reference" /> <w:lsdexception locked="false" priority="32" semihidden="false" unhidewhenused="false" qformat="true" name="Intense Reference" /> <w:lsdexception locked="false" priority="33" semihidden="false" unhidewhenused="false" qformat="true" name="Book Title" /> <w:lsdexception locked="false" priority="37" name="Bibliography" /> <w:lsdexception locked="false" priority="39" qformat="true" name="TOC Heading" /> </w:latentstyles> </xml><![endif]--><!--[if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin-top:0cm; mso-para-margin-right:0cm; mso-para-margin-bottom:10.0pt; mso-para-margin-left:0cm; line-height:115%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;} </style> <![endif]-->

小弟目前刚刚上手java, 看到think in java里有这一段code,如下:

//: reusing/SprinklerSystem.java

// Composition for code reuse.

class WaterSource {

private String s;

WaterSource() {

System.out.println("WaterSource()");

s = "Constructed";

}

public String toString() { return s; }

}


public class SprinklerSystem {

private String valve1, valve2, valve3, valve4;

private WaterSource source = new WaterSource();

private int i;

private float f;

public String toString() {

return

"valve1 = " + valve1 + " " +

"valve2 = " + valve2 + " " +

"valve3 = " + valve3 + " " +

"valve4 = " + valve4 + "\n" +

"i = " + i + " " + "f = " + f + " " +

"source = " + source;

}

public static void main(String[] args) {

SprinklerSystem sprinklers = new SprinklerSystem();

System.out.println(sprinklers);

}

}

 

/* Output:

WaterSource()

valve1 = null valve2 = null valve3 = null valve4 = null

i = 0 f = 0.0 source = Constructed

*///:


实在不懂为什么最后output里的source=Constructed. 特地前来请教,在线等。

欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版所有回复从这里展开收起列表
作者:about:home (等级:2 - 初出茅庐,发帖:52) 发表:2012-08-16 15:32:48  2楼
不知道有没解释对SprinklerSystem类里面定义了soure这一变量 private WaterSource source = new WaterSource(); 也就是说source 这个变量属于WaterSource类 然后main程序中System.out.println(sprinklers)会调用 public String toString() { return "valve1 = " + valve1 + " " + "valve2 = " + valve2 + " " + "valve3 = " + valve3 + " " + "valve4 = " + valve4 + "\n" + "i = " + i + " " + "f = " + f + " " + "source = " + source; } 这里面"source = " + source;这一句又会调用source这一变量(属于WaterSource类)中的 public String toString() { return s; } 而s的初始值是 Constructed
谢谢 很清楚
“ 这里面"source = " + source;这一句又会调用source这一变量(属于WaterSource类)中的
public String toString() { return s; }

而s的初始值是 Constructed ”

 

不过在"source = " + source; 这一句里,如果调用的是source这一变量的toString method, 那么不应该写成

"source = " +source.toString(); 么?

难道source可以被默认成source.toString()?

 

PS:其实这一章的标题是reusing class 和composition syntax, 一直高不懂什么意思?没看出来这个例子和composition 有什么关系。。

欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版所有回复从这里展开收起列表
作者:about:home (等级:2 - 初出茅庐,发帖:52) 发表:2012-08-16 15:41:33  3楼
前面那一大段吓到我了……想说,不是问java来着吗= =
学好java来华新抓虫。。。。
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版所有回复从这里展开收起列表
作者:about:home (等级:2 - 初出茅庐,发帖:52) 发表:2012-08-16 22:14:21  4楼
好像理解错你的问题了应该是这两个连接 http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.18.1 http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.11
谢谢 @pokka @zhj @月之女祭祀 明白了
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版所有回复从这里展开收起列表
论坛导航 -> 华新鲜事 -> 心情闲聊 | 返回上一页 | 本主题共有 4 篇文章,分 1 页, 当前显示第 1 页 | 回到顶部
<<始页  [1]  末页>>

请登录后回复:帐号   密码