
java的构造器
发布日期:2021-05-04 09:31:35
浏览次数:25
分类:原创文章
本文共 758 字,大约阅读时间需要 2 分钟。
1.可以创建对象
2. 可以访问成员方法
注意:
3. 不可以用private修饰构造器
4. 构造器和类的方法名一样
5. 多个构造器可以重载
6. 没有返回值
7. 构造方法不可以继承
package com.demo;public class Constructor { private int id; private String username; public Constructor(){ System.out.println( "我是无参构造器" ); } public Constructor(int id){ this.id=id; System.out.println( 14 ); } public Constructor(int id,String username){ this.id=id; this.username=username; System.out.println( "我是两个参数的构造器" ); } public static void main(String[] args) { Constructor constructor=new Constructor( ); Constructor constructor1=new Constructor( 1 ); Constructor constructor2=new Constructor( 1,"houzhi,hold on it,just do it again and again" ); }}
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2025年03月30日 09时08分19秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!