public: 当我们在应用程序中使用公共说明符时,所有类都可以访问该方法。
private: 当我们使用私有访问说明符时,仅在定义该方法的类中可以访问该方法。
protect: 当我们使用受保护的访问说明符时,该方法可在相同包中或在不同包中的子类中访问。
default: 当我们在方法声明中不使用任何访问说明符时,Java默认使用默认的访问说明符。仅从同一程序包中可见。
预定义方法
用户定义的方法
public class Demo
{
public static void main(String[] args)
{
// using the max() method of Math class
System.out.print("The maximum number is: " + Math.max(9,7));
}
}
The maximum number is: 9
//user defined method
public static void findEvenOdd(int num)
{
//method body
if(num%2==0)
System.out.println(num+" is even");
else
System.out.println(num+" is odd");
}
import java.util.Scanner;
public class EvenOdd
{
public static void main (String args[])
{
//creating Scanner class object
Scanner scan=new Scanner(System.in);
System.out.print("Enter the number: ");
//reading value from the user
int num=scan.nextInt();
//method calling
findEvenOdd(num);
}
import java.util.Scanner;
public class EvenOdd
{
public static void main (String args[])
{
//creating Scanner class object
Scanner scan=new Scanner(System.in);
System.out.print("Enter the number: ");
//reading value from user
int num=scan.nextInt();
//method calling
findEvenOdd(num);
}
//user defined method
public static void findEvenOdd(int num)
{
//method body
if(num%2==0)
System.out.println(num+" is even");
else
System.out.println(num+" is odd");
}
}
Enter the number: 12
12 is even
Enter the number: 99
99 is odd
public class Addition
{
public static void main(String[] args)
{
int a = 19;
int b = 5;
//method calling
int c = add(a, b);
//a and b are actual parameters
System.out.println("The sum of a and b is= " + c);
}
//user defined method
public static int add(int n1, int n2)
//n1 and n2 are formal parameters
{
int s;
s=n1+n2;
return s;
//returning the sum
}
}
The sum of a and b is= 24
public class Display
{
public static void main(String[] args)
{
show();
}
static void show()
{
System.out.println("It is an example of static method.");
}
}
It is an example of a static method.
public class InstanceMethodExample
{
public static void main(String [] args)
{
//Creating an object of the class
InstanceMethodExample obj = new InstanceMethodExample();
//invoking instance method
System.out.println("The sum is: "+obj.add(12, 13));
}
int s;
//user-defined method because we have not used static keyword
public int add(int a, int b)
{
s = a+b;
//returning the sum
return s;
}
}
The sum is: 25
访问器(accessor)方法:
修改器(mutator)方法:
public int getId()
{
return Id;
}
public void setRoll(int roll)
{
this.roll = roll;
}
public class Student
{
private int roll;
private String name;
public int getRoll()
//accessor method
{
return roll;
}
public void setRoll(int roll)
//mutator method
{
this.roll = roll;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public void display()
{
System.out.println("Roll no.: "+roll);
System.out.println("Student name: "+name);
}
}
abstract void method_name();
abstract class Demo
//abstract class
{
//abstract method declaration
abstract void display();
}
public class Myclass extends Demo
{
//method impelmentation
void display()
{
System.out.println("Abstract method?");
}
public static void main(String args[])
{
//creating object of abstract class
Demo obj = new MyClass();
//invoking abstract method
obj.display();
}
}
Abstract method...