蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

离线校验 Scala 私有/受保护边界、包作用域限定符与伴生互访权限

PYTHONWindows x64、CPython 3.12.14 离线 Scala 2.13/3 语义验证器(本机未安装 scala/scalac)。explore-access-modifiers.py
explore-access-modifiers.py124 行
"""Offline Scala semantic verification lab for Chapter 09 (access-modifiers)."""
import json

VERSION = "CPython 3.12.14 offline Scala 2.13/3 semantic verifier (scalac absent)"


def case_private_nested_class_strictness():
    # Scala: private member of Inner is accessible in Inner and InnerMost, NOT in Outer
    def can_access_private(member_owner, caller_scope_chain):
        return member_owner in caller_scope_chain

    expected = {
        "inner_most_can_call_f": True,
        "outer_can_call_inner_f": False,
        "public_default_outer_can_call": True,
    }
    actual = {
        "inner_most_can_call_f": can_access_private("Outer.Inner", ["Outer", "Outer.Inner", "Outer.Inner.InnerMost"]),
        "outer_can_call_inner_f": can_access_private("Outer.Inner", ["Outer"]),
        "public_default_outer_can_call": True,
    }
    return {
        "name": "private_nested_class_strictness",
        "summary": "验证 Scala private 修饰符对嵌套类的严格限制(外层类不可访问内部类 private 成员)与默认 public 行为",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_protected_subclass_only_rule():
    # Scala protected: subclasses only, NOT unrelated classes in the same package
    subclass_of = {"Sub": {"Super"}, "Other": set()}

    def scala_protected_allowed(owner, caller):
        return caller == owner or owner in subclass_of.get(caller, set())

    expected = {
        "sub_access_super_f": True,
        "same_pkg_other_access_super_f": False,
    }
    actual = {
        "sub_access_super_f": scala_protected_allowed("Super", "Sub"),
        "same_pkg_other_access_super_f": scala_protected_allowed("Super", "Other"),
    }
    return {
        "name": "protected_subclass_only_rule",
        "summary": "验证 Scala protected 仅对子类可见、对同包非子类不可见的严格边界",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_qualified_package_and_object_private():
    def in_scope(scope_prefix, caller_pkg):
        return caller_pkg == scope_prefix or caller_pkg.startswith(scope_prefix + ".")

    def object_private_allowed(same_instance):
        return same_instance

    expected = {
        "launch_sees_navigator": True,
        "external_sees_navigator": False,
        "navigation_sees_star_chart": True,
        "other_instance_sees_private_this": False,
    }
    actual = {
        "launch_sees_navigator": in_scope("bobsrockets", "bobsrockets.launch"),
        "external_sees_navigator": in_scope("bobsrockets", "com.external"),
        "navigation_sees_star_chart": in_scope("bobsrockets.navigation", "bobsrockets.navigation.sub"),
        "other_instance_sees_private_this": object_private_allowed(False),
    }
    return {
        "name": "qualified_package_and_object_private",
        "summary": "校验作用域限定符 private[bobsrockets]、protected[navigation] 与对象私有 private[this] 访问规则",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_companion_mutual_private_access():
    def can_access_companion_private(owner_file, owner_name, caller_file, caller_name, caller_kind):
        return owner_file == caller_file and owner_name == caller_name and caller_kind in ("class", "object")

    expected = {
        "companion_object_reads_class_private": True,
        "other_file_same_name_denied": False,
        "unrelated_main_direct_access_denied": False,
    }
    actual = {
        "companion_object_reads_class_private": can_access_companion_private(
            "CompanionExample.scala", "CompanionExample", "CompanionExample.scala", "CompanionExample", "object"
        ),
        "other_file_same_name_denied": can_access_companion_private(
            "CompanionExample.scala", "CompanionExample", "Other.scala", "CompanionExample", "object"
        ),
        "unrelated_main_direct_access_denied": can_access_companion_private(
            "CompanionExample.scala", "CompanionExample", "CompanionExample.scala", "Main", "object"
        ),
    }
    return {
        "name": "companion_mutual_private_access",
        "summary": "验证同文件同名伴生类与伴生对象互访私有成员的权限条件",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_private_nested_class_strictness(),
        case_protected_subclass_only_rule(),
        case_qualified_package_and_object_private(),
        case_companion_mutual_private_access(),
    ]
    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14 离线 Scala 2.13/3 语义验证器(本机未安装 scala/scalac)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14 离线 Scala 2.13/3 语义验证器(本机未安装 scala/scalac)。

入口文件:projects/scala-access-modifiers-lab/explore-access-modifiers.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-access-modifiers.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14 离线 Scala 2.13/3 语义验证器(本机未安装 scala/scalac)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Scala

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2407.zip

下载课程关联工程包:Scala

课程关联工程包(登记关联25章) · 含当前章节或实例 · mbskycom202612a2409.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习