java非公共类是选什么,Java中的非公共顶级类

What's the reason of making top-level class non-public in Java?

Let's say we have Foo.java, there could be

class Foo {

}

or

public class Foo {

}

I understand that there will be some class - visibility issues with the former example (probably it won't be visible from other packages). But anyway, are there any reasons why someone may want to do as in the first code sample?

UPD: What cons I see in the former solution: nobody cares that it's non-public. That class can be simply extended by some other public class in the same package later, then, non-public part of the class may bring you visibility/access issues.

解决方案

Here is an example.

No one needs to know about existence of our ConcreteDocument.

DocumentIF.java

public interface DocumentIF {

}

ConcreteDocument.java

class ConcreteDocument implements DocumentIF {

}

DocumentFactory.java

public class DocumentFactory {

public DocumentIF createDocument() {

return new ConcreteDocument();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值