欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java自学-类和对象 构造方法

程序员文章站 2022-06-22 10:54:27
怎么使用 Java 构造方法? 通过一个类创建一个对象,这个过程叫做 实例化 实例化是通过调用 构造方法 (又叫做 构造器 )实现的 步骤 1 : 什么是构造方法 方法名和类名一样(包括大小写) 没有返回类型 实例化一个对象的时候,必然调用构造方法 public class Hero { Strin ......

怎么使用 java 构造方法?

通过一个类创建一个对象,这个过程叫做实例化

实例化是通过调用构造方法(又叫做构造器)实现的

步骤 1 : 什么是构造方法

方法名和类名一样(包括大小写)
没有返回类型
实例化一个对象的时候,必然调用构造方法

public class hero {
 
    string name;
 
    float hp;
 
    float armor;
 
    int movespeed;
 
    // 方法名和类名一样(包括大小写)
    // 没有返回类型
    public hero() {
        system.out.println("实例化一个对象的时候,必然调用构造方法");
    }
     
    public static void main(string[] args) {
        //实例化一个对象的时候,必然调用构造方法
        hero h = new hero();
    }
 

}

步骤 2 : 隐式的构造方法

hero类的构造方法是

public hero(){ 
}

这个无参的构造方法,如果不写,就会默认提供一个

public class hero {
     
    string name; //姓名
     
    float hp; //血量
     
    float armor; //护甲
     
    int movespeed; //移动速度
     
    //这个无参的构造方法,如果不写,
    //就会默认提供一个无参的构造方法
    //  public hero(){ 
    //      system.out.println("调用hero的构造方法");
    //  }
 
    public static void main(string[] args) {
        hero garen =  new hero();
        garen.name = "盖伦";
        garen.hp = 616.28f;
        garen.armor = 27.536f;
        garen.movespeed = 350;
         
        hero teemo =  new hero();
        teemo.name = "提莫";
        teemo.hp = 383f;
        teemo.armor = 14f;
        teemo.movespeed = 330;
    }  
     
}

步骤 3 : 如果提供了一个有参的构造方法

一旦提供了一个有参的构造方法
同时又没有显式的提供一个无参的构造方法
那么默认的无参的构造方法,就“木有了“

public class hero {
      
    string name; //姓名
      
    float hp; //血量
      
    float armor; //护甲
      
    int movespeed; //移动速度
      
    //有参的构造方法
    //默认的无参的构造方法就失效了
    public hero(string heroname){ 
        name = heroname;
    }
      
    public static void main(string[] args) {
        hero garen =  new hero("盖伦"); 
          
        hero teemo =  new hero(); //无参的构造方法“木有了”
    }  
      
}

步骤 4 : 构造方法的重载
和普通方法一样,构造方法也可以重载

 public class hero {
           
        string name; //姓名
           
        float hp; //血量
           
        float armor; //护甲
           
        int movespeed; //移动速度
           
        //带一个参数的构造方法
        public hero(string heroname){ 
            name = heroname;
        }
         
        //带两个参数的构造方法
        public hero(string heroname,float herohp){ 
            name = heroname;
            hp = herohp;
        }
           
        public static void main(string[] args) {
            hero garen =  new hero("盖伦"); 
            hero teemo =  new hero("提莫",383);
        }
         
    }

练习

(为hero设计4个参数的构造方法
这四个参数分别是
string heroname
float herohp
float heroarmor
int heromovespeed)

答案

public class hero {
        
    string name; //姓名
        
    float hp; //血量
        
    float armor; //护甲
        
    int movespeed; //移动速度
        
    //带一个参数的构造方法
    public hero(string heroname){ 
        name = heroname;
    }
      
    //带两个参数的构造方法
    public hero(string heroname,float herohp){ 
        name = heroname;
        hp = herohp;
    }
    public hero(string heroname,float herohp,float heroarmor,int heromovespeed){ 
        name = heroname;
        hp = herohp;
        armor = heroarmor;
        movespeed=heromovespeed;
    }
     
    public static void main(string[] args) {
        hero garen =  new hero("盖伦"); 
        hero teemo =  new hero("提莫",383);
        hero db =  new hero("死哥",400,27,360);
    }
      
}