简体   繁体   中英

Public Java class named differently than file

At one of the university classes we have to develop programs in Java. One of the requirements is to name all our files with pattern surname_firstname_exerciseN_className.java . Another requirement is that we must split our programs into multiple files. As you can imagine, these two don't play well together.

I'm trying to work around this by "translating" my class names. For example, if I write a class named "Something", I do this in my long_prefix_something.java file:

public class long_prefix_something extends Something {}

class Something {
    // class code
}

And I want to use class Something in another file. So I do this in that other file:

class Something2 extends long_prefix_something { }

What bothers me, is that I can't translate long_prefix_something back to Something because of circular inheritance error, I have to use Something2 instead.

Is there anyway to overcome this? Any annotation to use or something similar?

Necessity of such hacks is usually sign of bad design. Circular (and multiple) inheritance is not allowed in java.

I agree (with other commenters) that this sounds a little scary to me... But there, nevertheless, exist a few java conventions for dealing with problems similar to yours.

It appears that you need classes which are "more descriptive" of their internal derivation then standard java classes in an inheritance hierarchy.

Your need can be satisfied in 2 standard ways : Interfaces (compile-time) or Reflection (runtime).

Although an exact solution is not clear because you seem to have a very complex code scenario here, the two examples can address the problem your having :

First method: By using interfaces.

1) Convert "Something" into an interface 2) Take the implmentation methods from and port them to a class SomethingImpl 3) Just have "abcde12345_something" Implement the "Something" interface.

2nd method: By using introspecting the classes.

This is a somewhat strange approach, but if interfaces won't work, you can add more hooks to your class that allow it to describe, to other objects, what type it is derived from.

1) It is clear that you are "wrapping" classes - so , you can add an API to your wrapped classes, such that each one provides a "getRootClass()" method, like this :

public class abcde_12345_something extends Something implements RootClassProvider{
    public Class getRootClass()
    {
         return Something.class;
    }
}

2) Now, at runTime - if you can't (for some reason) when you need to do special logical operations on "Something" objects, you can call

if(myabcd_12345.getRootClass().isAssignableFrom(Something))
    Something s = (Something) myabcd_12345; 

I think the best way to do what they are intending to do is to use packages, instead of cluttering the class names... I'd suggest that to your teacher if I were you:

ie surname_firstname.exerciseN.ClassName instead of surname_firstname_exerciseN_className.java

No, this isn't possible in Java.

Extending won't work and aliasing such as it seems you want doesn't exist in Java.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM