一个例子将java多态搞定

来源:互联网 发布:找物流专线软件 编辑:程序博客网 时间:2024/06/06 02:45

java多态分为编译时多态和运行时多态


1)编译时多态:看的是类型,看该引用的类型中有没有被访问的成员


2)运行时多态:看的是内存,new 谁调谁


总的来讲,就是编译看左边,运行看右边


例子:

1,main方法使用四个例子说明多态性质

public class Main {public static void main(String[] args) {// 1,典型的多态使用Person p1 = new Person("Jerome", 20);Student s1 = new Student("Sam", 21, "捡破烂");Person p2 = new Student("Alen", 22, "打酱油");System.out.println("p1:" + p1);System.out.println("p2:" + s1);System.out.println("s1:" + p2);System.out.println("*************************");// 2,对象数组的打印Object[] objs = { new Person("Jack", 23), new Student("Riee", 21, "搬砖"), new MyDate(2017, 6, 21) };print(objs);// 打印对象数组System.out.println("*************************");// 3,对象数组的查找// 这里只判断名字是否相同,如果名字相同就认为是同一个人Person p3 = new Person("Jack", 12);indexOf(objs, p3);System.out.println("***************************");// 4,对象数组的合并Object[] objs2 = { new Person("Jreo", 23), new Student("Sunny", 21, "搬砖"), new MyDate(2016, 6, 21), null };Object[] objs3;objs3 = combine(objs, objs2);print(objs3);System.out.println("Over...");// 到这里程序全部运行完毕}private static Object[] combine(Object[] objs, Object[] objs2) {// 判断空指针与数组长度为0 两个条件的顺序不能颠倒if(objs==null || objs.length==0){return objs2;}if(objs2 == null || objs2.length==0){return objs;}//经过前面的判断,到这里两个对象数组都不为空Object[] objs3 = new Object[objs.length+objs2.length];for (int i = 0; i < objs.length; i++) {objs3[i] = objs[i];}int k = objs.length;for (int i = 0; i < objs2.length; i++) {objs3[k++] = objs2[i];}return objs3;}private static void indexOf(Object[] objs, Person p) {if (objs == null || p == null) {System.out.println("没找到~");return;}for (Object object : objs) {if (object.equals(p)) {System.out.println("找到~");return;}}System.out.println("没找到~");}private static void print(Object[] objs) {// 写代码时,只要遇到访问"对象中的成员"或"数组中的元素",都要先进行空指针防护// 注意判断空指针与数组长度为0 两个条件的顺序不能颠倒if (objs == null || objs.length == 0) {System.out.println("对象数组为空或者长度为零");return;}// 这是多态的好处,我们并不需要知道对象数组里的是什么,// 反正都是一个地址,java会帮助我们找到相应的toString() 方法for (Object object : objs) {System.out.println(object);}}}


最后main 方法打印的是:

p1:Jerome,20
p2:Sam,21,捡破烂
s1:Alen,22,打酱油
*************************
Jack,23
Riee,21,搬砖
2017,6,21
*************************
找到~
***************************
Jack,23
Riee,21,搬砖
2017,6,21
Jreo,23
Sunny,21,搬砖
2016,6,21
null
Over...


2,Person类

public class Person {private String name;private int age;public Person(String name, int age) {super();this.name = name;this.age = age;}public Person() {super();}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return name + "," + age;}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + ((name == null) ? 0 : name.hashCode());return result;}@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;/*if (getClass() != obj.getClass())-----这是精确匹配return false;*/if(!(obj instanceof Person)){//---------这是渐进匹配return false;}Person other = (Person) obj;if (name == null) {if (other.name != null)return false;} else if (!name.equals(other.name))return false;return true;}}

3,Student类继承Person类

public class Student extends Person{private String speciality;//专业public Student(String name, int age, String speciality) {super(name, age);this.speciality = speciality;}public Student() {super();// TODO Auto-generated constructor stub}public Student(String name, int age) {super(name, age);// TODO Auto-generated constructor stub}public String getSpeciality() {return speciality;}public void setSpeciality(String speciality) {this.speciality = speciality;}@Overridepublic String toString() {return super.toString()+","+ speciality;}}

4,MyDate类用来区别Person类和Student类

public class MyDate {private int year;private int month;private int day;public MyDate(int year, int month, int day) {super();this.year = year;this.month = month;this.day = day;}public MyDate() {super();// TODO Auto-generated constructor stub}public int getYear() {return year;}public void setYear(int year) {this.year = year;}public int getMonth() {return month;}public void setMonth(int month) {this.month = month;}public int getDay() {return day;}public void setDay(int day) {this.day = day;}@Overridepublic int hashCode() {final int prime = 31;int result = 1;result = prime * result + day;result = prime * result + month;result = prime * result + year;return result;}@Overridepublic boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;MyDate other = (MyDate) obj;if (day != other.day)return false;if (month != other.month)return false;if (year != other.year)return false;return true;}@Overridepublic String toString() {return year + "," + month + "," + day;}}


原创粉丝点击