📜  显示不同访问级别的Java程序

📅  最后修改于: 2022-05-13 01:55:31.678000             🧑  作者: Mango

显示不同访问级别的Java程序

程序中的访问级别可以通过访问修饰符的概念来引入。 Java程序中的访问修饰符用于指定字段、方法、构造函数甚至类的范围。因此,我们可以通过对其应用访问修饰符来更改字段、构造函数、方法和类的访问级别。有四种类型的访问修饰符,如下所示:

Access ModifierDescription
PublicThe access level of a public modifier is everywhere. It can be accessed from within the class, outside the class, within the package, and even outside the package.
PrivateThe access level of a private modifier is only within the class, the modifier is mentioned in. It cannot be accessed from outside that class.
ProtectedThe access level of a protected modifier is within the package (it is mentioned in) and outside the package through child class. If you do not make the child class, it cannot be accessed from outside the package.
DefaultThe access level of a default modifier is only within the package. It cannot be accessed from outside the package. If you do not specify any access level, it will be set to default.

实施:访问修饰符用法

示例 1:使用单个类来显示访问修饰符的范围

Java
// java Program to illustrate One Single Class for Scope of
// Access modifiers
  
// Importing input output classes
import java.io.*;
  
// Main class
class GFG {
  
    // Method 1
    public static void method1()
    {
  
        // Print statement for method with public type
        System.out.println(
            "This method uses Public access modifier");
    }
  
    // Method 2
    private static void method2()
    {
  
        // Print statement for method with private type
        System.out.println(
            "This method uses Private access modifier");
    }
  
    // Method 3
    protected static void method3()
    {
  
        // Print statement for method with protected type
        System.out.println(
            "This method uses Protected access modifier");
    }
  
    // Method 4
    static void method4()
    {
  
        // Print statement for static default type method
        System.out.println(
            "This method uses Default access modifier");
    }
  
    // Method 5
    // Main driver method
    public static void main(String[] args)
    {
  
        // Display message only
        System.out.println(
            "Various access modifiers being used in the same class");
  
        // Calling all the above methods in main() method
        // body
        method1();
        method2();
        method3();
        method4();
    }
}


Java
// Java Program to illustrate Use of Two Different Classes
// in the Same Package to Show the Scope of Access modifiers
  
// Importing input output classes
import java.io.*;
  
// Main class
// Helper class
class Helper {
  
    // Method 1
    public static void method1()
    {
  
        // Print statement whenever this method is called
        System.out.println(
            "This method uses Public access modifier");
    }
  
    // Method 2
    // Would not be accessed if set to "private"
    public static void method2()
    {
  
        // Print statement whenever this method is called
        System.out.println(
            "This method uses Private access modifier");
    }
  
    // Method 3
    protected static void method3()
    {
  
        // Print statement whenever this method is called
        System.out.println(
            "This method uses Protected access modifier");
    }
  
    // Method 4
    static void method4()
    {
  
        // Print statement whenever this method is called
        System.out.println(
            "This method uses Default access modifier");
    }
}
  
// Class 2
// Main class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Display message only
        System.out.println(
            "Various access modifiers being used in the same class");
  
        // Calling all the above methods in main() method
        // body
        Helper.method1();
        Helper.method2();
        Helper.method3();
        Helper.method4();
    }
}


Java
// Java Program to access Private Members of a Class
// using Getter and Setter methods
  
// Class 1
// Helper class for AccessModifier
class Helper {
  
    // Member variables of this class
  
    // private fields
    private int age;
    private String name;
  
    // Method 1
    // Setter method used to set age, a private field
    public void setAge(int age) { this.age = age; }
  
    // Method 2
    // Setter method used to set name, a private field
    public void setName(String name) { this.name = name; }
  
    // Method 3
    // Getter method used to get age, a private field.
    public int getAge() { return this.age; }
  
    // Method 4
    // Getter method used to get name, a private field
    public String getName() { return this.name; }
}
  
// Class 2
// Main class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        Helper ob = new Helper();
  
        // Set value of private fields
        ob.setAge(888);
  
        // Set value of private fields
        ob.setName("Geeksfor Geeks ");
  
        // Print and display the age using
        // get value of private fields
        System.out.println("Age: " + ob.getAge());
  
        // Print and display the name using
        // get value of private fields
        System.out.println("Name: " + ob.getName());
    }
}


输出
Various access modifiers being used in the same class
This method uses Public access modifier
This method uses Private access modifier
This method uses Protected access modifier
This method uses Default access modifier

示例 2:在同一个包中使用两个不同的类来显示访问修饰符的范围



Java

// Java Program to illustrate Use of Two Different Classes
// in the Same Package to Show the Scope of Access modifiers
  
// Importing input output classes
import java.io.*;
  
// Main class
// Helper class
class Helper {
  
    // Method 1
    public static void method1()
    {
  
        // Print statement whenever this method is called
        System.out.println(
            "This method uses Public access modifier");
    }
  
    // Method 2
    // Would not be accessed if set to "private"
    public static void method2()
    {
  
        // Print statement whenever this method is called
        System.out.println(
            "This method uses Private access modifier");
    }
  
    // Method 3
    protected static void method3()
    {
  
        // Print statement whenever this method is called
        System.out.println(
            "This method uses Protected access modifier");
    }
  
    // Method 4
    static void method4()
    {
  
        // Print statement whenever this method is called
        System.out.println(
            "This method uses Default access modifier");
    }
}
  
// Class 2
// Main class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Display message only
        System.out.println(
            "Various access modifiers being used in the same class");
  
        // Calling all the above methods in main() method
        // body
        Helper.method1();
        Helper.method2();
        Helper.method3();
        Helper.method4();
    }
}
输出
Various access modifiers being used in the same class
This method uses Public access modifier
This method uses Private access modifier
This method uses Protected access modifier
This method uses Default access modifier

让我们讨论上面缺少的场景,其中我们访问类的私有成员,我们可以使用getter 和 setter 方法来获取和设置Java中各种参数的值。

示例 3:访问类的私有数据成员

Java

// Java Program to access Private Members of a Class
// using Getter and Setter methods
  
// Class 1
// Helper class for AccessModifier
class Helper {
  
    // Member variables of this class
  
    // private fields
    private int age;
    private String name;
  
    // Method 1
    // Setter method used to set age, a private field
    public void setAge(int age) { this.age = age; }
  
    // Method 2
    // Setter method used to set name, a private field
    public void setName(String name) { this.name = name; }
  
    // Method 3
    // Getter method used to get age, a private field.
    public int getAge() { return this.age; }
  
    // Method 4
    // Getter method used to get name, a private field
    public String getName() { return this.name; }
}
  
// Class 2
// Main class
public class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        Helper ob = new Helper();
  
        // Set value of private fields
        ob.setAge(888);
  
        // Set value of private fields
        ob.setName("Geeksfor Geeks ");
  
        // Print and display the age using
        // get value of private fields
        System.out.println("Age: " + ob.getAge());
  
        // Print and display the name using
        // get value of private fields
        System.out.println("Name: " + ob.getName());
    }
}
输出
Age: 888
Name: Geeksfor Geeks