10年期中java

 

/********************类的构造方法*************************/

又译构造器。

一个类可以有很多构造方法,同名不同参,这种特征叫重载overloading(重载不仅限与构造方法,也可用于其他方法。但是不可以有两个名字相同、参数类型也相同却返回不同类型的方法)。

指出方法名及参数类型,叫做方法的签名(signature)。

构造方法就是对类的一个初始化,如果没有该方法则会被自动地赋为默认值:数值为0,布尔值为false,对象引用为null。

默认构造器——没有参数的构造器

/********************接口******************************/

 接口就像一个抽象类,但它不是类。

很多同学都问过为什么要接口。

通过接口,我们可以定义一些通用类。就比如datameasure 只要写一个总的方法就可以了。不过接口需要继承,实现,被重写。

讲的详细一点吧,现在有老师,学生,教室,等类,每个类都有自己可以测量的内容,比如老师就测年龄,学生就测成绩,教室就测面积等等。定义一个measure的接口。在各个类中实现返回要测量的变量值。然后我们就可以定义一个通用的datameasure,随便传入学生或者老师,都可以直接进行测量。

 

 

 

 

附上期中作业

class Student implements Measurer
{//构造名为学生的类
/**************************成员变量*************************/

private String name;//姓名
private int age;//年龄
private char sex;//性别
private int ID;//学号,程序自动为对象编号
private String profession;//专业
private double score;//成绩
static int nextID=1;
/*******************************构造方法*************************/
public Student()//默认构造器
{
}

public Student(String name,int age,char sex,String profession,double score)
{//构造方法2
this.name=name;
this.age=age;
this.sex=sex;
this.profession=profession;
this.score=score;
this.ID=this.nextID;
this.nextID++;
}
/*******************************方法******************************/
public String getName()
{//返回名字
return name;
}
public int getAge()
{//返回年龄
return age;
}
public char getSex()
{//返回性别
return sex;
}
public String getProfession()
{//返回专业
return profession;
}
public double getScore()
{//返回成绩
return score;
}
public int getID()
{//返回ID
return ID;
}
public void setName(String x)
{//重设姓名
name=x;
}
public void setSex(char x)
{//重设性别
sex=x;
}
public void setProfession(String x)
{//重设专业
profession=x;
}
public void setScore(double x)
{//重设成绩
score=x;
}
/**************************equals方法************************/
public boolean equals(Object obj)
{
if(this==obj) return true;
if(obj==null) return false;
if(getClass()!=obj.getClass()) return false;
Student other=(Student)obj;
return (name==other.name && sex==other.sex && age==other.age && profession==other.profession && score==other.score);
}
/***********************toString方法*************************/
public String toString()
{
return name+",年龄"+age+",性别"+sex+",专业是"+profession+",成绩"+score+",学号为"+ID;
}
public double measure()
{
return score;
}
/************************将对象以文本方式写入磁盘文件方法,并从文件中将对象读出方法************/
/**写方法*/
/**采用分隔格式存储Student记录的一个数组;
-每条记录使用单独的一行存储;
-不同的成员变量间由分隔符分开;以"|"作为分隔符;*/

public void writeData1(PrintWriter out) throws IOException
{
out.println(name + "|"+ age + "|"+ sex+ "|"+profession+ "|"+ score+"|"+ID);
}
/**读方法*/
public void readData1(BufferedReader in) throws IOException
{
String s = in.readLine();
StringTokenizer t = new StringTokenizer(s, "|");
name = t.nextToken();
age= Integer.parseInt(t.nextToken());
sex= (t.nextToken()).charAt(0);
profession=t.nextToken();
score=Double.parseDouble(t.nextToken());
ID= Integer.parseInt(t.nextToken());
}

/********将一组对象以二进制方式写入磁盘文件方法,并用随机存取的方式从文件中读出方法***********/
/**写方法*/
public void writeData2(DataOutput out) throws IOException
{
DataIO.writeFixedString(name, NAME_SIZE, out);
out.writeInt(age);
out.writeChar(sex);
DataIO.writeFixedString(profession,PROFESSION_SIZE,out);
out.writeDouble(score);
out.writeInt(ID);
}
/**读方法*/
public void readData2(DataInput in) throws IOException
{
name = DataIO.readFixedString(NAME_SIZE, in);
age =in.readInt();
sex =in.readChar();
profession=DataIO.readFixedString(PROFESSION_SIZE,in);
score = in.readDouble();
ID= in.readInt();
}
public static final int NAME_SIZE = 40;
public static final int PROFESSION_SIZE = 80;
public static final int RECORD_SIZE =2*NAME_SIZE + 4 +2+2*PROFESSION_SIZE + 8+ 4;
}
class DataIO
{//自定类DataIO实现定长字符串的输入与输出;
public static String readFixedString(int size, DataInput in)
throws IOException
{ /**
-方法从输入流中读取字符,直到读取了size个字符
或者遇到一个0值的字符为止,并跳过输入域中其他的0值;
*/
StringBuilder b = new StringBuilder(size);
int i = 0;
boolean more = true;
while (more && i < size)
{
char ch = in.readChar();
i++;
if (ch == 0) more = false;
else b.append(ch);
}
in.skipBytes(2 * (size - i));
return b.toString();
}

public static void writeFixedString(String s, int size, DataOutput out)
throws IOException
{
/**实现字符串定长写入
-方法以参数size指定字符串写入的长度,如果不够,以0补充;*/
for (int i = 0; i < size; i++)
{
char ch = 0;
if (i < s.length()) ch = s.charAt(i);
out.writeChar(ch);
}
}
}
/*****************************接口****************************/
interface Measurer
{
double measure();
}
/*****************************DataSet方法*********************/
class DataSet
{
public DataSet()
{//DataSet的构造方法
sum = 0;
count = 0;
maximum = null;
}
public void add(Measurer x)
{//求和,同时记录最大值
sum = sum + x.measure();
if (count == 0 || maximum.measure()< x.measure())
maximum = x;
count++;
}
public double getAverage()
{//求平均
if (count == 0) return 0;
else return sum / count;
}
public Measurer getMaximum()
{//求最大值
return maximum;
}
private double sum;
private Measurer maximum;
private int count;
}
/******************************公共类********************************/
public class StudentTest
{
public static void main(String[] args)
{
Student[] student= new Student[4];
student[0]= new Student("a",19,'F',"信安",90);//相关方法的调用
student[1]= new Student("b",20,'M',"计科",89);
student[2]= new Student("c",19,'F',"智能",90);
student[3]= new Student("d",20,'M',"网工",91);

DataSet data= new DataSet();

data.add(student[0]);
data.add(student[1]);
data.add(student[2]);
data.add(student[3]);

System.out.println("平均分=" + data.getAverage());
System.out.println("最高分为"+data.getMaximum());
System.out.println(student[0].toString());
System.out.println(student[1].toString());
System.out.println(student[2].toString());
System.out.println(student[3].toString());
System.out.println("1号和4号相同?"+student[0].equals(student[3]));

try
{
/****************实现将一组对象以文本方式写入磁盘文件中,并从文件中将对象读出,在屏幕上显示******************/
//把所有学生记录存入文件student1.dat
System.out.println("实现将一组对象以文本方式写入磁盘文件中,并从文件中将对象读出,在屏幕上显示");

PrintWriter out1= new PrintWriter(new FileWriter("student1.dat"));
writeData1(student, out1);
out1.close();

// 从文件student1.dat里读出学生记录
BufferedReader in1= new BufferedReader(new FileReader("student1.dat"));
Student[] newStudent1= readData1(in1);
in1.close();

// 将读出的student在屏幕上显示
for (Student e : newStudent1)
System.out.println(e);

System.out.println();

/*****实现将一组对象以二进制方式写入磁盘文件中,并用随机存取的方式从文件中以倒序读出,在屏幕上显示***********/
System.out.println("实现将一组对象以二进制方式写入磁盘文件中,并用随机存取的方式从文件中以倒序读出,在屏幕上显示");

// 以二进制方式将一组student对象写入文件student2.dat
DataOutputStream out2 = new DataOutputStream(new FileOutputStream("student2.dat"));
for (Student s: student)
s.writeData2(out2);
out2.close();

//用随机存储的方式将文件中的一组对象以倒序读出
RandomAccessFile in2= new RandomAccessFile("student2.dat", "r");
int n = (int)(in2.length() / Student.RECORD_SIZE);
Student[] newStudent2= new Student[n];
for (int i = n - 1; i >= 0; i--)
{
newStudent2[n-1-i] = new Student();
in2.seek(i * Student.RECORD_SIZE);
newStudent2[n-1-i].readData2(in2);
}
in2.close();

//在屏幕上显示读出的数组
for (Student s : newStudent2)
System.out.println(s);
}
catch(IOException exception)
{
exception.printStackTrace();
}
}
static void writeData1(Student[] students, PrintWriter out)throws IOException
{
//写入一组student对象
out.println(students.length);
for (Student s : students)
s.writeData1(out);
}
static Student[] readData1(BufferedReader in)throws IOException
{
//读入一组student对象
int n = Integer.parseInt(in.readLine());
Student[] students = new Student[n];
for (int i = 0; i < n; i++)
{
students[i] = new Student();
students[i].readData1(in);
}
return students;
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值