问题
I am very very new to java and trying to extend File class to add some more methods and properties to it with below class
import java.io.File;
public class RecordingFile extends File {
private static final long serialVersionUID = -4774703382591639948L;
String RecordingNameFormatted;
String RecordingDurationFormatted;
String RecordingSizeFormatted;
String RecordingDateFormatted;
String RecordingBitrateFormatted;
public RecordingFile(String path) {
super(path);
File f = new File(path);
RecordingNameFormatted = f.getName().replace(".mp3", "");
RecordingDurationFormatted = Utils.millisecondsToHours(Utils.getMp3DurationPureJava(f));
RecordingSizeFormatted = Utils.humanReadableByteCount(f.length(), true);
RecordingDateFormatted = Utils.longToDate(f.lastModified());
RecordingBitrateFormatted = Utils.getBitratePureJava(f) +"Kbps";
}
}
Above doesn't work properly. I am not even sure if
File f = new File(path);
supposed to be there. For example when I try this
ArrayList<RecordingFile> ret = new ArrayList<RecordingFile>();
RecordingFile filesDir = new RecordingFile(path);
if (!filesDir.exists()) {
filesDir.mkdirs();
}
RecordingFile[] list = filesDir.listFiles();
Eclipse tells me that "Type mismatch: cannot convert from File[] to RecordingFile[]" How can i just extend File class by adding couple more properties/methods to it?
回答1:
None of the builtin things know about your class, so they can't possibly return your new types. what i think you're looking for is probably extension methods which don't exist in Java yet.
Java equivalent to C# extension methods
In order to use your stuff, you'd have to make a method that, given any collection of File objects, returns your new type. You'd then have to call that everywhere you want to use your type instead.
so that line would look like
RecordingFile[] list = convertToRecordingFiles(filesDir.listfiles());
i leave the implementation of convertToRecordingFiles as an excercise :)
回答2:
If you are extending File
then your class instances are also File
s. Calling the super constructor is enough, and then this will have all the public and protected fields & methods of File plus whatever you add.
回答3:
The issue is that when you call listFiles(), you are calling the File method listFiles(), which returns an array of the File type. Because of inheritance, your RecordingFile type IS A File, but a File IS NOT A RecordingFile. So when you call listFiles now, there is no issue since it returns an array of RecordingFiles which ARE Files, but the methods specifies that they are the File type.
As suggested above, you could write a utility method that converts Files to RecordingFiles.
Alternatively, you could just cast the results of the method call, since the File that it returns are in fact RecordingFiles. So change the line to: RecordingFile[] list = (RecordingFile[])filesDir.listFiles();
The cleanest choice, in my opinion, would be to overload the method on your new class: Give RecordingFile a method listFiles() that returns a list of RecordingFiles (tip: you can write the method exactly how I specified above).
回答4:
In terms of listing a (virtual)filesystem in a treeview I had to override the File class.. So I choosed John's answer to solve it. Here is some help for people who had never overriden a class before.
@Override
public MyFile[] listFiles() {
File[] theFiles = super.listFiles();
if (theFiles == null || theFiles.length == 0) return null;
MyFile[] myFiles = new MyFile[theFiles.length];
for (int i = 0; i < theFiles.length; i++) {
myFiles[i] = new MyFile(theFiles[i].getAbsolutePath());
}
return myFiles;
}
来源:https://stackoverflow.com/questions/11513327/java-myfile-extends-file