Java中的构造函数和方法之间的区别

您所在的位置:网站首页 java函数与方法的区别 Java中的构造函数和方法之间的区别

Java中的构造函数和方法之间的区别

2024-07-13 16:50| 来源: 网络整理| 查看: 265

Java方法 (Java methods)

A method is used to explore the behavior of an object.

一种方法用于探索对象的行为。

We can prefix access modifiers with methods.

我们可以在方法的前面加上访问修饰符。

A method must have a return type like a void, any primitives type (int, char, float, etc.), any Object type(Integer, Float, String, etc).

方法必须具有返回类型,例如void,任何原始类型(int,char,float等),任何Object类型(Integer,Float,String等)。

Java compiler does not call method implicitly we need to call explicitly.

Java编译器不会隐式调用方法,我们需要显式调用。

Let suppose if we have a class and in that class, we don't have any single method so in that case java compiler does not include any method from its own by default.

让我们假设如果我们有一个类,并且在该类中,我们没有任何单个方法,那么在这种情况下,java编译器默认不包含其自身的任何方法。

It is not mandated to have the same name of methods as its class name or in other words, method name may or may not be same as of class name.

它不是必须具有与其类名称相同的方法名称,或者换句话说,方法名称可以与或不与类名称相同。

Methods are of two types:

方法有两种:

No argument methodsParameterized methods

i) No argument method

i)没有参数方法

A method that has no parameter or argument or in other words this type of method does not take any argument is known as the no-argument method.

没有参数或参数的方法,换句话说,这种类型的方法不带任何参数的方法称为无参数方法。

Example:

例:

class NoArgumentMethod { // No argument method definition void display() { System.out.println("We are in no argument method"); } } class Main { public static void main(String[] args) { // creating an object of NoArgumentMethod class NoArgumentMethod nam = new NoArgumentMethod(); // calling display() method nam.display(); } }

Output:

输出:

E:\Programs>javac Main.java E:\Programs>java Main We are in no argument method

ii) Parameterized method

ii)参数化方法

A method that has parameters or in other words this type of method takes any argument is known as a parameterized method.

具有参数的方法,换句话说,这种类型的方法接受任何参数,称为参数化方法。

Example:

例:

class ParameterizedMethod { String name; // Parameterized method definition void display(String name) { System.out.println("The name is " + name); } } class Main { public static void main(String[] args) { // creating an object of ParameterizedMethod class ParameterizedMethod pm = new ParameterizedMethod(); // calling display() method pm.display("Rahul"); } }

Output

输出量

E:\Programs>javac Main.java E:\Programs>java Main The name is Rahul Java构造函数 (Java constructors)

A constructor is a special method which calls implicitly by JVM(Java Virtual Machine) at the time of object creation.

构造函数是一种特殊的方法,它在创建对象时由JVM(Java虚拟机)隐式调用。

The purpose of a constructor is to initialize an instance variable of the object.

构造函数的目的是初始化对象的实例变量。

We can prefix access specifiers with constructor also.

我们也可以在访问说明符前面加上构造函数。

A constructor must not have a return type i.e. it does not return anything.

构造函数不能具有返回类型,即它不返回任何东西。

A constructor is a special function which calls implicitly(i.e. it is good for the user that we don't need to call explicitly).

构造函数是一个隐式调用的特殊函数(即,对于我们不需要显式调用的用户而言是有益的)。

Let suppose we have a class named "Constructor" and in that class, we don't include any constructor so in that case java compiler will include a default constructor from its own.

假设我们有一个名为“ Constructor”的类,并且在该类中,我们不包含任何构造函数,因此在这种情况下,java编译器将包含一个自己的默认构造函数。

It is mandated to have the same name as the constructor as its class name or in other words, constructor name must be same as the class name.

它的名称必须与构造函数的类名称相同,或者换句话说,构造函数名称必须与类名称相同。

We have three types of constructors:

我们有三种类型的构造函数:

Default ConstructorParameterized ConstructorCopy Constructor

i) Default constructor

i)默认构造函数

This default constructor calls implicitly by the compiler if we don't include any constructor in the class. Default constructor has no parameter or in other words no-argument constructor.

如果我们在类中不包含任何构造函数,则此默认构造函数将由编译器隐式调用。 默认构造函数没有参数,或者说没有参数构造函数。

Example:

例:

class DefaultConstructor { // Constructor defined DefaultConstructor() { System.out.println("We are in default constructor"); } } class Main { public static void main(String[] args) { // Calling Default constructor DefaultConstructor dc = new DefaultConstructor(); } }

Output

输出量

E:\Programs>javac Main.java E:\Programs>java Main We are in default constructor

ii) Parameterized constructor

ii)参数化构造函数

The constructor that has parameter is known as parameterized constructor. If we want to initialize instance variable with our values and this constructor does not call by compiler implicitly.

具有参数的构造函数称为参数化构造函数。 如果我们想用我们的值初始化实例变量,而该构造函数不会被编译器隐式调用。

Example:

例:

class ParameterizedConstructor { String name; // Constructor defined ParameterizedConstructor(String name) { this.name = name; } } class Main { public static void main(String[] args) { // Calling Parameterized constructor ParameterizedConstructor pc = new ParameterizedConstructor("Preeti"); System.out.println("The name is :" + pc.name); } }

Output

输出量

E:\Programs>javac Main.java E:\Programs>java Main The name is : Preeti

iii) Copy constructor

iii)复制构造函数

A constructor that has one parameter and the parameter is the reference of the same class.

具有一个参数且该参数是同一类的引用的构造函数。

Example:

例:

class CopyConstructor { String name; // Constructor defined CopyConstructor(String name) { this.name = name; } } class Main { public static void main(String[] args) { // Calling constructor CopyConstructor cc1 = new CopyConstructor("Preeti"); // Copied cc1 constructor CopyConstructor cc2 = cc1; System.out.println("The value of cc1 is :" + cc1.name); System.out.println("The value of cc2 is :" + cc2.name); } }

Output

输出量

E:\Programs>javac Main.java E:\Programs>java Main The value of cc1 is :Preeti The value of cc2 is :Preeti

翻译自: https://www.includehelp.com/java/differences-between-constructors-and-methods-in-java.aspx



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3