现在的位置: 首页 > 综合 > 正文

java构造函数

2014年02月13日 ⁄ 综合 ⁄ 共 524字 ⁄ 字号 评论关闭

java构造函数
其语法格式如下:


   < modifiers>  <class_name>([< argu_list>]) {[< statements>]}


自定义构造方法:方法名与类同名,首字母大写!没有返回类型。除此之外其他都跟普通方法一样。

默认构造函数:没有形参没有方法体!如:public Person() {}


读下面的例子,体会构造函数的用法:



public class MyDate {
    private int day ;
    private int month ;
    private int year ;
    public Mydate(int d, int m, int y) {
   
     year = y;
   
        month = m;
   
        day = d;

}    

public void display() {

        System.out.println(year + " / " + month + " / " +day); 

}

public static void main(String[] args) {
             MyDate  m;

      m = new MyDate(22, 9, 2001);

      m.display();
    }   
}
输出结果:2001 / 9 / 22

抱歉!评论已关闭.