<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head> <link rel="stylesheet" href="css/stdlayout.css" type="text/css"> <link rel="stylesheet" href="css/print.css" type="text/css"> <meta content="text/html; charset=gb2312" http-equiv="content-type"> <title>重新定义(Override)方法</title></head><body><h3><a href="http://caterpillar.onlyfun.net/Gossip/index.html">FromGossip@caterpillar</a></h3><h1><a href="JavaGossip.htm">Java Gossip: 重新定义(Override)方法</a></h1><table style="text-align: left; width: 946px; height: 32px;" border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td style="width: 676px; vertical-align: top;"><small> 类别是物件的定义书,如果原来的定义并不符合您的需求,您可以在扩充类别的同时重新定义,举个例子来说,看看下面这个类别:<br> </small> <div style="margin-left: 40px;"><small><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">public class SimpleArray {</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> protected int[] array;</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> public SimpleArray(int i) {</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> array = new int[i];</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> }</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> public void setElement(int i, int data) {</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> array[i] = data;</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> }</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> ....</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <span style="font-weight: bold; font-family: Courier New,Courier,monospace;">} </span><br> </small></div> <small> <br>这个类别设计一个简单的阵列辅助类别,不过您觉得它的setElement()方法不够安全,您想要增加一些阵列的边界检查动作,于是扩充该类别,并重新定义setElement()方法:<br> </small> <div style="margin-left: 40px;"><small><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">public class SafeArray extends SimpleArray {</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> public SafeArray(int i) {</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> super(i);</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> }</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> public void setElement(int i, int data) {</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> if(i < array.length)</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> super.setElement(i, data);</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> }</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> </span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> ....</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}</span><br> </small></div> <small> <br>这么以来,以SafeArray类别的定义所产生的物件,就可以使用新的定义方法。<br> <br>当同一个成员方法在衍生类别中被重新定义,使用此衍生类别所生成的物件来呼叫该方法时,所执行的会是衍生类别中所定义的方法,而基底类别中的同名方法并不受影响。<br> <br>在上面您看到<span style="font-weight: bold;">super(</span>)与<span style="font-weight: bold;">super</span>,如果您在衍生类别中想要呼叫基底类别的建构方法,可以使用super()方法,另外若您要在衍生类别中呼叫基底类别方法,则可以如使用super.methodName(),就如上面所示范的,但使用super()或super呼叫父类别中方法的条件是父类别中的该方法不能是"private"。<br> <br>重新定义方法时要注意的是,<span style="font-weight: bold;">您可以增大父类别中的方法权限,但不可以缩小父类别的方法权限</span>,例如在扩充SimpleArray时,您不可以这么作:<br> </small> <div style="margin-left: 40px;"><small><span style="font-weight: bold; font-family: Courier New,Courier,monospace;">public class SafeArray extends SimpleArray {</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> ....</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> private void setElement(int i, int data) {</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> ....</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <span style="font-weight: bold; font-family: Courier New,Courier,monospace;"> }</span><br style="font-weight: bold; font-family: Courier New,Courier,monospace;"> <span style="font-weight: bold; font-family: Courier New,Courier,monospace;">}</span><br> </small></div> <small> <br>尝试将setElement()方法从"public"权限缩小至"private"权限是不行的,编译器会回报以下的错误讯息:<br>
没有合适的资源?快使用搜索试试~ 我知道了~
Java SE6 技术手册(上)
共77个文件
htm:73个
css:2个
jpg:2个
4星 · 超过85%的资源 需积分: 9 30 下载量 177 浏览量
2009-08-09
22:51:48
上传
评论 1
收藏 187KB RAR 举报
温馨提示
Java SE6 技术手册(上) 林信良 分上下两册 下部 我这也有下载的
资源推荐
资源详情
资源评论
收起资源包目录
Java SE6 技术手册(上).rar (77个子文件)
Java SE6 技术手册(上)
ObjectConstructor.htm 13KB
ClassMember.htm 8KB
VariableLengthArg.htm 6KB
css
print.css 3KB
stdlayout.css 2KB
GenericsExamples.htm 8KB
RestrictingGenericsType.htm 5KB
ValueSpecificClassBodies.htm 6KB
AdvancedArrayConcept.htm 8KB
InnerClass.htm 16KB
TwoDimensionArray.htm 9KB
ArithmeticOperator.htm 8KB
GenericsExtendImplement.htm 4KB
IncrementDecrementOperator.htm 10KB
RegularExpression.htm 10KB
InterfaceMultiInheritance.htm 5KB
template.htm 1KB
PatternMatcher.htm 4KB
StringBuilder.htm 4KB
BufferedReader.htm 5KB
ComparisonConditionalOperator.htm 7KB
VariableConstant.htm 9KB
BreakContinue.htm 6KB
StringClass.htm 13KB
UserInput.htm 6KB
AutoBoxingUnBoxing.htm 6KB
ExceptionHierarchy.htm 11KB
FinalKeyword.htm 6KB
ArraysCopyOf.htm 5KB
LogicalBitwiseOperator.htm 14KB
ObjectClass.htm 13KB
ExtendParentClass.htm 7KB
LoopFor.htm 8KB
BeforeGenerics.htm 6KB
BoxingCarefully.htm 10KB
ThrowThrows.htm 10KB
OverRide.htm 18KB
AboutStaticMember.htm 11KB
EnumeratedTypes.htm 14KB
LoopWhile.htm 11KB
ConditionSwitch.htm 10KB
AbstractClass.htm 7KB
StandardIO.htm 4KB
Package.htm 11KB
ExceptionHandling.htm 8KB
RecursionMethod.htm 6KB
ImmutableString.htm 9KB
ProtectedMember.htm 12KB
FirstJava.htm 6KB
AboutThis.htm 12KB
DataType.htm 7KB
GarbageCollection.htm 5KB
images
slimCat.jpg 4KB
twoDimensionArray-1.jpg 10KB
Console.htm 5KB
JavaGossip.htm 14KB
InterfaceType.htm 11KB
CommandLineArg.htm 9KB
ArraysClass.htm 10KB
ClassABC.htm 9KB
OverloadMethod.htm 6KB
DefineGenericsClass.htm 12KB
ForEach.htm 7KB
modifier.htm 2KB
FirstJavaForC.htm 5KB
SplitString.htm 6KB
ObjectArray.htm 5KB
Assertion.htm 9KB
ConditionIf.htm 14KB
InsideEnumeratedTypes.htm 6KB
StaticImport.htm 6KB
DefaultConstructor.htm 7KB
AddMethodToEnum.htm 5KB
OutputFormat.htm 12KB
ConstantSetup.htm 9KB
GenericsTypeWildcards.htm 11KB
OneDimensionArray.htm 9KB
共 77 条
- 1
资源评论
- artmusic02012-07-05资料算齐全蛋书不像书 没有个目录或索引供翻查
bigboy909
- 粉丝: 7
- 资源: 17
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功