Java 入门必须了解的结构方法

在这里插入图片描述

什么是构造方法

类体中有两大形式的成员,其中一个是成员方法(另一个就是成员变量啦~)。成员方法又分两种,一种是普通成员方法,另一种是构造方法(有的资料中也称之为构造函数)。

所谓构造方法,就是这个类在被实例化时(创建对象时)就要执行的方法。前面学习类的声明和实例化课时里,大家已经掌握实例化对象的方法:

实例化一个Football类的对象

new关键字的作用就是实例化对象,new后面的部分即构造函数,表示new一个对象时要调用的构造方法。注意,new关键字后紧跟的只能是构造方法,不能是普通成员方法。那么如何区分构造方法和普通成员方法呢?请继续往下看。

构造方法的作用:主要完成对象的初始化工作(见代码1)。

构造方法的形式

构造方法很好识别,构造函数有如下特点:

方法的名字即为类的名字

代码1:

public class Football{
    int size;
    String color;
    
    /*
     * 声明构造方法,该方法没有带任何参数;
     * 构造方法名称和类名称都是Football;
     * 方法体内给类的成员变量设置了初始值;
     * 实例化任何对象的时候,创建出来的对象size均为10,color均为white;
     * 对象被实例化后,可以修改对象的成员size和color的值;
     */
    public Football(){
        size = 10;
        color = "white";
    }
    
    public void bounce(){
        System.out.println("bang bang bang...");
    }
}

构造方法没有返回值类型。

这句话包含两个意思:一、声明构造方法时,不能给方法添加类型。二、方法体内不能有类似return …;这样的语句。下面构造方法的声明是错误的,见代码2:

代码2:

public class Football{
    int size;
    String color;
    
    /*
     * 错误:不能在方法前添加方法类型int,不能有返回值     
     * 但是编译能通过,因为编译器将此方法作为普通方法的声明,而非构造方法。
     */
    public int Football(){
        size = 10;
        color = "white";
        return 0;//错误,构造方法不能返回数据
    }
    
    public void bounce(){
        System.out.println("bang bang bang...");
    }
}

从上面类字可以看出,如果给构造方法添加返回值类型,添加return语句后,构造方法就退化成普通成员方法了,编译器不再把它当作构造方法。

构造方法的用法要点

1)构造方法一般都应用 public 类型来说明,这样才能在程序任意的位置创建类的实例。

2)构造方法只能在实例化对象时才能被调用,不能像普通方法那样通过对象来调用构造方法。

代码3:

public class AppMain{
    public static void main(String[] args){
        Football football1 = new Football();//正确
        football1.bounce();//正确
        football.Football();//错误,不能调用构造方法
    }
}
class Football{
    int size;
    String color;
    
    /*
     * 错误:不能在方法前添加方法类型int,不能有返回值     
     * 但是编译能通过,因为编译器将此方法作为普通方法的声明,而非构造方法。
     */
    public int Football(){
        size = 10;
        color = "white";
        return 0;//错误,构造方法不能返回数据
    }
    
    public void bounce(){
        System.out.println("bang bang bang...");
    }    
    
}

3)每个类至少有一个构造方法。如果代码中一个构造方法都没写,java系统会默认添加一个不带任何参数的构造方法,方法体中没有任何代码。例如:

代码4:

public class Football {
    int size;
    String color;

    public void bounce() {
        System.out.println("我能跳起来哦:bang bang bang...");
    }
}

以上代码等价于:

public class Football {
    int size;
    String color;
    
    // 构造方法,无参数,无方法体
    public Football(){
        
    }

    public void bounce() {
        System.out.println("我能跳起来哦:bang bang bang...");
    }
}

4)构造方法不能有任何非访问性质的修饰符修饰,例如static、final、synchronized、abstract等都不能修饰构造方法。

/*
 * 错误,stitac属于非访问性质的修饰符修饰
 */
public static class Football {
    int size;
    String color;
    
    // 构造方法,无参数,无方法体
    public Football(){
        
    }

    public void bounce() {
        System.out.println("我能跳起来哦:bang bang bang...");
    }
}

5)构造方法可以重载。

重载(overloading) 是指:在一个类里面,方法名字相同,而参数不同。注意:参数的个数不同或者参数的数据类型不同,都算做参数不同。其它一切情况,参数都是相同的,都不能叫做重载。

/*
 * 文件:Football.java
 * 功能:演示构造方法的重载
 */
public class Football {
    int size;
    String color;
    
    // 在执行实例化操作构造一个对象时,将传入的大小、颜色参数设置给成员变量
    public Football(int sz,String clr){
        size = sz;
        color = clr;
    }

    /* 构造方法重载
     * 在执行实例化操作构造一个对象时,
     * 只传入颜色参数,大小为默认值10.
     * 因此,所有构造出的对象,大小全部为10.
     */
    public Football(String clr){
        size = 10;
        color = clr;
    }
    
    public void bounce() {
        System.out.println("大小:" + size + "。颜色:" 
                + color + "。我能跳起来哦:bang bang bang...");
    }
}
 1 /*
 2  * 文件:AppMain.java
 3  * 功能:测试FootBall类,演示构造方法重载
 4  */
 5 public class AppMain {
 6 
 7     public static void main(String[] args) {
 8         // TODO Auto-generated method stub
 9         
10         /* 
11          * 下面第15行被注释掉的语句是错误,
12          * 因为Football类中声明了两个构造方法,
13          * 所以java系统不会再自动增加一个不带任何参数的构造方法 
14          */
15         //Football football1 = new Football();
16         
17         Football football1 = new Football(18,"黑色");
18         Football football2 = new Football(12,"白色");
19         
20         football1.bounce();
21         football2.bounce();
22     }
23 
24 }

学习测试:

  1. 请问下面Sample类中,哪个不是构造方法,哪个是带参数的构造方法,哪个是不带参数的构造方法:
public class Sample {  
      
    private int x;  
  
    public Sample() { 
        this(1);  
    }  
      
    public Sample(int x) { 
        this.x=x;  
    }  
      
    public int Sample(int x) { 
        return x++;  
    }  
  
}
  1. 请问下面代码输出结果是什么
public class Mystery {  
    private String s;  
      
    public void Mystery() { 
       s = "constructor";  
    }  
      
    void go() {  
       System.out.println(s);  
    }  
      
    public static void main(String[] args) {  
       Mystery m = new Mystery();  
       m.go();  
    }  
}  

3.请问下面代码输出结果是什么:

A.java:

public class A{
   public A(){
      System.out.println("调用了无参的构造函数");
   }
   public A(String mess){
      System.out.println("调用了有参的构造函数\n"+
         "参数内容为:"+mess);
   }
}

Test.java:


public class Test{  
   public static void main(String [] args){  
       A a_1=new A();//调用无参的构造函数  
       A a_2=new A("Hello");//调用有参的构造函数  
   }  
}  

【资料袋】

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值