在java中使用instanceof进行计数
文件系统类-用于测试在java中使用instanceof进行计数,java,loops,arraylist,instanceof,Java,Loops,Arraylist,Instanceof,文件系统类-用于测试 public abstract class AbstractFile { // instance variables - String name; public abstract int size(); public abstract int getNumFiles(); public abstract int getNumFolders(); public abstract AbstractFile find(String name); public String g
public abstract class AbstractFile
{
// instance variables -
String name;
public abstract int size();
public abstract int getNumFiles();
public abstract int getNumFolders();
public abstract AbstractFile find(String name);
public String getName(){
return name;
}
}
文件类
import java.util.ArrayList;
public class Folder extends AbstractFile
{
//Replace previous ArrayLists with a single
//ArrayList of AbstractFile references
private ArrayList<AbstractFile> files = new ArrayList();
AbstractFile abstractfile;
/**
* Constructor for objects of class Folder
*/
public Folder(String name)
{
super();
this.name = name;
}
// replace previous add methods
// with a single add(AbstractFile fileObject) method
public boolean add(AbstractFile fileObject)
{
return files.add(fileObject);
}
@Override
public int size()
{
int size =0; // size holds the running total
for (AbstractFile file : files){ // for each AbsFile ref
size+=file.size(); //call size() and update the running total
}
return size; // return the final value
}
@Override
public int getNumFiles(){
int numFiles = 0;
for(AbstractFile file: files)
{
numFiles += file.getNumFiles();
}
return numFiles;// default value
}
@Override
public int getNumFolders(){
int numFolders = 0;
// for(AbstractFile file: files)
// {
// if(file instanceof Folder)
// {
// numFolders += file.getNumFolders();
// }
// }
// return numFolders;// default value
for (Object e : files)
{
if (e instanceof Folder)
{
numFolders += e.getNumFolders();
}
}
return numFolders;// default value
}
@Override
public AbstractFile find(String name){
//TODO Later - not in mini assignment
return null;
}
}
public class FileSystem
{
public static void main(String[] args)
{
FileSystem fileSystem = new FileSystem();
fileSystem.fileTest1();
}
public void fileTest1(){
Folder documents = new Folder("Documents");
Folder music = new Folder("Music");
Folder photos = new Folder("Photos");
documents.add(music);
documents.add(photos);
File assign1 = new File("assign1.doc");
documents.add(assign1);
Folder dylan = new Folder("Dylan");
music.add(dylan);
Folder band = new Folder("The Band");
music.add(band);
File family = new File("family.jpg");
photos.add(family);
File tambourine = new File("tambourine.mp3");
dylan.add(tambourine);
File dixie = new File("dixie.mp3");
band.add(dixie);
File weight = new File("weight.mp3");
band.add(weight);
String contents1 = ("Hey, mister, can you tell me ");
String contents2 = ("Hey Mr Tambourine Man");
String contents3 = ("The night they drove old dixie down");
String contents4 = ("fee fi fo fum");
weight.setContents(contents1); // add contents to each File
tambourine.setContents(contents2);
dixie.setContents(contents3);
assign1.setContents(contents4);
//********test for size()****************
int expected = contents1.length() + contents2.length() + contents3.length() + contents4.length();
int result = documents.size();
if(result==expected){ // test fro equality
System.out.println("size() works");
}else{
System.out.println("size() doesn't work");
}
//*****************************************
//*****************test for getNumFiles()******************
expected =5;// what value should expected be set to?
result = documents.getNumFiles();
if(result==expected){ // test fro equality
System.out.println("NumFiles() works");
}else{
System.out.println("NumFiles() doesn't work");
}
//output the results of the test for equality
// **************************************
//*****************test for getNumFiles()******************
expected = 5; // what value should expected be set to?
result = documents.getNumFolders();
if(result==expected){ // test fro equality
System.out.println("NumFolder() works");
}else{
System.out.println("NumFolder() doesn't work");
System.out.printf("%d",result);
}
// **************************************
}
}
}
您好,基本上我在使用getNumFolders(方法)
时遇到了问题。我创建了一个测试类,其中包含5个文件夹,这些文件夹存储在ArrayList
类型的AbstractFile
文件中。我正在尝试循环通过此ArrayList
来计算文件夹,请记住此ArrayList
也包含文件,因此不能计算它们。
我将感谢任何帮助。
非常感谢 简单地说,如果您想计算文件夹实例的数量,那么下面就应该完成这项工作
public class File extends AbstractFile
{
private String contents;
/**
* Constructor for objects of class File
*/
public File(String name)
{
super();
this.name = name;
}
public String getContents(){
return contents;
}
public void setContents(String contents){
this.contents = contents;
}
@Override
public int size()
{
if(contents==null){ //handle situation where contents may not have been set
return 0;
}
return contents.length();
}
@Override
public int getNumFiles(){
return 1; // a File object just returns 1 (it contains one File - itself)
}
@Override
public int getNumFolders(){
//TODO
return 0;
}
@Override
public AbstractFile find(String name){
//TODO Later - not in mini assignment
return null;
}
根据您的测试用例,这里的期望值应该是2。
在测试用例的fileTest1()方法中
public int getNumFolders() {
int numFolders = 0;
for(AbstractFile file: files) {
if(file instanceof Folder) {
numFolders++;
}
}
return numFolders;
}
请将这个问题简化为一个问题,确保包括了这个问题——我猜这是一个编译时错误,对象不包含
getNumFiles()
。仅仅因为您使用了instanceof
不会更改e
的编译时类型-您需要强制转换。我添加了剩余的代码,谢谢!但您没有将其简化为一个最小的示例,也没有显示错误:(错误与e.getNumFolders()有关,getNumFolders()方法未被识别。那么这应该是问题所在-我已经在第一条评论中解释了原因。。。
expected = 2; // this should be two because there are two folders called music and photos in documents folder
result = documents.getNumFolders();