Java修饰符

本文探讨了类成员的访问级别,包括public、protected、private和无修饰符,解释了这些级别如何影响不同类、包和子类的访问权限。通过实例分析,理解如何在编程中决定方法和变量的可见性,以及跨包、继承和全局访问的规则。

Controlling Access to Members of a Class

Access Levels
ModifierClassPackageSubclassWorld
publicYYYY
protectedYYYN
no modifier(未指定默认default)YYNN
privateYNNN

        The first data column indicates whether the class itself has access to the member defined by the access level. As you can see, a class always has access to its own members. The second column indicates whether classes in the same package as the class (regardless of their parentage) have access to the member. The third column indicates whether subclasses of the class declared outside this package have access to the member. The fourth column indicates whether all classes have access to the member.

        Access levels affect you in two ways. First, when you use classes that come from another source, such as the classes in the Java platform, access levels determine which members of those classes your own classes can use. Second, when you write a class, you need to decide what access level every member variable and every method in your class should have.

        Let's look at a collection of classes and see how access levels affect visibility. The following figure shows the four classes in this example and how they are related.

Classes and Packages of the Example Used to Illustrate Access Levels

 Classes and Packages of the Example Used to Illustrate Access Levels

        The following table shows where the members of the Alpha class are visible for each of the access modifiers that can be applied to them.

Visibility
ModifierAlphaBetaAlphasubGamma
publicYYYY
protectedYYYN
no modifierYYNN
privateYNNN
### Java 修饰符详解及使用场景 Java 中的修饰符是用来控制类、方法、变量和构造器的访问级别和行为的关键字。它们分为两类:**访问控制修饰符**和**非访问控制修饰符**。 #### 1. 访问控制修饰符 访问控制修饰符用于限制类、方法和变量的可访问性,从而提高代码的封装性和安全性。Java 提供了四种访问控制修饰符:`private`、`default`(无修饰符)、`protected` 和 `public`。 - **`private`(私有)** 当一个类、方法或变量被声明为 `private` 时,它只能在声明它的同一类中访问。其他类无法直接访问该成员[^2]。 示例代码如下: ```java public class Example { private String secret = "This is a secret"; public void displaySecret() { System.out.println(secret); // 可以访问 } } ``` - **`default`(默认)** 如果没有显式地指定访问修饰符,则该成员具有默认访问权限。这种情况下,只有同一个包中的类可以访问该成员[^2]。 示例代码如下: ```java class DefaultExample { String message = "Default access"; } package com.example; class AnotherClass { DefaultExample example = new DefaultExample(); System.out.println(example.message); // 同一包内可以访问 } ``` - **`protected`(受保护)** 声明为 `protected` 的成员可以被同一个包中的任何类访问,也可以被不同包中的子类访问[^2]。 示例代码如下: ```java public class Parent { protected String info = "Protected data"; } public class Child extends Parent { public void displayInfo() { System.out.println(info); // 子类可以访问 } } ``` - **`public`(公共)** 声明为 `public` 的成员可以被任何类访问,无论它们是否在同一个包中[^2]。 示例代码如下: ```java public class PublicExample { public String message = "Public access"; } class AnyClass { PublicExample example = new PublicExample(); System.out.println(example.message); // 可以访问 } ``` #### 2. 非访问控制修饰符 非访问控制修饰符用于定义类、方法或变量的行为,而不是访问权限。 - **`final`** `final` 修饰符可以应用于类、方法或变量。当应用于类时,表示该类不能被继承;当应用于方法时,表示该方法不能被重写;当应用于变量时,表示该变量的值一旦赋值后不能更改[^4]。 示例代码如下: ```java public class FinalExample { final int number = 10; // 不能重新赋值 public FinalExample() { // number = 20; // 错误:不能重新赋值 } } ``` - **`static`** `static` 修饰符用于创建类级别的成员。静态成员属于类本身,而不是类的实例。因此,可以通过类名直接访问静态成员[^1]。 示例代码如下: ```java public class StaticExample { static int count = 0; public static void incrementCount() { count++; } } StaticExample.incrementCount(); // 类名调用静态方法 System.out.println(StaticExample.count); // 类名访问静态变量 ``` - **`abstract`** `abstract` 修饰符用于定义抽象类或方法。抽象类不能被实例化,必须由子类继承并实现其抽象方法[^1]。 示例代码如下: ```java public abstract class AbstractExample { public abstract void performAction(); // 抽象方法 } public class ConcreteExample extends AbstractExample { @Override public void performAction() { System.out.println("Concrete implementation"); } } ``` - **`synchronized`** `synchronized` 修饰符用于控制多线程环境下的访问。它确保在同一时刻只有一个线程可以访问被修饰的方法或代码块。 示例代码如下: ```java public class SynchronizedExample { public synchronized void doSomething() { // 线程安全的代码 } } ``` - **`transient`** `transient` 修饰符用于指示某个变量不应被序列化。当对象被序列化时,标记为 `transient` 的变量将不会被保存到文件中[^1]。 示例代码如下: ```java public class TransientExample implements Serializable { transient int tempValue = 10; // 不会被序列化 } ``` - **`volatile`** `volatile` 修饰符用于确保多个线程能够正确处理共享变量。它强制每次读取都从主内存中获取最新值,而不是从线程本地缓存中读取[^1]。 示例代码如下: ```java public class VolatileExample { volatile boolean flag = false; public void changeFlag() { flag = true; } } ``` --- ### 总结 Java 中的修饰符包括访问控制修饰符和非访问控制修饰符,每种修饰符都有其特定的用途和使用场景。合理使用这些修饰符可以增强代码的封装性、安全性和可维护性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

漁陽

彼此共勉,砥砺前行

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值