构造函数的应用
package nine;public class nine_1 {public static void main(String[] args) {nine_1 circle_1=new nine_1();//聲明nine的對象nine_1,調用無參數的構造函數;nine_1 circle_2=new nine_1(10,10);//聲明nine的對象nine_2,調用有兩個參數的構造函數System.out.println("長是: " + circle_1.width + " " +"寬是:" +circle_1.height + "面積是: " + circle_1.getArea() + "周長是: " + circle_1.getPerimeter());System.out.println("長和寬是: " + circle_2.width + " " +"寬是:" +circle_2.height + "面積是:" + circle_2.getArea() + "周長是:" + circle_2.getPerimeter());}public double width,height;nine_1()//默認調用的構造函數,給width和height賦初值為1;{width=1;height=1;}nine_1(double a,double b)//有兩個參數的構造函數,給width和height賦傳入的值;{width=a;height=b;}public double getArea() {//計算面積的方法return width*height;}public double getPerimeter()//計算周長的方法{return 2*(width+height);}}
總結
- 上一篇: Class.forName有什么作用
- 下一篇: 十三种技术文档模板_在线文档,知多少?