Java入门姿势【面向对象3】构造方法及其重载_方法的调用

上次我为大家写出啦“定义类与创建对象_了解局部变量”,上篇文章代码可能较多,如没有了解透彻的话请打开下方文章在进行观看一下哦!!

Java入门姿势【面向对象2】定义类与创建对象_了解局部变量

这次我们来说一下:构造方法以及重载 的使用

学习教程推荐:

构造方法及其重载

一、构造方法

对于一个类来说,一般有三种常见的成员:属性field、方法method、构造器constructor。这三种成员都可以定义零个或多个。

构造方法(constructor)也叫构造器,用于对象的初始化。构造器是一个创建对象时被自动调用的特殊方法,目的是对象的初始化。构造器的名称应与类的名称一致。Java通过new关键字来调用构造器,从而返回该类的实例,是一种特殊的方法。

声明格式:

[修饰符] 类名(形参列表){

//n条语句

}

构造器4个要点:

  • 构造器的方法名必须和类名一致!
  • 构造器通过new关键字调用!!
  • 构造器虽然有返回值,但是不能定义返回值类型(返回值的类型肯定是本类),不能在构造器里使用return返回某个值。
  • 如果我们没有定义构造器,则编译器会自动定义一个无参的构造方法。如果已定义则编译器不会自动添加!
  • 构造方法也是方法,只不过有特殊的作用而已。与普通方法一样,构造方法也可以重载。

示例一:定义计算机类并模拟其操作

示例代码:

public class Computer {
    //成员变量
    private String cpu="Intel";//cpu
    private String memory;//内存
    private String mainBoard;//主板
    private String keyBoard;//键盘
    //构造方法
    public Computer(){
        System.out.println("--------Computer()--------");
        cpu = "AMD";
    }
    public Computer(String cpu,String memory,            
String mainBoard,String keyBoard){
        this.cpu = cpu;
        this.memory = memory;
        this.mainBoard = mainBoard;
        this.keyBoard = keyBoard;
    }
//    public Computer(String cpu1,String memory1,String mainBoard1,String keyBoard1){
//        cpu = cpu1;
//        memory = memory1;
//        mainBoard = mainBoard1;
//        keyBoard = keyBoard1;
//    }
    //成员方法
    public void start(){
        System.out.println("-------starting------");
    }
    public void close(){
        System.out.println("------- closing----------");
    }
    public void show(){
        System.out.println("cpu="+cpu+",memory="+memory+
",mainBoard="+mainBoard+",keyBoard"+keyBoard);
    }
    public static void main(String[] args) {
        //购买一台电脑
        Computer computer2 = new Computer();
//        computer.cpu="酷睿"; omputer.memory="三星";
//        computer.mainBoard="华硕"; computer.keyBoard="罗技";
        Computer computer = new Computer("酷睿","三星","华硕","罗技");
       //让电脑运行
        computer.start();
        computer.show();
        computer.close();
    }
}

需要注意的地方:

1)对象的创建完全是由构造方法实现的吗?

不完全是。构造方法是创建Java对象重要途径,通过new关键字调用构造器时,构造器也确实返回了该类对象,但这个对象并不是完全由构造器负责创建的。创建一个对象分为如下四步:

1. 分配对象空间,并将对象成员变量初始化为0或空

2. 执行属性值的显示初始化

3. 执行构造方法

4. 返回对象的地址给相关的变量

2)如果方法构造中形参名与属性名相同时,需要使用this关键字区分属性与形参。

this.id 表示属性 id; id 表示形参 id

二、对象数组

        学习到这里在前面我们已经学过数组了,但是数组元素都是基本数据类型或者String类型。学习了类和对象之后,可以定义数组的元素类型是更加复杂的引用引用数据类型,每个元素可以是一个具体的对象,称为:对象数组

没有过的解释,大家伙直接上手敲一下代码尝试一下:

代码示例:使用对象数组存储多台计算机信息

public class TestArray {
    public static void main(String[] args) {
        //定义一个数组,存储4个分数,并遍历
        int [] arr;
        arr = new int[4];
        arr[0] = 90;
        arr[1] = 80;
        arr[2] = 100;
        arr[3] = 54;
        for(int score : arr){    System.out.println(score);      }
        System.out.println("======================");
        //定义一个数组,存储4台计算机,并遍历
        //Computer [] arr2;
        //arr2 = new Computer[4];
        Computer [] arr2 = new Computer[4];
        arr2[0] = new Computer("酷睿","三星","华硕","罗技");
        arr2[1] = new Computer("Intel","金士顿","技嘉","双飞燕");
        arr2[2] = new Computer("AMD","三星","华硕","双飞燕");
        arr2[3] = new Computer("酷睿","金士顿","技嘉","罗技");
        for (Computer computer:arr2){
           // System.out.println(computer.toString());
            computer.show();
        }
    }
}

大家也可以自己使用 对象数组存储多个对象并输出内容!!


方法的调用

方法调是Java开发中的基本操作。理解方法调用的内存分配过程,实参形参的传递过程非常必要。方法参数分为基本数据类型和引用数据类型两种,传递参数有着实质的区别。

一、基本数据类型的方法调用

代码示例参考:

public class Point {
    int x;
    int y;
}
public class TestRefArgs {
    public static void main(String[] args) {
        //定义两个变量
        Point p = new Point();
        p.x = 10;
        p.y = 20;
        //输出交换前两个变量的值
        System.out.println("交换前:p.x="+p.x+",p.y="+p.y);
        //交换两个变量的值
        swap(p);
        //输出交换后两个变量的值
        System.out.println("交换后:p.x="+p.x+",p.y="+p.y);
    }
    public static void swap(Point p){
        p = new Point();
        int temp;
        temp = p.x;
        p.x = p.y;
        p.y = temp;
    }
}

二、引用数据类型的方法调用

代码示例参考:

public class Point {
    int x;
    int y;
}
public class TestRefArgs {
    public static void main(String[] args) {
        //定义两个变量
        Point p = new Point();
        p.x = 10;
        p.y = 20;
        //输出交换前两个变量的值
        System.out.println("交换前:p.x="+p.x+",p.y="+p.y);
        //交换两个变量的值
        swap(p);
        //输出交换后两个变量的值
        System.out.println("交换后:p.x="+p.x+",p.y="+p.y);
    }
    public static void swap(Point p){
        p = new Point();
        int temp;
        temp = p.x;
        p.x = p.y;
        p.y = temp;
    }
}

注意事项:基本数据类型的参数是值传递,引用数据类型的参数传递是引用(地址),本质上也是值传递。

三、this的使用

对象创建的过程和this的本质

构造方法是创建Java对象的重要途径,通过new关键字调用构造器时,构造器也确实返回该类的对象,但这个对象并不是完全由构造器负责创建。创建一个对象分为如下四步:

1. 分配对象空间,并将对象成员变量初始化为0或空

2. 执行属性值的显示初始化

3. 执行构造方法

4. 返回对象的地址给相关的变量

this的本质就是“创建好的对象的地址”! 由于在构造方法调用前,对象已经创建。因此,在构造方法中也可以使用this代表“当前对象” 。

this最常的用法:

  • 调用成员变量:如果成员变量和局部变量同名,this必须书写,用来区分两者;如果没有同名的局部变量,this可以不写
  • 调用成员方法:这种情况下,this可以省略
  • 调用构造方法:使用this关键字调用重载的构造方法,避免相同的初始化代码。但只能在构造方法中用,并且必须位于构造方法的第一句。
  • this不能用于static方法中。

代码示例参考:this关键字的使用

public class Student {
    //成员变量
    private int sno;//学号
    private String name;//姓名
    private String sex;//性别
    private double score;//分数
    //构造方法
    public Student(){
    }
    public Student(int sno,String name,String sex ){
        this.sno = sno;
        this.name = name;
        this.sex = sex;
    }
    public Student(int sno,String name,String sex ,double score){
        this(sno,name,sex);
        //new Student(sno,name,sex);
//        this.sno = sno;
//        this.name = name;
//        this.sex = sex;
        this.score = score;
    }
    //成员方法
    public void study(){
        this.shout();
        shout();
        //System.out.println("好好学习,天天向上。add oil!!!");
        System.out.println("在教室努力的学习中,代码量一行行提升了.....");
    }
    public void shout(){
        System.out.println("好好学习,天天向上。add oil!!!");
    }
    public void show(){
        System.out.println(sno +"   "+this.name+"  "+sex+"  "+this.score);
    }
    public static void main(String[] args) {
        Student stu = new Student(7,"田七","男",77);
        stu.study();
        stu.shout();
        stu.show();
        //study();
        Student stu2 = new Student();
    }
}

以上就是本章节所讲述的全部内容啦,稍后我在更新后续哦,喜欢的伙伴支持一下哦~

感谢观看~

おすすめ

転載: blog.csdn.net/LSFZ88888/article/details/119853556