Access modifiers are used to set the feature of visibility of some particular classes, interfaces, variables, methods, constructors, data members, and the setter methods in Java programming language.
#在Java環境中,我們有不同類型的存取修飾符。
class Study { public void method16() {...} private void method7() {...} }
package a1; class Tutorialspoint{ void display(){ System.out.println("Welcome To Tutorialspoint!"); } }Java程式定義私有修飾符:
package a1; class A07{ private void display(){ System.out.println("Welcome To Tutorialspoint!"); } } class B07{ public static void main(String args[]){ A obj = new A(); obj.display(); } }
package a1; public class A07{ protected void display(){ System.out.println("Welcome To Tutorialspoint!"); } }Java程式定義public修飾符:
package a1; public class A{ public void display(){ System.out.println("Welcome To Tutorialspoint!"); } }
追隨的方法
Example 1
import java.io.*; public class tutorialspoint { public static void method07(){ System.out.println("This method uses Public access modifier - method07"); } private static void method16(){ System.out.println("This method uses Private access modifier-method16"); } protected static void method10(){ System.out.println("This method uses Protected access modifier-method10"); } static void method9701(){ System.out.println("This method uses Default access modifier-method10"); } public static void main(String[] args){ System.out.println("Various access modifiers being used in the same class"); method07(); method16(); method10(); method9701(); } }
Various access modifiers being used in the same class This method uses Public access modifier - method07 This method uses Private access modifier-method16 This method uses Protected access modifier-method10 This method uses Default access modifier-method10
Example 2
import java.io.*; class Helper { public static void method1(){ System.out.println("I Want To Travel Varanasi"); } public static void method2(){ System.out.println("It Is In UP, India"); } protected static void method3(){ System.out.println("Doon Express Is The Best Option"); } static void method4(){ System.out.println("__________________"); } } public class TP { public static void main(String[] args){ System.out.println("Various access modifiers being used in the same class"); Helper.method1(); Helper.method2(); Helper.method3(); Helper.method4(); } }
Various access modifiers being used in the same class I Want To Travel Varanasi It Is In UP, India Doon Express Is The Best Option
Example 3
import java.io.*; class Helper { private int age; private String name; public void setAge(int age) { this.age = age; } public void setName(String name) { this.name = name; } public int getAge() { return this.age; } public String getName() { return this.name; } } public class Tutorialspoint { public static void main(String[] args){ Helper ob = new Helper(); ob.setAge(2001); ob.setName("We Are The Tutors Of Tutorialspoint"); System.out.println("Age: " + ob.getAge()); System.out.println("Name: " + ob.getName()); } }
Age: 2001 Name: We Are The Tutors Of Tutorialspoint
以上是Java程式展示不同的存取級別的詳細內容。更多資訊請關注PHP中文網其他相關文章!