[英]Methods vs Constructors in Java
我刚刚开始使用Java进行编程。 在谈论方法和构造函数时,我们缺少使用的文字。 我不确定到底是什么方法或构造函数以及什么使每个方法唯一。 有人可以帮我定义它们并区分两者吗?
构造函数与方法之间的重要区别在于,构造函数初始化使用new
运算符创建的对象,而方法对已存在的对象执行操作。
构造函数不能直接调用; 当new
关键字创建对象时,将隐式调用它们。 可以直接在已使用new
创建的对象上调用方法。
构造函数和方法的定义在代码中看起来相似。 它们可以采用参数,可以具有修饰符(例如public
),并且在括号中具有方法主体。
构造函数的名称必须与类名称相同。 他们不能返回任何东西,甚至void
(对象本身是隐含的回报)。
尽管必须为void
,但必须声明方法以返回某些内容。
主要区别是
1. 构造函数用于初始化对象的状态,其中方法公开对象的行为。
2. 构造函数不得具有返回类型,而as 方法必须具有返回类型。
3. 构造函数名称与类名称相同,其中as 方法可能相同,也可能不同。
4. 构造函数隐式调用,而方法显式调用。
5. 构造函数编译器提供默认的构造函数,而方法编译器则不提供。
其他讲师和助教有时会告诉我构造函数是专门的方法。 我一直认为Java构造函数不是专门的方法 。
如果构造函数完全是方法,我希望它们具有与方法相同的功能。 至少它们之间的相似之处要多于不同之处。
构造函数必须使用new
运算符调用,而方法不能使用new
运算符调用。 相关:构造函数不能按名称调用,而方法必须按名称调用。
构造函数可能没有返回类型,而方法必须具有返回类型。
如果方法与类具有相同的名称,则它必须具有返回类型。 否则,它是一个构造函数。 在同一个类定义中可以有两个MyClass()签名,这些签名被不同地对待,这一事实应使所有确信构造函数和方法是不同的实体:
public class MyClass { public MyClass() { } // constructor public String MyClass() { return "MyClass() method"; } // method }
构造函数可以初始化实例常量,而方法则不能。
公共和受保护的构造函数不被继承,而公共和受保护的方法则被继承。
构造函数可以调用父类或同一个类的构造函数,而方法不能调用super()或this()。
它们都有参数列表。
它们都具有代码块,当该代码块被直接调用(方法)或通过new
调用(构造函数)时将执行该代码块。
构造函数可以是: 私有的 , 受保护的 , 公共的 。
方法可以是: 私有 , 受保护 , 公共 , 抽象 , 静态 , 最终 , 同步 , 本机 , strictfp 。
数据字段可以是: 私有 , 受保护 , 公共 , 静态 , 最终 , 瞬态 , 易失 。
在Java中,构造函数的形式和功能与方法明显不同。 因此,将它们称为专用方法实际上会使新程序员更难学习差异。 它们比相似的东西有更多不同,学习不同的实体对于Java至关重要。
我确实认识到Java在这方面与其他语言(即C ++)不同,在C ++中,专用方法的概念起源于语言规则并受其支持。 但是,在Java中,构造函数根本不是方法,更不用说专门的方法了。
甚至javadoc都认识到构造函数和方法之间的差异远大于相似之处。 并为构造函数提供了单独的部分。
在Java中,您编写的类是对象。 构造函数构造这些对象。 例如,如果我有一个像这样的Apple.class
:
public class Apple {
//instance variables
String type; // macintosh, green, red, ...
/**
* This is the default constructor that gets called when you use
* Apple a = new Apple(); which creates an Apple object named a.
*/
public Apple() {
// in here you initialize instance variables, and sometimes but rarely
// do other functionality (at least with basic objects)
this.type = "macintosh"; // the 'this' keyword refers to 'this' object. so this.type refers to Apple's 'type' instance variable.
}
/**
* this is another constructor with a parameter. You can have more than one
* constructor as long as they have different parameters. It creates an Apple
* object when called using Apple a = new Apple("someAppleType");
*/
public Apple(String t) {
// when the constructor is called (i.e new Apple() ) this code is executed
this.type = t;
}
/**
* methods in a class are functions. They are whatever functionality needed
* for the object
*/
public String someAppleRelatedMethod(){
return "hello, Apple class!";
}
public static void main(String[] args) {
// construct an apple
Apple a = new Apple("green");
// 'a' is now an Apple object and has all the methods and
// variables of the Apple class.
// To use a method from 'a':
String temp = a.someAppleRelatedMethod();
System.out.println(temp);
System.out.println("a's type is " + a.type);
}
}
希望我在代码的注释中解释了所有内容,但这是一个摘要:构造函数“构造”一个类类型的对象。 构造函数的名称必须与类相同。 它们主要用于初始化实例变量。方法是对象的功能。
构造函数是一种特殊的方法,它允许您创建类的新实例。 它涉及初始化逻辑。
“方法”是“子例程”是“过程”是“函数”是“子程序”是...同样的概念使用了许多不同的名称,但基本上是可以命名的代码段,调用”。 通常,代码以某种方式整齐地打包,带有某种“标头”,给出其名称和参数,以及一个“正文”,以BEGIN
& END
或{
& }
等开头。
“构造器”是一种特殊形式的方法,其目的是初始化类或结构的实例。
在Java中,方法的标头是<qualifiers> <return type> <method name> ( <parameter type 1> <parameter name 1>, <parameter type 2> <parameter name 2>, ...) <exceptions>
和一个方法主体用{}
括起来。
而且您可以从其他方法中得知构造函数,因为该构造函数具有其<method name>
的类名称,并且没有声明的<return type>
。
(当然,在Java中,您可以使用new
运算符new <class name> ( <parameter list> )
创建一个新的类实例。)
差r
:
r
隐式调用while方法。 构造函数是用于初始化数据成员的特殊函数,其中方法是执行特定任务的函数。
构造函数名称与类名称相同,方法名称可以相同也可以不相同,也可以为类名称。
构造函数不允许任何返回类型,其中方法允许返回类型。
构造函数通常是Method 。
当我们创建使用new运算符的类的对象时,我们将调用一种称为构造函数的特殊方法。
构造函数,用于执行实例变量的初始化。
public class Diff{
public Diff() { //same as class name so constructor
String A = "Local variable A in Constructor:";
System.out.println(A+ "Contructor Print me");
}
public void Print(){
String B = "Local variable B in Method";
System.out.println(B+ "Method print me");
}
public static void main(String args[]){
Diff ob = new Diff();
}
}
`
输出:
构造函数:构造函数中的局部变量A
因此,仅在此处显示构造方法Diff()语句,因为我们创建了Diff类对象。 在这种情况下,构造函数总是最先在此处实例化Class Diff()的Class。
通常,
构造函数设置功能。
一切都从这里开始,当我们在main方法构造函数中调用ob对象时,将使用该类并创建副本,并将其加载到“ Java虚拟机类加载器”中。
这个类加载器将这个副本加载到内存中,因此我们现在可以通过引用来使用它。
构造函数完成其工作,然后Method并完成其实际实现。
在这个程序中,当我们调用
ob.print();
然后方法就会来。
谢谢
阿林丹
主要区别如下:
1:构造函数必须与类名具有相同的名称,而方法不是这种情况
class Calendar{
int year = 0;
int month= 0;
//constructor
public Calendar(int year, int month){
this.year = year;
this.month = month;
System.out.println("Demo Constructor");
}
//Method
public void Display(){
System.out.println("Demo method");
}
}
2:构造函数初始化一个类的对象,而方法则不初始化。 方法对已经存在的对象执行操作。 换句话说,要调用一个方法,我们需要一个类的对象。
public class Program {
public static void main(String[] args) {
//constructor will be called on object creation
Calendar ins = new Calendar(25, 5);
//Methods will be called on object created
ins.Display();
}
}
3:构造函数没有返回类型,但是方法必须具有返回类型
class Calendar{
//constructor – no return type
public Calendar(int year, int month){
}
//Method have void return type
public void Display(){
System.out.println("Demo method");
}
}
这是Java中的构造函数和方法之间的一些主要关键区别
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.