位置:武汉粤嵌嵌入式IT培训机构 > 学校动态 > Java实例方法
粤嵌教育小编告诉大家,实例方法是执行特定任务的一组代码。有时程序会变大,我们希望将 main 方法的逻辑与其他方法分开。方法是写在Java内部类的函数。由于java是一种面向对象的编程语言,我们需要在一些类中编写方法。
关于实例变量的要点是:
实例方法可以直接且不变地访问实例变量和实例方法。
实例方法可以直接访问静态变量和静态方法。
句法:
修饰符 return_type method_name( ) {
方法体;
}
修饰符:定义方法的访问类型,可选用。
return_type:方法可能会返回一个值。例如:- int、void、String、char、float 等。
method_name:这是方法名称,您可以在编写变量名称时编写任何内容。
方法体:方法体描述了方法对语句的作用。
例子:
公共无效显示()
{
整数a = 10;
System.out.println(a);
}
静态方法中不能直接调用实例方法,所以可以使用类的对象调用实例方法。我们知道java程序的执行是从main方法开始的,而main方法是静态的,所以不能直接调用实例方法。我们必须创建类对象;然后,我们可以在main方法中调用实例方法。
让我们看看如何调用 Instance 方法:
示例 1:
// Java program to see how can we call // an instance method without parameter import java.io.*; class GFG { // static method public static void main (String[] args) { // Creating object of the class GFG obj = new GFG(); // Calling instance method obj.disp();
System.out.println("GFG!");
} // Instance method void disp() { // Local variable int a = 20;
System.out.println(a);
}
}
输出
20
GFG!
示例 2:
// Java program to see how can we call // an instance method without parameter import java.io.*; // Different class class class1 { // Instance method in different class void add() { int a= 2; int b= 3;
System.out.println("The sum of 2 and 3 is :" + (a+b));
}
} class GFG { // Static method public static void main (String[] args) { // creating object of the class class1 obj = new class1(); // calling instance method obj.add();
System.out.println("GFG!");
}
}
输出
2和3之和为:5
GFG!
带参数的实例方法在主方法中调用时采用参数。现在让我们看看示例以更好地理解。
句法:
修饰符 return_type 方法名(参数列表)
{
方法体;
}
参数列表:以逗号分隔的参数列表。这些是可选的;该方法可能包含零个参数。
例子:
公共无效显示(int a,int b)
{
诠释 x=a ;
整数y=b; int z = x+y;
System.out.println(z);
}
// Java program to see how can we call // an instance method with parameter import java.io.*; class GFG { // static method public static void main (String[] args) { // creating object GFG obj = new GFG(); // calling instance method by passing value obj.add(2,3);
System.out.println("GFG!");
} // Instance method with parameter void add(int a, int b) { // local variables int x= a; int y= b; int z= x + y;
System.out.println("Sum : " + z);
}
}
输出
总和:5
GFG!
Java中有两种实例方法:
访问器方法 (Getters)
突变器方法 (设置器)
访问器方法用于使代码更安全并提高其保护级别,访问器也称为getter。getter返回值(访问器),它返回int、String、double、float等数据类型的值。为方便程序,getter以“get”开头,后面跟着变量名。
mutator方法也称为setter。它为类程序中使用的任何变量设置值。并以单词“set”开头,后跟变量名。Getter 和 Setter 使程序员可以方便地设置和获取特定Java数据类型的值。在 getter 和 setter 中,变量的个字母应该是大写的。
Accessor 和 mutator 主要用于在 main 方法中访问或设置类的私有成员的值。
让我们通过一些例子来理解:
// Java program to demonstrate the // types of instance methods import java.io.*; class account { // private variable-balance private int balance = 50; // accessor method (getter) public int getBalance() { return balance;
} // Mutator method (setter) public void setBalance(int a) { // return balance + a; balance += a;
}
} class GFG { public static void main(String[] args) {
account obj = new account(); // setting new value for balance obj.setBalance(50); // calling the Mutator (accessor) System.out.println("Your Balance : "+ obj.getBalance());
System.out.println("GFG!");
}
}
输出
您的余额:100
GFG!
尊重原创文章,转载请注明出处与链接:http://www.peixun360.com/5630/news/519494/违者必究! 以上就是武汉粤嵌嵌入式IT培训机构 小编为您整理 Java实例方法的全部内容。