首页 > 其他 > 详细

the construction method (构造方法)

时间:2016-05-18 21:42:50      阅读:222      评论:0      收藏:0      [点我收藏+]

the methon of automatically(自动) executing(执行) in class instance(类实例化) is called the construction method

 

its name is the same as the name of class

 

 

 public class Dog{
    String name;
    int age;
  
    // 构造方法,没有返回值
    Dog(String name1, int age1){
        name = name1;
        age = age1;
        System.out.println("感谢主人领养了我");
    }
  
    // 普通方法,必须有返回值
    void bark(){
        System.out.println("汪汪,不要过来");
    }
 
    void hungry(){
        System.out.println("主人,我饿了");
    }
  
    public static void main(String arg[]){
        // 创建对象时传递的参数要与构造方法参数列表对应
        Dog myDog = new Dog("花花", 3);
    }
}

the construction method (构造方法)

原文:http://www.cnblogs.com/yuweida/p/5506427.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!