将包专用类导入到JShell [英] Importing Package-Private Classes to JShell

查看:145
本文介绍了将包专用类导入到JShell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java 9发行后,我一直在使用JShell,并且尝试导入自己制作的软件包.由于我要为其编写代码的整个应用程序都包含在该程序包中,因此除了一个类(我尚未编码)之外的每个类都是程序包私有的.我的类路径是正确的,但是我仍然不能使用JShell包中声明的任何类型(它会引发找不到符号"错误). 我需要将它们公开以使其可以访问,还是可以通过某种方式测试软件包专用类 ?这是我尝试过的确切代码.

I was playing around with JShell after the Java 9 release, and I tried importing a package I made. As the entire application I'm coding it for will be contained in that package, every class but one (which I haven't coded yet) is package-private. My classpath is correct, but I still can't use any of the types declared in the package in JShell (it throws a "cannot find symbol" error). Do I need to make them public for them to be accessible, or is there some way I can test package-private classes? Here's the exact code I tried.

我当前的目录是

C:\Users\Sylvaenn\OneDrive\Documents\Programs\Java\src

我的课程路径是

C:\Users\Sylvaenn\OneDrive\Documents\Programs\Java\cls

,包目录(用于字节码)为

and the package directory (for the bytecode) is

C:\Users\Sylvaenn\OneDrive\Documents\Programs\Java\cls\collatz

CollatzSequencecollatz中包含的程序包专用类.

CollatzSequence is a package-private class contained in collatz.

PS C:\Users\Sylvaenn> cd OneDrive\Documents\Programs\Java\src
PS C:\Users\Sylvaenn\OneDrive\Documents\Programs\Java\src> jshell
|  Welcome to JShell -- Version 9
|  For an introduction type: /help intro

jshell> import collatz.*;

jshell> CollatzSequence seq = new CollatzSequence(BigInteger.ONE);
|  Error:
|  cannot find symbol
|    symbol:   class CollatzSequence
|  CollatzSequence seq = new CollatzSequence(BigInteger.ONE);
|  ^-------------^
|  Error:
|  cannot find symbol
|    symbol:   class CollatzSequence
|  CollatzSequence seq = new CollatzSequence(BigInteger.ONE);
|                            ^-------------^

jshell> /imports
|    import java.io.*
|    import java.math.*
|    import java.net.*
|    import java.nio.file.*
|    import java.util.*
|    import java.util.concurrent.*
|    import java.util.function.*
|    import java.util.prefs.*
|    import java.util.regex.*
|    import java.util.stream.*
|    import collatz.*

jshell>

推荐答案

据我所知(如果我错了,请纠正我),您无法使用JShell在特定的程序包中创建类 (在JShell中创建的类始终在默认包中.

As far as i know (correct me if i am wrong), you cannot create a Class in a specific package using JShell (classes created within JShell are always in the default package).

话虽如此,您不能从JShell中访问程序包专用类.这是Java的正常"行为.

That being said, you cannot access your package-private classes from within JShell. This is "normal" Java behaviour.

这篇关于将包专用类导入到JShell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆