【问题标题】:Accessing a superclass arraylist in a subclass访问子类中的超类数组列表
【发布时间】:2014-08-08 06:45:44
【问题描述】:

希望我能按预期描述我的问题:我有 2 个课程; JavaApplication65(请不要质疑名称:D)和JavaApplication65 的子类Logs。现在我在 JavaApplication65 中有一个名为 temp 的 ArrayList,我想将其值复制到 Logs 中名为 allLogsText 的 Arraylist 中。我该怎么做?

package javaapplication65;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 *
 * @author MertKarakas
 */
public class JavaApplication65 {
/**
 * @param args the command line arguments
 */
Logs importer = new Logs();
private String nameOfLog;
private String wholeLog;
static ArrayList<String> temp = new ArrayList<String>();

/**
 *
 * @param nameOfLog1
 * @param wholeLog1
 */
public JavaApplication65(String nameOfLog1, String wholeLog1){
    this.nameOfLog = nameOfLog1;
    this.wholeLog = wholeLog1;
}
public JavaApplication65(){}
public static void main(String[] args) throws FileNotFoundException{
    System.out.println("Data Log V1. @author MertKarakas");
    Scanner s = new Scanner(new File("/Users/mertkarakas/Desktop/Logs/LogList.txt"));
    while (s.hasNext()){
        temp.add(s.next());
        }
    s.close();
    new Logs().menuOpt();

}
}


public class Logs {
Scanner console = new Scanner(System.in);
ArrayList<JavaApplication65> allLogs = new ArrayList<JavaApplication65>();
ArrayList<String> allLogsText = new ArrayList<String>(JavaApplication65.temp);
private String name;
public void Logs(){
    this.name = "";
}

public void menuOpt(){
    System.out.println("Menu: Write a log[log], See entered logs[see] --> Read an entered log[#], Delete a log[del], Read a specific log[spf]");
    WriteStringToFile2(allLogsText);
    choices();
    WriteStringToFile2(allLogsText);
}

public void choices(){
    String input1 = console.next();
    if ( input1.equalsIgnoreCase("log") ){
        log();
    }else if ( input1.equalsIgnoreCase("see") ){
        getLogs();
        System.out.println("Enter the index of requested log: ");
        int requestedLog = console.nextInt();
        getALog(requestedLog);
    }else if( input1.equalsIgnoreCase("del") ){

    }else if ( input1.equalsIgnoreCase("spf") ){

    }else{
        System.out.println("Wrong input, returnin to menu.");
        menuOpt();
    }
}
public void log(){
    System.out.println("Enter name of the log: ");
    String logName2 = console.next();
    System.out.println("Enter your log: ");
    String wholeLog2 = console.next();
    WriteStringToFile(logName2, wholeLog2);
    JavaApplication65 tempLog = new JavaApplication65(logName2, wholeLog2);
    allLogs.add(allLogs.size(), tempLog);
    allLogsText.add(allLogsText.size(), logName2);
    getLogs();
    System.out.println("Log successfully added.");

}

public List<String> getLogs(){
    return allLogsText.subList(0, allLogsText.size());
}
public String getALog(int k){
    String temp = allLogsText.get(k);
    return temp;
}

public void WriteStringToFile(String logNamerino, String wholeLogarino){
        try {
        String str = wholeLogarino;
        File newTextFile = new File("/Users/mertkarakas/Desktop/Logs/" + logNamerino + ".txt");

        FileWriter fw = new FileWriter(newTextFile);
        fw.write(str);
        fw.close();

    } catch (IOException iox) {
    }
}
    public void WriteStringToFile2(ArrayList<String> list){  
        try {
            for(int k =0; k < list.size(); k++){
                String str = list.get(k);
                File newTextFile = new      File("/Users/mertkarakas/Desktop/Logs/LogList.txt");
                FileWriter fw = new FileWriter(newTextFile);
                fw.write(str);
                fw.close();
            }
        } catch (IOException iox) {
    }
}

}

正如您在最后一行代码中看到的(ArrayList allLogsText = new ArrayList(temp)),它给出了一个错误,即“找不到符号”对于 temp。请帮助我...谢谢提前。更新我做了一个调整“ArrayList allLogsText = new ArrayList(JavaApplication65.temp);”我将它设为 JavaApplication65.temp,它似乎可以工作。但它会起作用吗?我的意图是制作一种日记,并使用这行代码尝试获取已写入 LogList.txt 的每个条目**

【问题讨论】:

  • 你想在这里实现什么??为什么要将温度放在 main 中?你在几个概念上是错误的。你能描述一下你的目标是什么吗?不要混合使用静态成员和实例成员。

标签: java inheritance arraylist subclass superclass


【解决方案1】:

范围很重要。

您正在访问的Listmain() 方法的本地地址。您只能在 main() 方法中访问它。

将它移到顶层,它应该是一个实例成员(并提供一个 getter 来返回它)以进行访问。

【讨论】:

  • 我这样做了,但是现在它说 main 是一个静态方法,所以 arraylist 也应该是静态的。也这样做了,但仍然给出错误“找不到符号,符号:变量临时,位置:类日志”
  • @user3141704 你能用最新的代码更新帖子吗?
【解决方案2】:

您的临时列表是 Method Local,如果您想在子类中使用属​​性,请在您的 Super 类的 class level 中声明这些属性。像这样:

public class JavaApplication65 {
    ArrayList<String> temp;
    public static void main(String[] args) throws FileNotFoundException{
    JavaApplication65 java= new JavaApplication65();    
    java.temp = new ArrayList<String>();

    }
}

public class Logs extends JavaApplication65 {
     Scanner console = new Scanner(System.in);
     ArrayList<JavaApplication65> allLogs = new ArrayList<JavaApplication65>();
     ArrayList<String> allLogsText = new ArrayList<String>(temp);
}

【讨论】:

  • 这并没有改变任何东西。它仍然给出同样的错误。
  • 找不到符号。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-24
  • 2018-10-14
  • 2013-11-24
  • 2021-05-24
  • 2015-07-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多