Java 访问控制权限:Mammal 类和 Cat 类示例

本示例演示了 Java 中不同访问控制权限 (public, protected, default, private) 在类和方法中的应用。通过 Mammal 类和 Cat 类,演示了继承、重写、访问控制和对象引用之间的关系。

1. 创建 Java 工程

首先,创建一个新的 Java 工程。

2. 创建 Mammal 类

创建 Mammal 类,设置属性 weight, height, legs, tail,并分别使用 public、protected、default 和 private 对其进行访问控制权限设置。同时定义方法 printWeight(), printHeight(), printLegs(), printTail(),并分别使用 public、protected、default 和 private 对其进行访问控制权限设置。每个方法中只有一条输出语句。

public class Mammal {
    public double weight;
    protected double height;
    double legs;
    private double tail;

    public void printWeight() {
        System.out.println('The weight is: ' + weight);
    }

    protected void printHeight() {
        System.out.println('The height is: ' + height);
    }

    void printLegs() {
        System.out.println('The number of legs is: ' + legs);
    }

    private void printTail() {
        System.out.println('The length of tail is: ' + tail);
    }
}

3. 创建 Cat 类

创建 Cat 类作为 Mammal 类的子类。重写 printWeight() 方法,使其输出不同的内容。

public class Cat extends Mammal {
    @Override
    public void printWeight() {
        System.out.println('The weight of the cat is: ' + weight);
    }
}

4. 创建测试类 Test

创建测试类 Test 作为主类,并在 main() 方法中创建 Mammal 类和 Cat 类的对象,分别使用它们访问其成员,观察代码窗口中的提示信息。最后,将 Cat 类对象的引用赋值给 Mammal 类变量,并调用 printWeight() 方法,观察输出结果。

public class Test {
    public static void main(String[] args) {
        Mammal animal = new Mammal();
        animal.weight = 10.5;
        animal.height = 25.0;
        animal.legs = 4;
        // animal.tail = 15.0; // Error: 'tail' has private access in 'Mammal'
        animal.printWeight(); // The weight is: 10.5
        animal.printHeight(); // The height is: 25.0
        animal.printLegs(); // The number of legs is: 4
        // animal.printTail(); // Error: 'printTail()' has private access in 'Mammal'

        Cat tommy = new Cat();
        tommy.weight = 8.0;
        tommy.height = 20.0;
        tommy.legs = 4;
        // tommy.tail = 12.0; // Error: 'tail' has private access in 'Mammal'
        tommy.printWeight(); // The weight of the cat is: 8.0
        tommy.printHeight(); // The height is: 20.0
        tommy.printLegs(); // The number of legs is: 4
        // tommy.printTail(); // Error: 'printTail()' has private access in 'Mammal'

        animal = tommy;
        animal.printWeight(); // The weight of the cat is: 8.0
    }
}

输出结果

The weight is: 10.5
The height is: 25.0
The number of legs is: 4
The weight of the cat is: 8.0
The height is: 20.0
The number of legs is: 4
The weight of the cat is: 8.0

通过该示例,我们可以观察到以下几点:

  • public 访问权限:类中的 public 属性和方法可以在任何地方被访问。
  • protected 访问权限:类中的 protected 属性和方法只能在当前类、子类以及同一包中的其他类中访问。
  • default 访问权限 (也称为包访问权限):类中的 default 属性和方法只能在当前类、同一包中的其他类中访问。
  • private 访问权限:类中的 private 属性和方法只能在当前类中访问。
  • 子类继承了父类的 public 和 protected 成员,但不能继承 private 成员。
  • 子类可以重写父类的 public 和 protected 成员,但不能重写 private 成员。
  • 对象引用可以指向子类对象,此时可以通过父类引用访问子类对象继承或重写的方法。

本示例帮助理解 Java 访问控制权限的概念和应用,并展示了它们在实际代码中的应用。通过实践,可以更深入地理解不同访问控制权限之间的区别和作用。

Java 访问控制权限:Mammal 类和 Cat 类示例

原文地址: http://www.cveoy.top/t/topic/paZO 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录