this的用法:
package cc.openhome;
public class Somese {
private int a=10;
private String text="n.a.";
int getA()
{
return a;
}
String getText()
{
return text;
}
public Somese(int a)
{
****if(a>0){this.a=a;}****
}
public Somese(int a,String text)
{
****if(a>0){this.a=a;}****
if(text!=null){
this.text=text;
}}
public static void main(String[] args) {
Somese some=new Somese(1);
System.out.printf("Somese(%d%n", some.getA());
Somese come=new Somese(1,"string");
System.out.printf("Somese%d,%s%n", come.getA(),come.getText());
}
}
看**以上重点部分,可以是个重复部分,所以我们可以用this来改变一下;
final关键字:
如果局部变量声名了final,表示设值后就不能再变动,对象也可以声名为final,那一定要在构造函数中对于该数据成员有指定值动作,否则编译错误。
改变为: