1. 程式人生 > >一個例子將java多型搞定

一個例子將java多型搞定

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;
	}
	@Override
	public String toString() {
		return name + "," + age;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}
	@Override
	public 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;
	}

	@Override
	public 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;
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + day;
		result = prime * result + month;
		result = prime * result + year;
		return result;
	}
	@Override
	public 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;
	}
	@Override
	public String toString() {
		return year + "," + month + "," + day;
	}
	
}