第10章 多态
第10章 多态
本章重点:
1、多态
2、动态绑定和静态绑定
3、超类
多态就是拥有多种形态。
在Java语言中,多态主要 是指拥有相同的形式,但不同的参数实现不同的功能。
多态的规则:
1、方法名称一定要一样。
2、传入的参数类型一定要不一样。
public class student{
	int x;
	int y;
	int z;
	
	void print(int x){
		System.out.println(2 * x);
	}
	void print(int x, int y){
		System.out.println(2 * x + y);
	}
	public static void main(String[] args){
		student st = new student();
		st.print(2);
		st.print(2, 3);
	}
}
继承与多态:
public class studentA{
	void print(){
		System.out.println("这是一个方法");
	}
	public static void main(String[] args){
		studentA sta = new studentA();
		studentB stb = new studentB();
		sta.print();
		stb.print();
	}
}
class studentB extends studentAP{
	void print(){
		System.out.println("这是一个子类");
	}
}
所谓绑定就是对象句柄与方法的绑定。
绑定分为静态绑定和动态绑定。
静态绑定不存在多态的问题。
动态绑定存在多态的问题。
equals方法的使用:
1、自反性。
2、对称性。
3、传递性。
4、一致性。
重载:一个类里,名字相同但参数不同的方法。
多态:避免在父类里大量重载,而引起代码臃肿且难于维护的解决方案。
多态的两种表现形式:重载和覆盖。
 
                    
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号