《一分钟掌握几个java知识点》01:构造方法、构造方法内调用构造方法
速读本文:
构造方法名要跟类名一致
构造方法内调用构造方法,使用this();
public class circle {
public double r;
public String colour;
public static final double pi=3.14;
public circle(double r){//构造方法名要跟类名一致
this.r=r;
this.colour="红色";
}
public circle(String colour){ //构造方法名要跟类名一致
this(5);//构造方法内调用构造方法,使用this();
this.colour=colour;
System.out.println("This circle's colour is:"+colour);
}
public double c(){
double r;
r=this.r;
return 2*pi*r;
}
public double area(){
double r;
r=this.r;
return pi*r*r;
}
public void show(){
String colour;
double r,c,s;
r=this.r;
c=this.c();
s=this.area();
colour=this.colour;
System.out.println("半径"+r);
System.out.println("颜色"+colour);
System.out.println("周长"+c);
System.out.println("半径"+s);
}
}