-7

我需要在方法 openLog 之外声明 PrintWriter 以便我可以从多个方法访问它,因为这样我只能在一个方法内访问 PrintWriter,但是我不能从其他方法访问它!

package com.donemanuel.DSDK;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;


public class LogKit {
    void openLog() throws IOException{

    Date ltm = new Date( );
    SimpleDateFormat lt = new SimpleDateFormat ("'['dd.MM hh:mm:ss a']: '");
    final String logtm = lt.format(ltm);
    PrintWriter logd = new PrintWriter("res/LOGTIME_"+logtm, "UTF-8");

    String prefix = "[Logger]:";

    logd.println(prefix + "DSDK Logger opened!"); 

    logd.println("----------xXx----------");
    logd.flush();

}
void custommessage(String logmsg){
    logd.println(logmsg); //I want to print custom messages with my API, but log is declared in another void so thats the problem.
    //If i would declare logd (printwriter) outside a void it would give me an error!
}
}
4

4 回答 4

0

在 java 中,您可以通过以下方式声明类的“变量”(称为“属性”或“字段”):

public class YourClass {
  PrintWriter logd = new PrintWriter("path/file", "UTF-8");


  void openLog() throws IOException{
     //your code that uses "logd" here
  }

  void custommessage(String logmsg){
     //your other code that uses "logd" here
  }
}

有关类和类构造函数的属性初始化的更多信息,请尝试将 google 与这些关键字一起使用。

问候。

于 2013-06-17T09:36:17.170 回答
0

你说的“虚空”是什么意思?

在上面的代码中,您无法从“custommessage(String)”方法访问 logd 变量,因为范围内没有具有此名称的变量。如果您的问题是访问与您在“openLog()”方法中定义的同名变量,只需创建一个新属性。使用 logd 变量引用的对象设置此属性,然后将此属性用于“custommessage(String)”。

样品:

public class AttributeSample {
  private Object myAttribute = null;

  public void init() {
    myAttribute = new Object();
  }

  public void doSomething() {
    if (myAttribute != null) {
      System.out.println(myAttribute);
    } else {
      throw new RuntimException("Oups ! You must call init() before.");
    }
  }
}
于 2013-06-17T09:37:12.673 回答
0

在您的情况下,问题是您正在尝试以可能引发异常的方式初始化字段。您需要提供一些有关如何处理此异常的上下文。

如果不是异常,您可以简单地初始化字段,例如:

public class LogKit {
    private int i = 0; // this initialises fine

    void openLog() {
        ...
    }
}

但是,构造函数PrintWriter抛出异常。所以运行它的代码必须要么捕获异常,要么声明它被抛出。目前您正在您的openLog方法中创建 PrintWriter ,并且此方法声明throws IOException了 ,因此编译器很高兴。但是,当您将其移出时,则没有此类条款。

带有初始化器的字段实际上是在(自动生成的)构造函数中运行的。我上面给出的示例实际上与以下内容相同:

public class LogKit {
    private int i;

    public LogKit() {
        i = 0;
    }

    void openLog() {
        ...
    }
}

所以解决这个问题的一种方法是自己声明构造函数,它可以让你指定它可能会抛出异常:

public class LogKit {
    PrintWriter logd;

    public LogKit() throws IOException {
        Date ltm = new Date( );
        SimpleDateFormat lt = new SimpleDateFormat ("'['dd.MM hh:mm:ss a']: '");
        logd = new PrintWriter("res/LOGTIME_"+logtm, "UTF-8");
    }
}

这可能是最好的方法 - 通过在构造函数中创建编写器,可以保证在类的整个生命周期中定义它。

如果由于某种原因,您无法处理引发异常的构造函数,那么您需要将字段的创建推迟到以后。在这种情况下,引用的每一位代码logd都必须记住,null如果它还没有被分配,它可能是。custommessage如果之前调用它应该怎么做openLog?你需要考虑类的不同状态,这就是为什么这更复杂。但在某些情况下,这种“生命周期”方法是唯一可行的方法。

于 2013-06-17T10:14:07.560 回答
-1

如下更改您的代码

package com.donemanuel.DSDK;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;


public class LogKit {
 PrintWriter logd ;
void openLog() throws IOException{

Date ltm = new Date( );
SimpleDateFormat lt = new SimpleDateFormat ("'['dd.MM hh:mm:ss a']: '");
final String logtm = lt.format(ltm);
logd = new PrintWriter("res/LOGTIME_"+logtm, "UTF-8");

String prefix = "[Logger]:";

logd.println(prefix + "DSDK Logger opened!"); 

logd.println("----------xXx----------");
logd.flush();

}
void custommessage(String logmsg)  throws IOException{
logd.println(logmsg); //I want to print custom messages with my API, but log is declared in another void so thats the problem.
//If i would declare logd (printwriter) outside a void it would give me an error!
}
}
于 2013-06-17T09:38:58.573 回答