Java基础_自定义Java对象
自定义的对象类
package com.org.test;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import java.io.File;
import java.io.IOException;
public class Java_ObjectTest {
private int titles;
private String name;
private double scores;
public Java_ObjectTest(int titles, String name, double scores){
this.titles = titles;
this.name= name;
this.scores = scores;
}
public void Write_Msg(File file, int num) throws IOException, WriteException {
WritableWorkbook workbook = Workbook.createWorkbook(file);
WritableSheet sheet = workbook.createSheet("学生成绩", 0);//创建表名称
System.out.println(this.name);
Label labelA = new Label(num, 0, String.valueOf(this.titles));//其中num是列,r是行,创建单元格
sheet.addCell(labelA);
Label labelB = new Label(num+1, 0, this.name);//创建单元格
sheet.addCell(labelB);
Label labelC = new Label(num+2, 0, String.valueOf(this.scores));//创建单元格
sheet.addCell(labelC);
workbook.write();
workbook.close();
}
public int getTitles() {
return titles;
}
public double getScores() {
return scores;
}
public String getName() {
return name;
}
}
对象的调用:
package com.org.test;
import com.org.test.Java_ObjectTest;
import jxl.write.WriteException;
import java.io.File;
import java.io.IOException;
public class Java_RunTest {
public static void main(String[] args) throws IOException, WriteException {
int titles = 1;
String name = "张三";
double scores = 88.9;
File file = new File("D://myexcel1.xls");
Java_ObjectTest Test = new Java_ObjectTest(titles, name, scores);
Test.Write_Msg(file, 0);
}
}