构造器

    xiaoxiao2025-06-05  19

    构造器又称为构造方法:constructor 构造器用于构造该类的实例 格式如下: 【修饰符】类名(形参列表){ //n条语句 } 是一种特殊的方法: 1. 通过new关键字调用!! 2. 构造器虽然有返回值,但是不能定义返回类型(返回值的类型肯定是本类),不能在构造器里调用return 3. 如果我们没有定义构造器,则系统会自动定义一个无参的构造函数。如果已定义则编译器不会添加! 4. 构造器的方法名必须和类名一致! 5. 作用:构造该类的对象,经常也用来初始化对象的属性

    public class Point{ int x; int y; int z; public Point(int _x,int _y,int _z){ x=_x; y=_y; z=_z; } public static void main(String[] args) { Point p=new Point(1,2,3); System.out.println(p.x); } }
    转载请注明原文地址: https://ju.6miu.com/read-1299632.html
    最新回复(0)