标签:面向 [] 转型 alt 举例 年龄 int 论语 new
多态
1.多态的概述
  同一个对象(事务),在不同的时刻体现出来的不同状态。
  使用举例:
  猪:是一种动物,是属于一种哺乳动物
  
2.多态的前提
  (1).要有继承的关系。
  (2).要有方法的重写。
      其实也可以没有,但是如果没有方法的重写,那么这一个就没有意义。
      动物 c=new 猫();
      c.show()
      动物 d=new 狗();
      d.show();
  (3).要有父类的引用指向子类对象
      父 f=new 子();  
3.多态中的成员访问特点
   (1).成员变量
      编译看左边,运行看右边
   (2).构造方法
      创建子类对象的时候,访问父类的构造方法,对父类的数据进行初始化。
   (3).成员方法(主要是由于方法重写的缘故造成子类的方法覆盖了父类的方法)
       编译看左边,运行看右边
   (4)静态方法
      编译看左边,运行看左边
   代码:
   父类代码:
   public class Father {
	public int num=10;
	
	public void show(){
		System.out.println("show father");
	}
	
	public static void function(){
		System.out.println("function father");
	}
    }
	
	//子类代码以及测试类
	public class Student extends Father{
	public int num=200;
	public int num2=400;
	
	public void show(){
		System.out.println("student show");
	}
	
	public void method(){
		System.out.println("method student");
	}
	
	public static void function(){
		System.out.println("function student");
	}
	
	
	//多态测试
	@SuppressWarnings("static-access")
	public static void main(String[] args) {
		Father ft=new Student();
		//访问父类的成员变量的时候运行看左边
		System.out.println(ft.num);
		//找不到符号,编译的时候报错,编译看右边,找的是父类里面的变量,没有就会报错
		//System.out.println(ft.num2);
		ft.show();
		//找不到符号,报错
		//ft.method();
		ft.function();
		
	}
    }
	//输出结果
	10
    student show
    function father
4.多态的好处
  (1).提高了代码的维护性(继承保证)
  (2).提高了代码的扩展性(由多态保证)
      我们写一个动物类,然后使用猫狗案例来实现各自需要的功能,由于这一些功能是相似的(吃饭、睡觉、玩耍)等,所以我们在工具类中统一制定一个,就可以对所有的动物进行操作使用,
	  而由于在调用的的时候每一个对象都是不一样的,所以调用也会不一样,这就能够保证代码的扩展性。
  (3).代码:
    动物类
    public class Animal {
	//吃饭
	public void eat(){
		System.out.println("animal eat()");
	}
	//睡觉
	public void sleep(){
		System.out.println("animal sleep");
	}
    }
	//狗类
	public class dog extends Animal{
	public void eat(){
		System.out.println("狗吃肉");
	}
    public void sleep() {
	    System.out.println("狗蜷缩着睡觉");
    }
    }
	//猫类
	public class cat extends Animal{
	public void eat(){
		System.out.println("猫吃鱼");
	}
    public void sleep() {
	    System.out.println("猫趴着睡觉");
    }
    }
	//工具类
	//针对动物操作的工具类
    public class AnimalTool {
	private AnimalTool(){}
	
	//调用猫的功能
    public static void useCat(cat c){
    	c.eat();
    	c.sleep();
    }
    //调用狗功能
    public static void useDog(dog d){
    	d.eat();
    	d.sleep();
    }
    }
5.多态中的转型问题
  (1).多态的弊端
      多态不能够使用子类的特有功能。如果想要使用子类特有的功能,那么如何实现,这一个时候就需要使用转型。
	  
  (2).转型主要有俩种:向下转型和向上转型
      向下转型:(子类变成父类)
          格式:fu f=new Zi();
	  向上转型:(父类变成子类)
	      格式:Zi z=(Zi)f
   代码:
   //父类代码
   public class Father {		
	public void show(){
		System.out.println("show father");
	}	
  }
   //子类代码以及测试
   public class Student extends Father{
	
	public void show(){
		System.out.println("student show");
	}
	
	public void method(){
		System.out.println("method student");
	}	
	//多态测试
	public static void main(String[] args) {
		Father f=new Student();
		f.show();
		//f.method();报错
		Student st=(Student) f;
		st.show();
		st.method();
	
	}
    }
	//输出结果
	student show
    student show
    method student
   (3).多态问题的理解(孔子装爹)
   class 孔子爹{
      public int age=40;
	  
	  public void teach(){
	      System.out.println("孔子爹会将易经");
	  }
   }
    
   class 孔子 extends 孔子爹{
      public int age=20;
	  
	  public void teach(){
	   System.out.println("孔子会讲论语");
	  }
	  
	  public void playGame(){
	     System.out.println("玩英雄联盟");
	  }
   
   }
    
	//最近学习易经的人比较多,很多人就请孔子爹去讲课,这一天孔子爹就被请走了,但是还是有人来请,
	//同时,价格还不错,就剩下孔子在家,孔子一想,我是不是可以考虑去?然后就穿上父亲的衣服,粘上胡子,然后就开始假装是他爹,然后就走了
	//这里面实质是一个向上转型的过程
	//孔子爹  k爹=new 孔子();外面看是孔子爹,实质是孔子
	System.out.println(k爹.age);//40因为是孔子在装爹,如果说年龄不像,就没有人认为他是他爹,也不会叫他去讲课
	k爹.teach();孔子只会讲论语,这一个过程就是多态中调用成员变量和方法的过程。
	k爹.playGame();尽管这一个是儿子本身,但是这一个是是在装父亲,所以是不能够玩游戏呢。
	
	
	//讲完了,下班回家
	//脱下爹的伪装,然后又变回自己
	//其实这一个时候是一个向下转型的一个过程,这一个时候就是还原自己的样子
	孔子 k=(孔子) k爹;
	System.out.println(k.age);//20
	k.teach();//讲论语
	k.playGame();//玩英雄联盟
6.多态练习
(1).不同地方不同的饮食文化
   //person类
   public class Person {
	public void eat(){
		System.out.println("person eat");
	}
    }
   //南方人
   public class SouthPerson extends Person{
	public void eat(){
		System.out.println("炒菜,吃米饭");
	}
	public void bus(){
		System.out.println("经商");
	}
    }
   //北方人
   public class NorthPerson extends Person{
	public void eat(){
		System.out.println("炖菜,吃馒头");
	}
	
	public void yanjiu(){
		System.out.println("研究");
	}
    }
    //测试类
	public class DuoTaiTest {
	public static void main(String[] args) {
		//南方人
		Person p=new Person();
		p.eat();
		System.out.println("-------------");
		SouthPerson sp=new SouthPerson();
		sp.bus();
		sp.eat();
		System.out.println("-------------");
		//北方人
		p=new NorthPerson();
		p.eat();
		System.out.println("-------------");
		NorthPerson np=(NorthPerson)p;
		np.eat();
		np.yanjiu();
	}
    }
	//测试结果
	person eat
    -------------
    经商
    炒菜,吃米饭
    -------------
    炖菜,吃馒头
    -------------
    炖菜,吃馒头
    研究
(2).面试题:看程序写结果
    class A{
	    public void show(){
		show2();
		}
		
		public void show2(){
		System.out.println("我");
		}
	}
	
	class B extends A{
	   public void show2(){
		System.out.println("爱");
		} 
	}
    class C extends B{
	
	 public void show(){
		show2();
		}
		
		public void show2(){
		System.out.println("你");
		}
	
	}
	public class DuoTaiTest{
	   public static void main(String[] args) {
	     A a=new B();
		 a.show();
		 
		 B b=new C();
		 b.show()
	   
	   }
}
//输出结果:爱你
标签:面向 [] 转型 alt 举例 年龄 int 论语 new
原文地址:https://www.cnblogs.com/nwxayyf/p/9524239.html