需要使用其他类

时间:2015-08-31 11:09:29

标签: java

我试图从另一个类中访问一个类中的String。第一节课如下:

import java.sql.*;
import javax.swing.*;
import java.text.SimpleDateFormat;
import java.util.Date;


public class UseData {
Connection connection=null;

public void usePart1Part2(String part1, String part2, String part3, String part4) {
    SimpleDateFormat format = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
    String date = format.format(new Date());

    connection = SQliteConnectionClass.dbconnector();
    try {

        String value1 = part1;   //
        String value2 = part2;   //
        String value3 = part3;   //
        String value4 = part4;   // data need to be used in GenerateSummonPDF.java
        String Timestamp = date;

value1到value4用于以下GenerateSummonPDF类:

public class GenerateSummonPDF {

    public static void main(String[] args) {
        Document document = new Document();
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\Users\\User\\workspace\\enforement system\\Summon PDF list\\Serial No.pdf"));
            document.open();
            document.add(new Paragraph("the data(value1 to value4 need to print it out here"));  //here is where i need the data
            document.close();
            writer.close();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

提前感谢您的帮助。

4 个答案:

答案 0 :(得分:5)

您可以使用吸气剂。以下是作为UseData方法添加的getter的演示:

        public class UseData {

        private String value1;

        public void usePart1Part2(String part1, String part2, String part3, String part4)
        {
            value1 = part1;
        }

        /**
         * Getter for value1
         * @return the value1
         */
        public String getValue1() {
            return value1;
        }

    }

至于从其他类调用getter:

    public class GenerateSummonPDF {

        /**
         * @param args
         */
        public static void main(String[] args) {

            UseData usedata = new UseData();
            String value1 = usedata.getValue1();
        }

    }

答案 1 :(得分:1)

    public class UseData {
    private String value1 ;
    private String value2 ;
    private  String value3 ;

// getter- setter for value1 , value2 , value3
// rest of the code.

}

now add modify GenerateSummonPDF as follows :

    public class GenerateSummonPDF {

public static void main(String[] args)
   {
      Document document = new Document();
      try
      {


 PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\Users\\User\\workspace\\enforement system\\Summon PDF list\\Serial No.pdf"));
         document.open();

          UseData useData = new UseData ();
          useData.usePart1Part2("part1Value" , "part2Value" ,"part3Value" ,"part4Value") ;        

         document.add(new Paragraph("the data("+useData.getValue1()+useData.getValue2()+useData.getValue3()+useData.getValue4()+"));  //here is where i need the data


         document.close();
         writer.close();
      } catch (DocumentException e)
      {
         e.printStackTrace();
      } catch (FileNotFoundException e)
      {
         e.printStackTrace();
      }
   }

然后在你的下一个类中创建对象并从这些方法中获取值 像这样的事情

public class usedata { 
   Connection connection=null;
   String value1;   //
   String value2;   //
   String value3;   //
   String value4;  
public void usePart1Part2(String part1, String part2, String part3, String part4)
{
    Date curDate = new Date();
    SimpleDateFormat format = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
    String DateToStr = format.format(curDate);

    connection= SQliteConnectionClass.dbconnector();
try {

   value1 = part1;   //
   value2 = part2;   //
   value3 = part3;   //
   value4 = part4;   // data need to be used in GenerateSummonPDF.java
   String Timestramp = DateToStr;
}

public String getValue1()
{
   return value1;    
} 

public String getValue2()
{
  return value2;    
} 

public String getValue3()
{
  return value3;    
} 

public String getValue4()
{
  return value4;    
}

我希望你能从中得到。 快乐学习java

答案 2 :(得分:0)

type

答案 3 :(得分:0)

import java.sql.*;
import javax.swing.*;
import java.text.SimpleDateFormat;
import java.util.Date;


public class usedata {
public String part1;
public String part2;
public String part3;
public String part4;
Connection connection=null;

public void usePart1Part2(String part1, String part2, String part3, String part4)
{
this.part1 = part1
this.part2 = part2
this.part3 = part3
this.part4 = part4
Date curDate = new Date();
SimpleDateFormat format = new SimpleDateFormat("dd-M-yyyy hh:mm:ss");
        String DateToStr = format.format(curDate);
connection= SQliteConnectionClass.dbconnector();
try {

   String value1 = part1;   //
   String value2 = part2;   //
   String value3 = part3;   //
   String value4 = part4;   // data need to be used in 
   String Timestramp = DateToStr;
....

public class GenerateSummonPDF {

public static void main(String[] args)
   {
      userdata ud = new userdata();
      //you can't acces to the other class attribut
      System.out.println(ud.part1 + ud.part2 + ud.part3 + ud.part4);
      Document document = new Document();
      try
      {
     PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\Users\\User\\workspace\\enforement system\\Summon PDF list\\Serial No.pdf"));
     document.open();
     document.add(new Paragraph("the data(value1 to value4 need to print it out here"));  //here is where i need the data
     document.close();
     writer.close();
      } catch (DocumentException e)
  {
     e.printStackTrace();
  } catch (FileNotFoundException e)
  {
     e.printStackTrace();
  }
   }