提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
一个类的包访问成员可由同一包中的其他类访问。
public class Main // 来自 NowJava.com - 时 代 Java { public static void main(String[] args) { PackageData packageData = new PackageData(); // output String representation of packageData System.out.printf("After instantiation:%n%s%n", packageData); // change package access data in packageData object packageData.number = 77; packageData.string = "Goodbye"; // output String representation of packageData System.out.printf("%nAfter changing values:%n%s%n", packageData); } } // class with package access instance variables class PackageData { int number; // package-access instance variable/**from 时 代 Java - nowjava.com**/ String string; // package-access instance variable // constructor public PackageData() { number = 0; string = "Hello"; }