A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© Ethan丶 中级黑马   /  2015-9-17 09:55  /  267 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 Ethan丶 于 2015-9-17 11:55 编辑
  1. package com.itheima;
  2. /*
  3. * polymorphism:同一个对象,在不同时刻体现出来的状态
  4. * cat-cat;dog-dog;animal-cat/dog
  5. * 多态的特点:继承,方法重写,父类引用指向子类对象
  6. *
  7. */
  8. public class Polymorphism {
  9.         public static void main(String[] args) {
  10.                 Animal c=new Cat();   //成员变量:编译看左边,运行看左边
  11.                 Animal d=new Dog();   
  12.                 c.method();
  13.                 System.out.println("----");
  14.                 d.method();
  15. //                d.method1();  //成员方法:编译看左边,运行看右边
  16.                 System.out.println(d.num);
  17. //                System.out.println(d.num2);
  18.                 d.function();  //静态方法 编译看左边,运行看左边
  19.         }        
  20.                 /*//方法区
  21.                 Dog d1 =new Dog();
  22.                 Dog d2 =new Dog();
  23.                 Dog d3 =new Dog();   
  24.                 methodKey(d1);
  25.                 methodKey(d2);
  26.                 methodKey(d3);
  27.         
  28.         
  29.         public static void methodKey(Dog d){
  30.                 d.method();
  31.                 d.method1();
  32.         }*/
  33. }

  34. class Cat extends Animal{
  35.         Cat (){
  36.                
  37.         }
  38.         public void method(){
  39.                 System.out.println("cat");
  40.         }
  41. }
  42. class Dog extends Animal{
  43.         public int num=1000;
  44.         public int num2=10;
  45.         Dog (){
  46.                
  47.         }
  48.         public void method(){
  49.                 System.out.println("dog");
  50.         }
  51.         public void method1(){
  52.                 System.out.println("dog1");
  53.         }
  54.         public static void function(){
  55.                 System.out.println("static dog");
  56.         }
  57. }
  58. class Animal {
  59.         int age;
  60.         public int num =100;
  61.         Animal(){
  62.                
  63.         }
  64.         public static void function(){
  65.                 System.out.println("static Animal");
  66.         }
  67.         
  68.         public void method(){
  69.                 System.out.println("Animal");
  70.         }
  71. }
复制代码

4 个回复

倒序浏览
  1. package com.itheima;
  2. /*
  3. * polymorphism
  4. * 多态向上向下转型
  5. * 上:Far f=new Soon();
  6. * 下:Soon s=(Son) f;
  7. */
  8. public class PolyDemo {
  9.         public static void main(String[] args) {
  10.                 Far f = new Soon();
  11.                 f.teach();
  12.                 System.out.println(f.age);  //40
  13.                 System.out.println("---");
  14.                
  15.                 Soon s = (Soon) f;  //向下砖
  16.                 s.teach();
  17.                 s.play();
  18.                 System.out.println(s.age);  //20
  19.         }
  20. }

  21. //父类
  22. class Far {
  23.         public int age = 40;
  24.         public void teach(){
  25.                 System.out.println("教育");
  26.         }
  27. }
  28. //子类
  29. class Soon extends Far {
  30.         public int age = 20;  //子的age
  31.         public void teach(){
  32.                 System.out.println("教育");  //重写父类
  33.         }
  34.         public void play(){
  35.                 System.out.println("娱乐");  //子特有的
  36.         }
  37. }
复制代码
回复 使用道具 举报

polymorphism 的 总结
回复 使用道具 举报
加油,越努力越幸运
回复 使用道具 举报
自己的思路~
  1. package com.itheima;
  2. /*
  3. * interface  means  like a
  4. * abstract means is a
  5. * Teacher and Student  
  6. * same:name,age,eat,sleep  eat:man eat is abstract
  7. * different: Teacher:smork
  8. */
  9. public class Test7 {
  10.         public static void main(String[] args) {               
  11.                 Person tp = new Teacher();
  12.                 Person sp = new Student();
  13.                 tp.setAge(24);
  14.                 tp.setName("teacher");
  15.                 tp.sleep();
  16.                 tp.eat();
  17.                 System.out.print(tp.getAge());
  18.                 System.out.println(tp.getName());
  19.                 System.out.println("-------");
  20.                 sp.setAge(12);
  21.                 sp.setName("student");
  22.                 sp.sleep();
  23.                 tp.eat();
  24.                 System.out.print(sp.getAge());
  25.                 System.out.println(sp.getName());
  26.                 System.out.println("-------");
  27.                
  28.                 PersonActivity pat = new TeacherSmo();
  29.                 pat.smok();
  30.                 System.out.println("-------");
  31.                 PersonActivity pas = new StudentSmo();
  32.                 pas.smok();
  33.                
  34.         }
  35. }
  36. interface PersonActivity {
  37.         public abstract void smok();
  38. }
  39. class Person {
  40.         private String name;
  41.         private int age;
  42.         public Person(){}
  43.         public Person(String name,int age){
  44.                 this.name = name;
  45.                 this.age = age;
  46.         }
  47.         public void sleep(){
  48.                 System.out.println("sleep");
  49.         }
  50.         public void eat(){}
  51.         public String getName() {
  52.                 return name;
  53.         }
  54.         public void setName(String name) {
  55.                 this.name = name;
  56.         }
  57.         public int getAge() {
  58.                 return age;
  59.         }
  60.         public void setAge(int age) {
  61.                 this.age = age;
  62.         }
  63.        
  64. }
  65. class Teacher extends Person {
  66.         public void eat(){
  67.                 System.out.println("T's eat");
  68.         }
  69.         public Teacher(){}
  70.         public Teacher(String name,int age){
  71.                 super(name,age);
  72.         }
  73.         public void sleep(){
  74.                 System.out.println("T'sleep");
  75.         }
  76. }
  77. class Student extends Person {
  78.         public void eat(){}
  79.         public Student(){}
  80.         public Student(String name,int age){
  81.                 super(name,age);
  82.         }
  83.         public void sleep(){
  84.                 System.out.println("S'sleep");
  85.         }
  86. }
  87. class TeacherSmo implements PersonActivity {
  88.         public void eat(){}
  89.         public TeacherSmo(){}
  90.         public void smok(){
  91.                 System.out.println("T'smok");
  92.         }
  93.         public void sleep(){
  94.                 System.out.println("T'sleep");
  95.         }
  96. }
  97. class StudentSmo implements PersonActivity {
  98.         public void eat(){
  99.                 System.out.println("S's eat");
  100.         }
  101.         public StudentSmo(){}
  102.         public void smok(){
  103.                 System.out.println("S'smok");
  104.         }
  105.         public void sleep(){
  106.                 System.out.println("s'sleep");
  107.         }
  108. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马