每个类在没有声明构造方法的前提下,会自动生成一个不带参数的构造方法,如果类一但声明有构造方法,就不会产生了.证明如下:
例1:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
class person
{
person(){System.out.println("父类-person");}
person(int z){}
}
class student extends person
{
// student(int x ,int y){super(8);}
}
class Rt
{
public static void main(String[]args)
{
student student_dx=new student();//创建student类的对象
}
}
//输出结果:父类-person
|
例2:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
class person
{
person(){System.out.println("父类-person");}
person(int z){}
}
class student extends person
{
student(int x ,int y){super(8);}
}
class Rt
{
public static void main(String[]args)
{
student student_dx=new student(3,4);//创建student类的对象
}
}
//没有输出结果
|
例1说明:student类自动生成student() {super();}(前提是:student类没有声明构造方法的前提下) 'super()'是用来调用父类的构造方法.
例2中的person()方法没有被调用,说明student类没有产生student(){super();}方法.这是因为student类已经声明构造方法,默认的那个不带参数的构造方法就不产生了.
再举例:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
class person
{
person(int z){}
}
class student extends person
{
}
class Rt
{
public static void main(String[]args)
{
student student_dx=new student();//创建student类的对象
}
}
/*报错:
exercise14.java:8: 找不到符号
符号: 构造函数 person()
位置: 类 person
class student extends person
^
1 错误
*/
|
说明:student类自动产生了一个student(){super();},但是由于person类已经声明了构造方法,默认的那个带参数的构造方法没有产生.,所以报错中提到找不到构造函数person()
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
相关文章
猜你喜欢
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-27 30
-
2025-06-05 58
-
2025-05-29 31
-
2025-05-29 16
-
php解析url并得到url中的参数及获取url参数的四种方式
2025-05-29 29
热门评论

