java下自定义read方法的实现并可以继承(转)
转载:http://blog.csdn.net/cen616899547/article/details/7498099
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)); } }