Java类成员访问控制与嵌套类详解
立即解锁
发布时间: 2025-08-18 02:26:18 阅读量: 2 订阅数: 18 

### Java 类成员访问控制与嵌套类详解
#### 1. 访问属性的使用
在 Java 中,同一包内的类可以直接使用其他类名来声明变量或指定方法参数类型,但其他类的变量和方法不一定能直接访问,其可访问性由访问属性控制。
不同包之间,只有声明为 `public` 的类才能被其他包中的类访问,未声明为 `public` 的类只能被同一包内的类访问。
类成员的访问属性有以下四种,其允许的访问范围如下表所示:
| 属性 | 允许的访问范围 |
| ---- | ---- |
| 无访问属性 | 同一包内任何类的方法 |
| public | 只要类声明为 `public`,任何地方的任何类的方法都可访问 |
| private | 只能从类内部的方法访问,外部类完全无法访问 |
| protected | 同一包内任何类的方法,以及任何地方的子类的方法 |
下面通过一个简单的示例来说明同一包内类成员的访问情况:
```mermaid
graph LR
classDef process fill:#E5F6FF,stroke:#73A6FF,stroke-width:2px;
A(Class1):::process -->|int a| B(Class2):::process
A -->|public int b| B
A -->|protected int c| B
A -.->|private int e| B
A -->|int a| C(SubClass1):::process
A -->|public int b| C
A -->|protected int c| C
A -.->|private int e| C
```
从上图可以看出,同一包内,`Class1` 的 `private` 成员不能被 `Class2` 和 `SubClass1` 直接访问。
不同包之间的访问情况更为严格,只有 `Class1` 声明为 `public`,且其成员也为 `public` 时,才能被其他包中的普通类(如 `Class2`)访问。
#### 2. 指定访问属性
要为类成员指定访问属性,只需在声明前添加相应的关键字。以下是添加了访问属性的 `Point` 类示例:
```java
import static java.lang.Math.sqrt;
public class Point {
// Create a point from its coordinates
public Point(double xVal, double yVal) {
x = xVal;
y = yVal;
}
// Create a Point from an existing Point object
public Point(final Point aPoint) {
x = aPoint.x;
y = aPoint.y;
}
// Move a point
public void move(double xDelta, double yDelta) {
// Parameter values are increments to the current coordinates
x += xDelta;
y += yDelta;
}
// Calculate the distance to another point
public double distance(final Point aPoint) {
return sqrt((x - aPoint.x)*(x - aPoint.x)+(y - aPoint.y)*(y - aPoint.y));
}
// Convert a point to a string
public String toString() {
return Double.toString(x) + ", " + y; // As “x, y”
}
// Coordinates of the point
private double x;
private double y;
}
```
在这个示例中,`x` 和 `y` 被声明为 `private`,外部类无法直接访问或修改它们。若需要获取其值,可以通过访问器方法(如 `getX()`);若需要修改其值,可以通过修改器方法(如 `setX()`)。
#### 3. 选择访问属性
在大多数简单类(无子类)的情况下,应明确指定类成员为 `public` 或 `private`,而不是省略访问属性。一般来说,公共类的变量应设为 `private`,外部调用的方法应设为 `public`。
但也有一些例外情况:
- 对于包内非 `public` 的类,有时允许包内其他类直接访问数据成员会更方便。
- 若数据成员被指定为 `final`,且其值固定且可能在类外部有用,可将其声明为 `public`。
- 类中仅供内部其他方法使用的方法应指定为 `private`。
- 像标准类 `Math` 这样的实用工具类,应将所有成员声明为 `public`。
#### 4. 使用包和访问属性
下面通过一个示例展示如何使用自定义包。将之前定义的 `Point` 和 `Line` 类放入名为 `Geometry` 的包中,并编写一个程序来导入和测试这些类。
首先,修改 `Point.java` 文件:
```java
package Geometry;
import static java.lang.Math.sqrt;
public class Point {
// Create a point from its coordinates
public Point(double xVal, double yVal) {
x = xVal;
y = yVal;
}
// Create a Point from an existing Point object
public Point(final Point aPoint) {
x = aPoint.x;
y = aPoint.y;
}
// Move a point
public void move(double xDelta, double yDelta) {
// Parameter values are increments to the current coordinates
x += xDelta;
y += yDelta;
}
// Calculate the distance to another point
public double distance(final Point aPoint) {
return sqrt((x - aPoint.x)*(x - aPoint.x)+(y - aPoint.y)*(y - aPoint.y));
}
// Convert a point to a string
public String toString() {
return Double.toString(x) + ", " + y; // As “x, y”
}
// Retrieve the x coordinate
public double getX() {
return x;
}
// Retrieve the y coordinate
public double getY() {
return y;
}
// Set the x coordinate
public void setX(double inputX) {
```
0
0
复制全文
相关推荐










