多态性


对象的多态性

向上转型:自动完成

    父类     父类对象=子类对象

class A1{
	public void tell1(){
		System.out.println("A1_tell1");
	}
	public void tell2(){
		System.out.println("A1_tell2");
	}
}

class B extends A1{
	public void tell1(){
		System.out.println("B_tell1");
	}
	public void tell3(){
		System.out.println("B_tell2");
	}
}

public class PolDemo01 {

	/**
	 * @param args
	 */
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		B b = new B();
		A1 a = b; // 向上转型
		a.tell1();
		a.tell2();
	}

}



向下转型: 强制转换完成

    子类     子类对象=(子类)父类实例

class A1{
	public void tell1(){
		System.out.println("A1_tell1");
	}
	public void tell2(){
		System.out.println("A1_tell2");
	}
}

class B extends A1{
	public void tell1(){
		System.out.println("B_tell1");
	}
	public void tell3(){
		System.out.println("B_tell2");
	}
}

public class PolDemo01 {

	/**
	 * @param args
	 */
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		// 向下转型必须先发生向上转型,在发生向下转型
		A1 a = new B();
		B b = (B)a;
		b.tell1();
		b.tell2();
		b.tell3();
	}

}



对象多态性的使用

class A2{
	public void tell1(){
		System.out.println("A-tell1");
	}
}

class B2 extends A2{
	public void tell2(){
		System.out.println("B-tell1");
	}
}

class C2 extends A2{
	public void tell3(){
		System.out.println("C-tell1");
	}
}

public class PolDemo02 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		say(new B2());
		say(new C2());

	}
	// 对象的多态性使得代码简化,不同每个类都创建一个单独的方法
	public static void say(A2 a){
		a.tell1();
	}

}



instanceof 关键字

class A1{
	public void tell1(){
		System.out.println("A1_tell1");
	}
	public void tell2(){
		System.out.println("A1_tell2");
	}
}

class B extends A1{
	public void tell1(){
		System.out.println("B_tell1");
	}
	public void tell3(){
		System.out.println("B_tell2");
	}
}

public class PolDemo01 {

	/**
	 * @param args
	 */
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		A1 a = new A1();
		System.out.println(a instanceof A1);//判断a是不是A 的实例
		System.out.println(a instanceof B);
		
		A1 a1 = new B(); //向上转型后,A与B有了联系
		System.out.println(a instanceof A1);
		System.out.println(a instanceof B);
	}

}


面向对象抽象类的应用

abstract class People{
	private String name;
	private int age;
	
	public People(int age, String name){
		this.name = name;
		this.age = age;
	}
	
	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;
	}
	public abstract void want();
}

class Students extends People{
	private int score;
	public int getScore() {
		return score;
	}
	public void setScore(int score) {
		this.score = score;
	}
	public Students(int age, String name, int score){
		super(age, name);
		this.score = score;
	}
	public void want(){
		System.out.println("name:"+ this.getAge()+" "+"age:"+this.getName()
				+ "score:" + this.getScore());
	}
	
}

class Worker extends People{
	private int money;

	public int getMoney() {
		return money;
	}

	public void setMoney(int money) {
		this.money = money;
	}
	public Worker(int age, String name, int money){
		super(age, name);
		this.money = money;
	}
	public void want(){
		System.out.println("name:"+ this.getAge()+" "+"age:"+this.getName()
				+ "score:" + this.getMoney());
	}
}

public class AbsDemo01 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Students s = new Students(10, "小明", 100);
		s.want();
		
		Worker w = new Worker(30, "大明", 1000);
		w.want();

	}

}



接口的应用

interface USB{
	void start();
	void stop();
}

class Computer{
	public static void work(USB u){
		u.start();
		System.out.println("working");
		u.stop();
	}
}

class USBDisk implements USB{
	public void start(){
		System.out.println("USB is working");
	}
	public void stop(){
		System.out.println("USB is stopping");
	}
}

class Printer implements USB{
	public void start(){
		System.out.println("Printer is working");
	}
	public void stop(){
		System.out.println("Printer is stopping");
	}
}


public class InterDemo01 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Computer.work(new USBDisk());
		Computer.work(new Printer());
	}

}


极客网址:http://www.jikexueyuan.com/path/javaweb/