这里可以继承吗?内部父类是否扩展外部子类?

发布于 2024-10-08 14:10:27 字数 131 浏览 0 评论 0原文

A
|_A1
|  |_parent.java
|_child.java

Parent.java 是否以任何可能的方式继承 child.java ?

这里 A 和 A1 是包或目录

A
|_A1
|  |_parent.java
|_child.java

does parent.java inherits child.java in any possible way?

here A and A1 are packages or directories

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

執念 2024-10-15 14:10:27

仅当 Child 具有 extends 子句时

public class Child extends Parent{}

如果是这样,Child 将有权访问 publicprotected 的所有成员代码>父级。否则,Child 将只能访问 Parentpublic 成员。

如果文件位于同一目录层次结构内,但不在同一目录内,则包不被视为相关,因此具有默认(“包保护”)可见性的成员不可见。

相关阅读:

Only if Child has an extends clause

public class Child extends Parent{}

If so, Child will have access to all public and protected members of Parent. Otherwise, Child will only have access to public members of Parent.

If files are inside the same directory hierarchy, but not within the same directory, the packages are not considered related, and hence members with default ("package-protected") visibility are not visible.

Relevant reading:

风启觞 2024-10-15 14:10:27

您的图片显示了文件夹和 java 源文件。我们有一个文件夹 A,其中包含文件 child.java 和另一个文件夹 A1A1 包含 java 源文件 parent.java

在文件系统文件夹中排列源文件(或类文件)不会创建或声明类之间的关系。

如果您希望类 parent 继承类 child (iaw:parent 继承 child 的字段和方法- 在现实世界中很奇怪但可能),你必须在java源代码中声明这种关系。保留您的名称,文件必须如下所示:

child.java

package A;
public class child {};

parent.java

package A.A1;
import A.child;
public class child extends parent {};

注意 - java 命名约定强烈建议,即包名称都是小写字母,类名以大写字母开头。

Your picture shows folders and java source files. We have one folder A that contains the file child.java and another folder A1. A1 contains the java source file parent.java.

Arranging source files (or class file) in filesystem folers does not create or declare relationships between classes.

If you want class parent to inherit (from) class child (iaw: parent inherits fields and method from child - strange in real world but possible), you have to declare this relation in the java source code. Keeping your names, the files have to look like this:

child.java

package A;
public class child {};

parent.java

package A.A1;
import A.child;
public class child extends parent {};

Note - java naming conventions strongly recommend, that package names are all lower case and class names start with a capital letter.

清晰传感 2024-10-15 14:10:27

我的理解是,这是你的包/树层次结构,并且你要求任何继承。
如果有任何继承,它看起来像任何继承/树层次结构吗?如果您破坏继承树,则意味着在任何其他不相关的包或任何其他节点下不应该有任何继承的类。换句话说,缺乏设计模式。如果这样做,您将处理包级别以及与其他包的冗余可访问性,因此您不能谈论此项目中的 OO 或任何封装

NO

What I understood is that it is your package/tree hierarchy and you asked for any inheritance.
If there is any inheritance, does it look like any inheritance/tree hierarchy? If you destroy the inheritance tree it means there shouldn't be any inherited class under any other irrelevant package or under any other node. In other words, lack of design pattern. If you do this, you will handle the redundant accesibility in package-level and with other packages and so you can't talk anything about OO or any encapsulation in this project

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文