刚刚学java不久,之前看了下java在socket通信,文件读写等都可以实现read(byte[], int offset, int length)的方法,一直都在研究是怎么实现的,今天学了下并进行了实现,
代码如下:
package thisandsuper;
class Person
{
String name;
public int age = 10;
public byte[] datas;
Person()
{
System.out.println("调用父类无参数构造类" + age) ;
}
Person(String name, int age)
{
this.name = name;
this.age = age;
System.out.println("调用父类有参数构造类") ;
onPerson(name, age);
}
public void onPerson(String name, int age)
{
System.out.println("father onPerson:" +"age:"+ age + " name "+ name);
}
public int read(byte[] bytes)
{
int i;
int length = name.length();
byte[] buffer = new byte[length];
buffer = name.getBytes();
for(i = 0; i < length; i++)
{
bytes[i] = buffer[i];
}
return length;
}
}
class Student extends Person
{
public int age = 20;
Student()
{
System.out.println("调用子类无参数构造类" + age) ;
}
Student(String name, int age)
{
super(name, age);
System.out.println("调用子类有参数构造类") ;
}
public void onPerson(String name, int age)
{
System.out.println("child onPerson:" +"age:"+ age + " name "+ name);
}
}
public class MyTest
{
public static void main(String[] args)
{
Student s1 = new Student();
Student s2 = new Student("li xiangxing",15);
byte[] buffer = new byte[1024];
int length = s2.read(buffer);
System.out.println("read:" + length + " " + new String(buffer));
}
}
运行结果如下:
调用父类无参数构造类10
调用子类无参数构造类20
调用父类有参数构造类
child onPerson:age:15 name li xiangxing
调用子类有参数构造类
read:12 li xiangxing