面向对象(构造函数)

/**
* Created by 刘朋程 on 2014-5-22.博客园
*/
class GouZaoHanShu {
    private int age;                //私有变量age
    private String name;            //私有变量name

    GouZaoHanShu ()                   //默认构造函数值为空
    {
        System.out.println("A:"+"name="+name+"  "+"age="+age);
        cry();                         //调用cry方法
    }
    GouZaoHanShu (String Myname)        //带有一个函数name的构造函数
    {
        name = Myname;
        System.out.println("B:"+"name="+name+"  "+"age="+age);
        cry();                          //调用cry方法   
    }
    GouZaoHanShu (String Myname,int Myage)  //带有两个函数name和age的构造函数
    {
        name = Myname ;
        age = Myage ;
        System.out.println("C:"+"name="+name+"  "+"age="+age);
        cry();                              //调用cry方法
    }
    public void cry()                       //创建方法cry
    {
        System.out.println("cry.....");
    }
}

/**
* Created by 刘朋程 on 2014-5-22.博客园
*/


public class GouZaoHanShuDemo
{   public static void main(String[] args)
    {
        GouZaoHanShu a = new GouZaoHanShu();          //新建一个对象a
        GouZaoHanShu b = new GouZaoHanShu("张三");    //新建一个对象b
        GouZaoHanShu c = new GouZaoHanShu("李四",25); //新建一个对象c
        c.cry();                                       //调用cry方法
    }
}

原文地址:https://www.cnblogs.com/liupengcheng/p/3745366.html