构造函数

1.构造函数

  java构造函数 ,也称构造方法、构造器。是一种特殊的函数:函数名与类名相同而且无返回值。其中分为无参构造函数和有参构造函数,当我们没有显示的构造函数,java为我们提供的隐式无参构造函数会生效,当我们类中存在显示的构造函数无论有参无参,java提供的隐式无参构造函数将不会生效。

 1 package xyz;
 2 
 3 public  class Person {
 4     public String shengao;
 5     public String tizhong;
 6     public String xingbie;
 7     //有参构造
 8     public Person(String shengao ,String tizhong ,String xingbie ){
 9         this.shengao = shengao;
10         this.tizhong = tizhong;
11         this.xingbie = xingbie;
12     };
13     
14     public String getShengao() {
15         return shengao;
16     }
17 
18     public void setShengao(String shengao) {
19         this.shengao = shengao;
20     }
21 
22     public String getTizhong() {
23         return tizhong;
24     }
25 
26     public void setTizhong(String tizhong) {
27         this.tizhong = tizhong;
28     }
29 
30     public String getXingbie() {
31         return xingbie;
32     }
33 
34     public void setXingbie(String xingbie) {
35         this.xingbie = xingbie;
36     }
37 
38 
39     public String mmp(String fuck){
40         System.out.println("person : mmp");
41         System.out.println("shengao:"+this.shengao);
42         System.out.println("tizhong:"+this.tizhong);
43         System.out.println("xingbie:"+this.xingbie);
44         return fuck;
45     }
46     
47 }

 

作用:用来初始化成员属性和成员方法的。

特点:1.函数名与类名相同。

   2.不用申明返回值类型(不同于void类型返回值,void是没有具体返回值类型;构造函数是连类型都没有)。

     3.一个对象建立后,构造函数只运行一次如果想给对象的值再赋新的值,就要使用set和get方法,此时是当做一般函数使用。

       4.构造方法的主要作用是完成对象的初始化工作,它能够把定义对象时的参数传给对象的域。 

       5.构造方法不能由编程人员调用,而要系统调用。 

       6.一个类可以定义多个构造方法,如果在定义类时没有定义构造方法,则编译系统会自动插入一个无参数的默认构  造器,这个构造器不执行任何代码。

       7.构造方法可以重载,以参数的个数,类型,或排列顺序区分。

1 public static void main(String[] args) {
2         // 创建了一个person对象,构造器给person对象的身高,体重赋值了,性别赋值为空
3         Person person = new Person("172cm","45kg",null);
4         //这个时候想要给这个person对象性别赋值,就只能使用普通方法如:set和get方法
5         person.setXingbie("女");
6         person.mmp("");
7     }

结果输出如下:

      shengao:172cm
      tizhong:45kg
      xingbie:女

2.静态代码块和构造函数的执行顺序

 

 1 public class KeepStaticCode {
 2 
 3     static
 4     {
 5         System.out.println("1");
 6     }
 7     {
 8         System.out.println("2");
 9     }
10     public KeepStaticCode()
11     {
12         System.err.println("3");//err
13     }
14     public static void main(String[] args) {
15         // TODO Auto-generated method stub
16         new KeepStaticCode();
17     }
18 
19 }

结果总结:

  12 3 其中3的位置不确定,而且为红色; 
1和2顺序是不变的,因为它们是静态(类装载时被执行),3是在构造函数里。所以,程序执行顺序是静态部分然后是构造函数。但是输出结果还要收到System.out和System.err的影响。System.out是行缓冲而System.err是不缓冲,所以System.err.println(“3”)是立即输出。但System.out.println(“1”)和System.o……                  System.err.println(“3”); 改成System.out.print(“3”) 就是123,静态块先于main方法执行,加载类最先执行,最后进入构造块。

     

3.拓展思考

其实,构造函数里面有一个值得思考的问题:为什么构造函数没有返回值?当创建对象的时候,我们又得到了一个实例比如: String  abc =  new String("hello world");  为什么呢?有没有方法确定知道构造函数有没有返回??

 有兴趣可以去寻找一下答案!!

 提供网上比较有参考价值的两段话:

For purposes other than simple initialization, classes can have constructors. Constructors are blocks of statements that can be used to initialize an object before the reference to the object is returned by new. Constructors have the same name as the class they initialize. Like methods, they take zero or more arguments, but constructors are not methods and thus have no return type. Arguments, if any, are provided between the parentheses that follow the type name when the object is created with new. Constructors are invoked after the instance variables of a newly created object of the class have been assigned their default initial values and after their explicit initializers are executed. 

We create the object sun refers to using new. The new construct is by far the most common way to create objects (we cover the other ways in Chapter 16). When you create an object with new, you specify the type of object you want to create and any arguments for its construction. The runtime system allocates enough space to store the fields of the object and initializes it in ways you will soon see. When initialization is complete, the runtime system returns a reference to the new object.

翻译:

  出于简单初始化以外的目的,类可以具有构造函数。 构造函数是可以在new返回对对象的引用之前初始化对象的语句块。 构造函数与它们初始化的类具有相同的名称。 与方法一样,它们采用零个或多个参数,但构造函数不是方法,因此没有返回类型。 当使用new创建对象时,在类型名称后面的括号之间提供参数(如果有)。 在为类的新创建的对象的实例变量分配其默认初始值并且在执行显式初始化程序之后,将调用构造函数。

  我们创建对象sun指的是使用new。 新构造是迄今为止创建对象的最常用方法(我们将在第16章介绍其他方法)。 使用new创建对象时,可以指定要创建的对象的类型以及其构造的任何参数。 运行时系统分配足够的空间来存储对象的字段,并以您很快看到的方式对其进行初始化。 初始化完成后,运行时系统将返回对新对象的引用。

 Stack Overflow 也有讨论 :https://stackoverflow.com/questions/1788312/why-do-constructors-not-return-values?noredirect=1&lq=1

                                   《完》  

 

posted @ 2018-07-06 16:32  星辰忧沙  阅读(307)  评论(0编辑  收藏  举报