构造方法和方法的区别:
构造方法要与类名相同,无返回类型,在类初始化的时候调用。
方法最好与类名不同,对象调用,静态方法可用类名.方法().
构造器和方法在下面三个方面区别:修饰符,返回值,命名。
1。和方法一样,构造器可以有任何访问的修饰: public, protected, private或者没有修饰(通常被package 和 friendly调用). 不同于方法的是,构造器不能有以下非访问性质的修饰: abstract, final, native, static, 或者 synchronized。
2。返回类型也是非常重要的。方法能返回任何类型的值或者无返回值(void),构造器没有返回值,也不需要void。
3。两者的命名。构造器使用和类相同的名字,而方法则不同。按照习惯,方法通常用小写字母开始,而构造器通常用大写字母开始。构造器通常是一个名词,因为它和类名相同;而方法通常更接近动词,因为它说明一个操作。
构造方法和方法中this和supper的用法区别:
"this"的用法
构造器和方法使用关键字this有很大的区别。方法引用this指向正在执行方法的类的实例。静态方法不能使用this关键字,因为静态方法不属于类的实 例,所以this也就没有什么东西去指向。构造器的this指向同一个类中,不同参数列表的另外一个构造器,我们看看下面的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | package com.dr.gouzao; public class Platypus { String name; Platypus(String input) { name = input; } Platypus() { this ( "John/Mary Doe" ); } public static void main(String args[]) { Platypus p1 = new Platypus( "digger" ); Platypus p2 = new Platypus(); System.out.println(p1.name + "----" + p2.name); } } |
在上面的代码中,有2个不同参数列表的构造器。第一个构造器,给类的成员name赋值,第二个构造器,调用第一个构造器,给成员变量name一个初始值 "John/Mary Doe".
在构造器中,如果要使用关键字this,那么,必须放在第一行,如果不这样,将导致一个编译错误。
在一个构造方法中只能调用一次其它的构造方法,并且调用构造方法的语句必须是第一条语句。
"super"的用法
构造器和方法,都用关键字super指向超类,但是用的方法不一样。方法用这个关键字去执行被重载的超类中的方法。看下面的例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package com.dr.gouzao; class Mammal { void getBirthInfo() { System.out.println( "born alive." ); } } class Platypus1 extends Mammal { void getBirthInfo() { System.out.println( "hatch from eggs" ); System.out.print( "a mammal normally is " ); super .getBirthInfo(); } } |
在上面的例子中,使用super.getBirthInfo()去调用超类Mammal中被重载的方法。
构造器使用super去调用超类中的构造器。而且这行代码必须放在第一行,否则编译将出错。看下面的例子:
1 2 3 4 5 6 7 8 9 10 | public class SuperClassDemo { SuperClassDemo() { } } class Child extends SuperClassDemo { Child() { super (); } } |
在上面这个没有什么实际意义的例子中,构造器 Child()包含了 super,它的作用就是将超类中的构造器SuperClassDemo实例化,并加到 Child类中。
编译器自动加入代码 ,当我们写一个没有构造器的类,编译的时候,编译器会自动加上一个不带参数的构造器。
现在具体介绍一下构造方法的几种用法:
类的继承机制使得子类可以使用父类的功能(即代码),并且子类也具有父类的类型。下面介绍类在继承关系上的初始化的顺序问题。
示例1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class SuperClass { SuperClass() { System.out.println( "SuperClass constructor" ); } } public class SubClass extends SuperClass { SubClass() { System.out.println( "SubClass constructor" ); } public static void main(String[] args) { SubClass sub = new SubClass(); } } |
输出结果: SuperClass constructor
SubClass constructor
在子类中只实例化了一个子类对象。从输出结果上看,程序并不是一开始就运行自己的构造方法,而是先运行其父类的默认构造方法。注意:程序自动调用其父类的默认构造方法。
实例2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class SuperClass { SuperClass(String str) { System.out.println( "Super with a string." ); } } public class SubClass extends SuperClass { SubClass(String str) { System.out.println( "Sub with a string." ); } public static void main(String[] args) { SubClass sub = new SubClass( "sub" ); } } |
在JDK下编译此程序不能成功。正如上例中说的:程序在初始化子类时先要寻找其父类的默认构造方法,结果没找到,那么编译自然不能通过。
解决这个问题有两个办法:
1.在父类中增加一个默认构造方法。
2.在子类的构造方法中增加一条语句:super(str); 且必须在第一句。
这两种方法都能使此程序通过编译,但就本程序来说运行结果却不相同。
第1种方法的运行结果是:
Sub with a string.
第2种方法的运行结果是:
Super with a string.
Sub with a string.
第2种解决方法实际上是指定编译器不要寻找父类的默认构造方法,而是去寻找带一个字符串为参数的构造方法。
下面介绍对象的初始化顺序问题。
示例3:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | class One { One(String str) { System.out.println(str); } } class Two { One one_1 = new One( "one-1" ); One one_2 = new One( "one-2" ); One one_3 = new One( "one-3" ); Two(String str) { System.out.println(str); } } public class Test { public static void main(String[] args) { System.out.println( "Test main() start" ); Two two = new Two( "two" ); } } |
输出结果:
Test main() start...
one-1
one-2
one-3
two
在main()方法中实例化了一个Two类的对象。但程序在初始化Two类的对象时,并非先调用Two类的构造方法,而是先初始化Two类的成员变量。这里Two类有3个成员变量,它们都是One类的对象,所以要先调用3次One类的相应的构造方法。最后在初始化Two类的对象。
即在创建对象时,对象所在类的所有数据成员会首先进行初始化,如果其中的成员变量有对象,那么它们也会按照顺序执行初始化工作。在所有类成员初始化完成后,才调用对象所在类的构造方法创建对象。构造方法作用就是初始化。
示例4:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | class One { One(String str) { System.out.println(str); } } class Two { One one_1 = new One( "one-1" ); One one_2 = new One( "one-2" ); static One one_3 = new One( "one-3" ); Two(String str) { System.out.println(str); } } public class Test { public static void main(String[] args) { System.out.println( "Test main() start" ); Two two_1 = new Two( "two-1" ); System.out.println( "------------" ); Two two_2 = new Two( "two-2" ); } } |
输出结果:
Test main() start...
one-3
one-1
one-2
two-1
------------
one-1
one-2
two-2
如果一个类中有静态对象,那么它会在非静态对象前初始化,但只初始化一次。非静态对象每次调用时都要初始化。
实例5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | class One { One(String str) { System.out.println(str); } } class Two { One one_1 = new One( "one-1" ); One one_2 = new One( "one-2" ); static One one_3 = new One( "one-3" ); Two(String str) { System.out.println(str); } 3 } public class Test { static Two two_3 = new Two( "two-3" ); public static void main(String[] args) { System.out.println( "Test main() start" ); Two two_1 = new Two( "two-1" ); System.out.println( "------------" ); Two two_2 = new Two( "two-2" ); } } |
输出结果:
one-3
one-1
one-2
two-3
Test main() start...
one-1
one-2
two-1
------------
one-1
one-2
two-2
程序中主类的静态变量会在main()方法执行前初始化。结果中只输出了一次one-3,这也说明:如果一个类中有静态对象,那么它会在非静态对象前初始化,但只初始化一次。非静态对象每次调用时都要初始化。
实例6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | class One { One(String str) { System.out.println(str); } } class Two { static int i = 0 ; One one_1 = new One( "one-1" ); static One one_2 = new One( "one-2" ); static One one_3 = new One( "one-3" ); Two(String str) { System.out.println(str); } } public class Test { public static void main(String[] args) { System.out.println( "Test main() start" ); System.out.println( "Two.i = " Two.i); } } |
输出结果:
Test main() start...
one-2
one-3
Two.i = 0
不仅第1次创建对象时,类中所有的静态变量要初始化,第1次访问类中的静态变量(没有创建对象)时,该类中所有的静态变量也要按照它们在类中排列的顺序初始化。
初始化的顺序包括构造方法调用的顺序如下:
1.主类的静态成员首先初始化。
2.主类的超类的构造方法按照从最高到最低的顺序被调用。
3.主类的非静态对象(变量)初始化。
4.调用主类的构造方法。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· .NET 进程 stackoverflow异常后,还可以接收 TCP 连接请求吗?
· 本地部署 DeepSeek:小白也能轻松搞定!
· 基于DeepSeek R1 满血版大模型的个人知识库,回答都源自对你专属文件的深度学习。
· 在缓慢中沉淀,在挑战中重生!2024个人总结!
· 大人,时代变了! 赶快把自有业务的本地AI“模型”训练起来!
· Tinyfox 简易教程-1:Hello World!