Java构造方法的理解

 

  • 无参的构造方法会在创建类对象时自动生成(在你没有写任何构造方法时),我们常能在资料种看到构造方法的作用是初始化对象的内容,具体时怎么初始化的,又比较模糊。

  • public class Student {//一个student类
       String name;//姓名
       int age;//年龄

       public Student(String name, int age) {//有参构造,两个参数为在创建对象时需要传入的参数
           this.name = name;//this.name表示当前类中的变量,等号后的name为这个构造方法中的参数。这句是赋值:把通过创建对象传入的姓名赋值给当前类,起倒初始化的作用。
           this.age = age;//同上
      }

       public static void main(String[] args) {
           Student student = new Student("张三",11);//创建student对象,并传入参数,进行初始化。这部分创建了对象,会自动调用构造方法对类对象进行初始化。
           System.out.println(student.name+student.age);
      }
    }
  • 作用:

    1. 使用new关键字,本质是在调用构造器

    2. 用来初始化对象的值

  • 需求:创建四个人,张三、李四、王五、刘六赋属性

  • //构造方法
    public class Student {//一个学生类
       String name;//姓名
       int age;//年龄

       public Student(String name,int age){
           this.name = name;
           this.age = age;
      }

       public static void main(String[] args) {
           Student student1 = new Student("张三",18);
           System.out.println(student1.name+"今年"+student1.age+"岁!");

           Student student2 = new Student("李四",18);
           System.out.println(student2.name+"今年"+student2.age+"岁!");

           Student student3 = new Student("王五",18);
           System.out.println(student3.name+"今年"+student3.age+"岁!");

           Student student4 = new Student("刘六",18);
           System.out.println(student4.name+"今年"+student4.age+"岁!");

      }
    }
  • //不用构造方法
    public class Student {//一个学生类
       String name;//姓名
       int age;//年龄

       public static void main(String[] args) {
           Student student1 = new Student();
           student1.name = "张三";
           student1.age = 18;
           System.out.println(student1.name+"今年"+student1.age+"岁!");

           Student student2 = new Student();
           student2.name = "李四";
           student2.age = 18;
           System.out.println(student2.name+"今年"+student2.age+"岁!");

           Student student3 = new Student();
           student3.name = "王五";
           student3.age = 18;
           System.out.println(student3.name+"今年"+student3.age+"岁!");

           Student student4 = new Student();
           student4.name = "刘六";
           student4.age = 18;
           System.out.println(student4.name+"今年"+student4.age+"岁!");

      }
    }
  • 如果该类中不写有参构造方法,就不能在创建类对象时,传入参数(默认为无参构造)。执意要传入参数,会报红。

  • 需求:

    假设你创建了一个学生类,但不是所有的学生都会拥有同样的技能,可能有的会唱歌、跳舞,有的只会唱歌。为了满足能创建拥有不同能力的学生对象,就需要写不同参数的构造方法。

    package com.practice;

    public class Student {
       String name;//姓名
       String sing;//会唱歌
       String dance;//会跳舞

       public Student(String name, String sing,String dance) {//会唱歌、跳舞的构造方法
           this.name = name;
           this.sing = sing;
           this.dance = dance;
      }

       public Student(String name, String sing) {//会唱歌的构造方法
           this.name = name;
           this.sing = sing;
      }

       public Student(String name) {//什么也不会的构造方法
           this.name = name;
      }

       public Student() {//如果已经写了有参构造,需要用到无参构造时,需要自己写。

      }

       public static void main(String[] args) {
           Student student1 = new Student("张三","唱歌","跳舞");//三个参数,自动第调用有三个参数的构造方法
           System.out.println(student1.name+student1.sing+student1.dance+"\n");
           Student student2 = new Student("李四","唱歌");//两个参数,自动调用有两个参数的构造方法(如果存在好几个有
           // 两个参数的构造方法,会根据不同的参数数据类型进行调用)
           System.out.println(student2.name+student2.sing+student2.dance+"\n");
           Student student3 = new Student("王五");//一个参数,啥也不会
           System.out.println(student3.name+student2.sing+student2.dance+"\n");
           Student student4 = new Student();//无参,啥也没有
           System.out.println(student4.name+student4.sing+student4.dance);
      }
    }

    ===================================================================================

    张三唱歌跳舞

    李四唱歌null

    王五唱歌null

    nullnullnull

 

posted @ 2021-10-17 19:32  wKingy  阅读(281)  评论(0编辑  收藏  举报