protected 关键字
概述
protected
关键字是Java的访问修饰符,允许同包类和子类访问被修饰的成员。
语法格式
protected int variable; // 受保护的变量
protected void method() { } // 受保护的方法
protected ClassName() { } // 受保护的构造器
基本用法
// 父类
package com.example.parent;
public class Animal {
protected String name; // 子类可以访问
protected int age;
private String id; // 子类不能访问
protected Animal(String name, int age) {
this.name = name;
this.age = age;
this.id = generateId();
}
protected void eat() {
System.out.println(name + " 正在吃东西");
}
protected void displayInfo() {
System.out.println("动物: " + name + ", 年龄: " + age);
}
private String generateId() {
return "ID_" + System.currentTimeMillis();
}
}
// 子类
package com.example.child;
import com.example.parent.Animal;
public class Dog extends Animal {
protected String breed;
public Dog(String name, int age, String breed) {
super(name, age); // 调用protected构造器
this.breed = breed;
}
@Override
protected void eat() {
super.eat(); // 调用protected方法
System.out.println(name + " 吃狗粮"); // 访问protected字段
}
public void dogInfo() {
displayInfo(); // 调用继承的protected方法
System.out.println("品种: " + breed);
}
}
// 同包类可以访问protected成员
package com.example.parent;
public class AnimalCare {
public void careForAnimal(Animal animal) {
System.out.println("照顾动物: " + animal.name); // 可以访问protected字段
animal.eat(); // 可以调用protected方法
}
}
访问级别对比
public class AccessLevelDemo {
public String publicField = "任何地方都可访问";
protected String protectedField = "同包或子类可访问";
String packageField = "同包可访问";
private String privateField = "仅本类可访问";
public void showAccess() {
// 本类中可以访问所有成员
System.out.println(publicField);
System.out.println(protectedField);
System.out.println(packageField);
System.out.println(privateField);
}
}
// 同包子类
class SubClass extends AccessLevelDemo {
public void testAccess() {
System.out.println(publicField); // ✓ 可以访问
System.out.println(protectedField); // ✓ 可以访问
System.out.println(packageField); // ✓ 可以访问
// System.out.println(privateField); // ✗ 不能访问
}
}
// 同包非子类
class SamePackageClass {
public void testAccess() {
AccessLevelDemo demo = new AccessLevelDemo();
System.out.println(demo.publicField); // ✓ 可以访问
System.out.println(demo.protectedField); // ✓ 可以访问(同包)
System.out.println(demo.packageField); // ✓ 可以访问
// System.out.println(demo.privateField); // ✗ 不能访问
}
}
模板方法模式
public abstract class GameTemplate {
// 模板方法 - public,子类不能重写
public final void playGame() {
initialize();
startPlay();
endPlay();
cleanup();
}
// protected方法供子类实现
protected abstract void initialize();
protected abstract void startPlay();
protected abstract void endPlay();
// 默认实现,子类可以重写
protected void cleanup() {
System.out.println("游戏结束,清理资源");
}
}
class Football extends GameTemplate {
@Override
protected void initialize() {
System.out.println("足球游戏初始化");
}
@Override
protected void startPlay() {
System.out.println("足球游戏开始");
}
@Override
protected void endPlay() {
System.out.println("足球游戏结束");
}
}
class Basketball extends GameTemplate {
@Override
protected void initialize() {
System.out.println("篮球游戏初始化");
}
@Override
protected void startPlay() {
System.out.println("篮球游戏开始");
}
@Override
protected void endPlay() {
System.out.println("篮球游戏结束");
}
@Override
protected void cleanup() {
super.cleanup();
System.out.println("篮球特殊清理");
}
}
protected关键字在继承体系中提供了适当的封装级别,是实现模板方法模式的重要工具。