【发布时间】:2013-10-04 09:22:43
【问题描述】:
我刚刚开始使用 Java 进行编程。在谈论方法和构造函数时,我们使用的文本是缺失的。我不确定一个方法或构造函数究竟是什么,以及是什么让它们变得独一无二。有人可以帮我定义它们并区分两者吗?
【问题讨论】:
-
@JeroenVannevel 是的,这些确实很有帮助,而且是一个很好的资源!!
标签: java methods constructor
我刚刚开始使用 Java 进行编程。在谈论方法和构造函数时,我们使用的文本是缺失的。我不确定一个方法或构造函数究竟是什么,以及是什么让它们变得独一无二。有人可以帮我定义它们并区分两者吗?
【问题讨论】:
标签: java methods constructor
构造函数和方法之间的重要区别在于构造函数初始化使用new 运算符创建的对象,而方法对已经存在的对象执行操作。
构造函数不能直接调用;当new 关键字创建一个对象时,它们会被隐式调用。可以直接在已使用new 创建的对象上调用方法。
构造函数和方法的定义在代码中看起来很相似。它们可以带参数,可以有修饰符(例如public),并且它们有大括号中的方法体。
构造函数的名称必须与类名相同。他们不能返回任何东西,即使是void(对象本身就是隐式返回)。
方法必须声明返回一些东西,虽然它可以是void。
【讨论】:
new关键字调用构造函数并创建对象。在构造函数内部,它可以通过 this() 或 super() 语法调用不同的或超类构造函数,有时带有参数。
主要区别是
1.构造函数用于初始化对象的状态,其中方法是暴露对象的行为。
2.构造函数不能有返回类型,而方法必须有返回类型。
3.构造函数名称与类名相同,其中方法可能与类名相同,也可能不同。
4.构造函数隐式调用,而方法显式调用。
5.构造函数编译器提供默认构造函数,而方法编译器不提供。
【讨论】:
其他导师和助教偶尔会告诉我,构造函数是专门的方法。我一直认为在 Java 中构造函数不是专门的方法。
如果构造函数完全是方法,我希望它们具有与方法相同的能力。至少它们的相似之处多于不同之处。
必须使用new 运算符调用构造函数,而不能使用new 运算符调用方法。相关:不能按名称调用构造函数,而必须按名称调用方法。
构造函数可能没有返回类型,而方法必须有返回类型。
如果方法与类同名,则它必须具有返回类型。否则,它是一个构造函数。您可以在同一个类定义中有两个 MyClass() 签名,它们的处理方式不同,这一事实应该让所有人相信构造函数和方法是不同的实体:
public class MyClass {
public MyClass() { } // constructor
public String MyClass() { return "MyClass() method"; } // method
}
构造函数可以初始化实例常量,而方法不能。
不继承公共和受保护的构造函数,而继承公共和受保护的方法。
构造函数可以调用超类或同一个类的构造函数,而方法不能调用 super() 或 this()。
它们都有参数列表。
它们都有代码块,当该块被直接调用(方法)或通过new(构造函数)调用时,这些代码块将被执行。
构造函数可以是:private、protected、public。
方法可能是:private、protected、public、abstract、static 、final、同步、native、strictfp。
数据字段可能是:private、protected、public、static、最终,瞬态,易失性。
在 Java 中,构造函数的形式和功能与方法有很大不同。因此,称它们为专门的方法实际上使新程序员更难了解这些差异。它们比相似的要不同得多,并且将它们作为不同的实体来学习在 Java 中是至关重要的。
我确实认识到 Java 在这方面不同于其他语言,即 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);
}
}
希望我在代码的 cmets 中解释了所有内容,但这里是一个摘要: 构造函数“构造”类类型的对象。构造函数的名称必须与类相同。它们主要用于初始化实例变量 方法是对象的功能。
【讨论】:
构造函数是一种特殊的方法,它允许您创建类的新实例。它与初始化逻辑有关。
【讨论】:
“方法”是“子程序”是“过程”是“函数”是“子程序”是...相同的概念有许多不同的名称,但基本上是一个命名的代码段您可以从其他代码“调用”。通常,代码以某种方式整齐地打包,带有某种“标题”,它给出了它的名称和参数,以及由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 显式调用,而方法隐式调用。【讨论】:
Constructor cannot inherited by child classes 与方法的区别非常重要。
Constructor in type..; only public, protected & private are permitted,因此它们不能是抽象的、最终的、原生的、静态的或同步的。
构造函数是用于初始化数据成员的特殊函数,其中方法是执行特定任务的函数。
构造函数名与类名同名,其中方法名可能是也可能不是类名。
构造函数不允许任何返回类型,其中方法允许返回类型。
【讨论】:
主要区别如下 -
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");
}
}
【讨论】:
构造函数通常是方法。
当我们使用 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();
}
}
`
输出:
Constructor:Contructor 中的局部变量 A 打印我
所以,这里只显示构造方法 Diff() 语句,因为我们创建了 Diff 类对象。在这种情况下,构造函数总是首先在此处实例化 Class Diff()。
通常,
构造函数已设置功能。
一切都从这里开始,当我们在主方法构造函数中调用 ob 对象时,会获取这个类并创建副本并将其加载到“Java 虚拟机类加载器”中。
这个类加载器获取这个副本并加载到内存中,所以我们现在可以通过引用来使用它。
构造器完成它的工作,然后方法来完成它的真正实现。
在这个程序中我们调用时
ob.print();
然后方法会来。
谢谢
阿林丹
【讨论】:
以下是java中构造函数和方法之间的一些主要区别
【讨论】: