整数四个字节拆分合并
package day0703; import java.util.Arrays; import java.util.Scanner; public class Test1 { public static void main(String[] args) { System.out.print("输入整数:"); int i = new Scanner(System.in).nextInt(); IntByte obj = new IntByte(i); System.out.println(obj.getInt()); System.out.println(Arrays.toString(obj.getBytes())); IntByte obj2 = new IntByte(obj.getBytes()); System.out.println(obj2.getInt()); } } package day0703; import java.util.Arrays; public class IntByte { int n; byte[] bytes; public IntByte(byte[] a) { int r = 0; r = r | ((a[0]&0x000000FF)<<24); r = r | ((a[1]&0x000000FF)<<16); r = r | ((a[2]&0x000000FF)<<8); r = r | ((a[3]&0x000000FF)<<0); this.n = r; this.bytes = a; } public IntByte(int n) { this.n = n; this.bytes = new byte[4]; int num = 24; for (int i =0;i<this.bytes.length;i++) { this.bytes[i] = (byte)(n>>num); num-=8; } } public int getInt() { return n; } public byte[] getBytes() { return Arrays.copyOf(bytes,4); } }
学生
package day0704; public class Student { int id; String name; String gender; int age; public Student() { } public Student(int id, String name, String gender, int age) { this.id = id; this.name = name; this.gender = gender; this.age = age; } public Student(int id, String name, String gender) { this(id,name,gender,0); } public Student(int id, String name) { this(id,name,null); } public String toString() { return id+","+name+","+gender+","+age; } } package day0704; public class Test1 { public static void main(String[] args) { Student s1 = new Student(); Student s2 = new Student(3,"张三"); Student s3 = new Student(4,"李四","男"); Student s4 = new Student(5,"王五","女",18); System.out.println(s1); System.out.println(s2); System.out.println(s3); System.out.println(s4); } }
This关键字
This.xxx this特殊引用,引用当前对象的内存地址
This(…):构造方法之间调用
方法重载overload
同名不同参
println()
println(int)
println(double)
println(char)
println(String)
println(…)
继承
代码复用
单继承:一个类只能继承一个父类
构造方法不继承
私有成员不继承
Person
package day0705; public class Test1 { public static void main(String[] args) { Person person = new Person("张三", 22,"男"); Student student = new Student(); Employee employee = new Employee(); student.name = "李四"; student.gender = "女"; student.age = 18; student.school = "家里蹲大学"; employee.name = "王五"; employee.gender = "女"; employee.age = 17; employee.salary = 12000; System.out.println(person.toString()); System.out.println(student.toString()); System.out.println(employee.toString()); } } package day0705; public class Person { String name; int age; String gender; public Person() { } public Person(String name, int age, String gender) { this.name = name; this.age = age; this.gender = gender; } public String toString() { return name+","+gender+","+age; } } package day0705; public class Student extends Person { String school; } package day0705; public class Employee extends Person { double salary; }
父类构造方法
先创建父类对象时,也会先执行父类的构造方法
默认执行无参构造方法
package day0705; public class Test2 { public static void main(String[] args) { new Student(); new Student("张三",22,"男","学校"); } } package day0705; public class Student extends Person { String school; public Student() { System.out.println("student无参"); } public Student(String name, int age, String gender, String school) { super(name, age, gender); this.school = school; System.out.println("shcool全参"); } } package day0705; public class Person { String name; int age; String gender; public Person() { System.out.println("person无参"); } public Person(String name, int age, String gender) { this.name = name; this.age = age; this.gender = gender; System.out.println("person全参"); } public String toString() { return name+","+gender+","+age; } }