在阅读了c#的隐藏特性之后,我想知道Java的隐藏特性有哪些?
当前回答
交集类型允许您(有点)执行具有继承层次结构的枚举。您不能继承实现,但可以将其委托给助手类。
enum Foo1 implements Bar {}
enum Foo2 implements Bar {}
class HelperClass {
static <T extends Enum<T> & Bar> void fooBar(T the enum) {}
}
当您有许多实现某种模式的不同枚举时,这很有用。例如,许多具有父子关系的枚举对。
enum PrimaryColor {Red, Green, Blue;}
enum PastelColor {Pink, HotPink, Rockmelon, SkyBlue, BabyBlue;}
enum TransportMedium {Land, Sea, Air;}
enum Vehicle {Car, Truck, BigBoat, LittleBoat, JetFighter, HotAirBaloon;}
你可以编写泛型方法,说“好的,给定一个枚举值,它是其他一些枚举值的父枚举值,所有可能的子枚举的子类型中有多少百分比是这个特定的父值作为它们的父?”,并让它都是类型安全的,并且没有强制转换。(例如:“海洋”是所有可能的车辆的33%,“绿色”是所有可能的粉彩的20%)。
代码是这样的。这很糟糕,但有办法让它变得更好。特别要注意的是,“叶子”类本身非常整洁——泛型类的声明非常丑陋,但您只编写一次。一旦有了泛型类,使用它们就很容易了。
import java.util.EnumSet;
import javax.swing.JComponent;
public class zz extends JComponent {
public static void main(String[] args) {
System.out.println(PrimaryColor.Green + " " + ParentUtil.pctOf(PrimaryColor.Green) + "%");
System.out.println(TransportMedium.Air + " " + ParentUtil.pctOf(TransportMedium.Air) + "%");
}
}
class ParentUtil {
private ParentUtil(){}
static <P extends Enum<P> & Parent<P, C>, C extends Enum<C> & Child<P, C>> //
float pctOf(P parent) {
return (float) parent.getChildren().size() / //
(float) EnumSet.allOf(parent.getChildClass()).size() //
* 100f;
}
public static <P extends Enum<P> & Parent<P, C>, C extends Enum<C> & Child<P, C>> //
EnumSet<C> loadChildrenOf(P p) {
EnumSet<C> cc = EnumSet.noneOf(p.getChildClass());
for(C c: EnumSet.allOf(p.getChildClass())) {
if(c.getParent() == p) {
cc.add(c);
}
}
return cc;
}
}
interface Parent<P extends Enum<P> & Parent<P, C>, C extends Enum<C> & Child<P, C>> {
Class<C> getChildClass();
EnumSet<C> getChildren();
}
interface Child<P extends Enum<P> & Parent<P, C>, C extends Enum<C> & Child<P, C>> {
Class<P> getParentClass();
P getParent();
}
enum PrimaryColor implements Parent<PrimaryColor, PastelColor> {
Red, Green, Blue;
private EnumSet<PastelColor> children;
public Class<PastelColor> getChildClass() {
return PastelColor.class;
}
public EnumSet<PastelColor> getChildren() {
if(children == null) children=ParentUtil.loadChildrenOf(this);
return children;
}
}
enum PastelColor implements Child<PrimaryColor, PastelColor> {
Pink(PrimaryColor.Red), HotPink(PrimaryColor.Red), //
Rockmelon(PrimaryColor.Green), //
SkyBlue(PrimaryColor.Blue), BabyBlue(PrimaryColor.Blue);
final PrimaryColor parent;
private PastelColor(PrimaryColor parent) {
this.parent = parent;
}
public Class<PrimaryColor> getParentClass() {
return PrimaryColor.class;
}
public PrimaryColor getParent() {
return parent;
}
}
enum TransportMedium implements Parent<TransportMedium, Vehicle> {
Land, Sea, Air;
private EnumSet<Vehicle> children;
public Class<Vehicle> getChildClass() {
return Vehicle.class;
}
public EnumSet<Vehicle> getChildren() {
if(children == null) children=ParentUtil.loadChildrenOf(this);
return children;
}
}
enum Vehicle implements Child<TransportMedium, Vehicle> {
Car(TransportMedium.Land), Truck(TransportMedium.Land), //
BigBoat(TransportMedium.Sea), LittleBoat(TransportMedium.Sea), //
JetFighter(TransportMedium.Air), HotAirBaloon(TransportMedium.Air);
private final TransportMedium parent;
private Vehicle(TransportMedium parent) {
this.parent = parent;
}
public Class<TransportMedium> getParentClass() {
return TransportMedium.class;
}
public TransportMedium getParent() {
return parent;
}
}
其他回答
类型参数方差的联合:
public class Baz<T extends Foo & Bar> {}
例如,如果你想取一个参数,它既是可比的又是集合的:
public static <A, B extends Collection<A> & Comparable<B>>
boolean foo(B b1, B b2, A a) {
return (b1.compareTo(b2) == 0) || b1.contains(a) || b2.contains(a);
}
如果两个给定的集合相等,或者其中任何一个包含给定元素,则该方法返回true,否则返回false。需要注意的一点是,您可以对参数b1和b2调用Comparable和Collection方法。
JDK发行版中的bin目录下的JVisualVM。监视甚至分析任何java应用程序,即使是没有使用任何特殊参数启动的应用程序。仅在Java 6SE JDK的最新版本中。
我喜欢
Javadoc的taglet和doclet使我们能够自定义Javadoc输出。 JDK工具:jstat, jstack等。
真正让我吃惊的是自定义序列化机制。
而这些方法是私有的!!,它们会在对象序列化期间被JVM“神秘地”调用。
private void writeObject(ObjectOutputStream out) throws IOException;
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException;
通过这种方式,您可以创建自己的自定义序列化,使其更加“随便”(安全、快速、罕见、简单等)。
如果大量信息必须通过节点传递,这是真正应该考虑的事情。序列化机制可以更改为发送数据的一半。很多时候,瓶颈并不在平台上,而是在通过线路发送的数量上,这可能会为您节省数千个硬件dll。
这是一篇文章。 http://java.sun.com/developer/technicalArticles/Programming/serialization/
语言级assert关键字。
推荐文章
- 在流中使用Java 8 foreach循环移动到下一项
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 用Java计算两个日期之间的天数
- 如何配置slf4j-simple
- 在Jar文件中运行类
- 带参数的可运行?
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 我可以在Java中设置enum起始值吗?
- Java中的回调函数
- c#和Java中的泛型有什么不同?和模板在c++ ?
- 在Java中,流相对于循环的优势是什么?
- Jersey在未找到InjectionManagerFactory时停止工作
- 在Java流是peek真的只是调试?
- Recyclerview不调用onCreateViewHolder
- 将JSON字符串转换为HashMap