实时演示
public class Student { int roll_no; String stu_name; Student(int i, String n) { // Parameterized constructor roll_no = i; stu_name = n; } void display() { System.out.println(roll_no+" "+stu_name); } public static void main(String args[]) { Student s1 = new Student(1,"Adithya"); Student s2 = new Student(2,"Jai"); s1.display(); s2.display(); } }
在上面的程序中,程序员定义了一个带有 2 个参数的参数化构造函数。现在编译器没有在代码中添加默认构造函数,程序员也没有编写任何 0 参数构造函数。
1 Adithya 2 Jai
以上就是在Java中,默认构造函数和带参数的构造函数有什么区别?的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号