java中的方法_Java中的方法
java中的方法
Java中的方法 (Methods in Java)
Method in Java is similar to a function defined in other programming languages. Method describes behavior of an object. A method is a collection of statements that are grouped together to perform an operation.
Java中的方法类似于其他编程语言中定义的功能。 方法描述对象的行为 。 方法是语句的集合,这些语句被组合在一起以执行操作。
For example, if we have a class Human, then this class should have methods like eating(), walking(), talking() etc, which describes the behavior of the object.
例如,如果我们有一个Human类,则该类应具有诸如eating(),walking(),talking()等方法,这些方法描述了对象的行为。
Declaring method is similar to function. See the syntax to declare the method in Java.
声明方法类似于功能。 请参见在Java中声明该方法的语法。
return-type methodName(parameter-list)
{
//body of method
}
return-type refers to the type of value returned by the method.
return-type是指方法返回的值的类型。
methodName is a valid meaningful name that represent name of a method.
methodName是表示方法名称的有效有意义的名称。
parameter-list represents list of parameters accepted by this method.
parameter-list表示此方法接受的参数列表。
Method may have an optional return statement that is used to return value to the caller function.
方法可能具有可选的return语句,该语句用于将值返回给调用方函数。
方法示例: (Example of a Method:)
Lets understand the method by simple example that takes a parameter and returns a string value.
让我们通过一个简单的示例了解该方法,该示例采用一个参数并返回一个字符串值。
public String getName(String st)
{
String name="StudyTonight";
name=name+st;
return name;
}
Modifier : Modifier are access type of method. We will discuss it in detail later.
修饰符:修饰符是方法的访问类型。 我们将在后面详细讨论。
Return Type : A method may return value. Data type of value return by a method is declare in method heading.
返回类型:方法可以返回值。 方法标题中声明方法返回值的数据类型。
Method name : Actual name of the method.
方法名称:方法的实际名称。
Parameter : Value passed to a method.
参数:传递给方法的值。
Method body : collection of statement that defines what method does.
方法主体:定义了什么方法的语句的集合。
调用方法 (Calling a Method)
Methods are called to perform the functionality implemented in it. We can call method by its name and store the returned value into a variable.
调用方法以执行其中实现的功能。 我们可以通过其名称来调用method并将返回的值存储到变量中。
String val = GetName(".com")
It will return a value studytonight.com after appending the argument passed during method call.
在追加方法调用期间传递的参数后,它将返回一个值studytonight.com 。
返回多个值 (Returning Multiple values)
In Java, we can return multiple values from a method by using array. We store all the values into an array that want to return and then return back it to the caller method. We must specify return-type as an array while creating an array. Lets see an example.
在Java中,我们可以使用数组从一个方法返回多个值。 我们将所有值存储到要返回的数组中,然后将其返回给调用方方法。 创建数组时, 必须将return-type指定为数组。 让我们来看一个例子。
Example:
例:
Below is an example in which we return an array that holds multiple values.
下面是一个示例,其中我们返回一个包含多个值的数组。
class MethodDemo2{
static int[] total(int a, int b)
{
int[] s = new int[2];
s[0] = a + b;
s[1] = a - b;
return s;
}
public static void main(String[] args)
{
int[] s = total(200, 70);
System.out.println("Addition = " + s[0]);
System.out.println("Subtraction = " + s[1]);
}
}
从方法返回对象 (Return Object from Method)
In some scenario there can be need to return object of a class to the caller function. In this case, we must specify class name in the method definition.
在某些情况下,可能需要将类的对象返回给调用者函数。 在这种情况下,我们必须在方法定义中指定类名 。
Below is an example in which we are getting an object from the method call. It can also be used to return collection of data.
下面是一个示例,其中我们从方法调用中获取一个对象。 它还可以用于返回数据收集。
Example:
例:
In this example, we created a method get() that returns object of Demo class.
在此示例中,我们创建了一个get()方法,该方法返回Demo类的对象。
class Demo{
int a;
double b;
int c;
Demo(int m, double d, int a)
{
a = m;
b = d;
c = a;
}
}
class MethodDemo4{
static Demo get(int x, int y)
{
return new Demo(x * y, (double)x / y, (x + y));
}
public static void main(String[] args)
{
Demo ans = get(25, 5);
System.out.println("Multiplication = " + ans.a);
System.out.println("Division = " + ans.b);
System.out.println("Addition = " + ans.c);
}
}
参数Vs。 方法中的参数 (Parameter Vs. Argument in a Method)
While talking about method, it is important to know the difference between two terms parameter and argument.
在讨论方法时,重要的是要了解两个术语参数和参数之间的区别。
Parameter is variable defined by a method that receives value when the method is called. Parameter are always local to the method they dont have scope outside the method. While argument is a value that is passed to a method when it is called.
参数是由方法定义的变量,该方法在调用该方法时会接收值。 参数始终是方法的局部变量,它们在方法外部没有作用域。 While 参数是在调用时传递给方法的值。
You can understand it by the below image that explain parameter and argument using a program example.
您可以通过下图使用程序示例解释参数和自变量来理解它。
call-by-value
call-by-reference
(call-by-value
and call-by-reference
)
There are two ways to pass an argument to a method
有两种方法可以将参数传递给方法
- call-by-value : In this approach copy of an argument value is pass to a method. Changes made to the argument value inside the method will have no effect on the arguments.按值调用:在这种方法中,将参数值的副本传递给方法。 对方法内部的参数值所做的更改将不会对参数产生影响。
- call-by-reference : In this reference of an argument is pass to a method. Any changes made inside the method will affect the agrument value.call-by-reference:在此参数引用中传递给方法。 方法内部进行的任何更改都将影响汇总值。
NOTE :However there is no concept of call-by-reference in Java. Java supports only call by value.
注意:但是,在Java中没有按引用调用的概念。 Java仅支持按值调用。
call-by-value
示例 (Example of call-by-value
)
Lets see an example in which we are passing argument to a method and modifying its value.
让我们看一个例子,其中我们将参数传递给方法并修改其值。
public class Test
{
public void callByValue(int x)
{
x=100;
}
public static void main(String[] args)
{
int x=50;
Test t = new Test();
t.callByValue(x);//function call
System.out.println(x);
}
}
50
50
See, in the above example, value passed to the method does not change even after modified in the method. It shows that changes made to the value was local and argument was passed as call-by-value.
请参见上面的示例,即使在方法中进行了修改, 传递给该方法的值也不会更改 。 它表明对值所做的更改是局部的,并且参数作为按值调用传递。
java中的方法
上一篇: eBay 出价 1.69 亿美元收购 Fraud Sciences
下一篇: JAVA中的方法