Java多态

向上转型与向下转型

重点:向下转型要加强转,而且要加instanceof判断


请看下面这段代码:

public class Main {
	public static void main(String args[]) {
		Animal a = new Dog();//子传父,向上转型无需强转
		a.show();

		if(a instanceof Dog) {
			Dog b = (Dog)a;//父传子,向下转型需要强制转化
			b.show();
		}

		Animal c = new Cat();
		c.show();
		
		Dog d = (Dog)c;
		d.show();//如果不加判断,编译可以通过,但是会出现RE错误
		
		//instanceof 的作用:当需要向下转型的时候无法判断两个对象的匹配是否兼容,这是需要使用操作符进行判断
	}
}

class Animal {
	public void show() {
		System.out.println("Animal Object");
	}
}

class Dog extends Animal {
	public void show() {
		System.out.println("Dog Object");
	}
}

class Cat extends Animal {
	public void show() {
		System.out.println("Cat Object");
	}
}
会出现RE错误:


观察下面的修改后的代码(14---18行),即可解决问题:

public class Main {
	public static void main(String args[]) {
		Animal a = new Dog();//子传父,向上转型无需强转
		a.show();

		if(a instanceof Dog) {
			Dog b = (Dog)a;//父传子,向下转型需要强制转化
			b.show();
		}

		Animal c = new Cat();
		c.show();

		/*在此处添加*/
		if(c instanceof Dog){
			Dog d = (Dog)c;
			d.show();//如果不加判断,编译可以通过,但是会出现RE错误
		}
		//instanceof 的作用:当需要向下转型的时候无法判断两个对象的匹配是否兼容,这是需要使用操作符进行判断
	}
}

class Animal {
	public void show() {
		System.out.println("Animal Object");
	}
}

class Dog extends Animal {
	public void show() {
		System.out.println("Dog Object");
	}
}

class Cat extends Animal {
	public void show() {
		System.out.println("Cat Object");
	}
}

成功解决问题:




多态的方法与属性

1.属性不参加多态

2.静态方法不参与多态

3.关于向上与向下转型的多态见下:

public class Main {
	public static void main(String args[]) {
		Cat cat = new Cat();
		cat.m = 20;//父类的Animal中的m属性被隐藏
		System.out.println(cat.getM());//120 
		System.out.println(cat.getSuperM());//调用父类中被隐藏的属性0
		System.out.println(cat.seeM());//0
		System.out.println(cat.getN());//33

		Animal animal = cat;//向上转型
		cat.m = 20;
		animal.m = -100;//属性无法参加多态,因此Cat中的m=20;Animal中的m=-100
		System.out.println(animal.getM());//120
		System.out.println(animal.seeM());//-100
		System.out.println(animal.getM());//120
	}
}

class Animal {
	int m;
	int n=33;
	int getM() {return m;}
	int seeM() {return m;}
	int getN() {return n;}
	static void show() {//静态方法无法被重写
		System.out.println("我是Animal的静态方法");
	}
}

class Cat extends Animal {
	int m;//父类的m被隐藏
	int a=10;
	int getM() {return m+100;}
	int getA() {return a;}
	int getSuperM() {return super.m;}
}

输出结果:



        这究竟是为什么呢?上课好好听的童鞋一定不会问这种问题,orz,只需要记住属性不参与多态,但是属性会被隐藏,例如上面的代码中Cat中出现了与父类Animal同名属性m,那么如果我定义一个Cat类对象输出m,这个m就是Cat类的m而从Animal中继承的m将会被隐藏;方法要参与多态,注意向上转型与向下转型后调用的是谁的方法,然后注意这个方法使用的属性属于谁就好。


多态的应用

abstract抽象类(类的层次化),只能被继承,不能产生实例对象


对抽象类的抽象方法要完全重写,不然无法产生实例

//抽象类,不能产生对象,只能被继承
abstract class Animal {
	abstract void show();
	abstract void display();
	void print(){}//抽象类中可以有普通方法
}

class SmallAnimal extends Animal {
	void show() {//对抽象类的抽象方法未继承完全,编译出错
		System.out.println("I am a animal");
	}
	
}

像这样改就对了:

//抽象类,不能产生对象,只能被继承
abstract class Animal {
	abstract void show();
	abstract void display();
	void print(){}//抽象类中可以有普通方法
}

class SmallAnimal extends Animal {
	//对抽象类的抽象方法全部重写
	void show() {
		System.out.println("I am a animal");
	}
	void display() {
		System.out.println("I am a small animal")
	}
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水能zai舟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值