第2课第4节_Java面向对象编程_多态性_P【学习笔记】
摘要:韦东山android视频学习笔记
面向对象程序的三大特性之继承性:
1、向上转换:只能定义被子类覆写的方法,不能调用在子类中定义的方法。
1 class Father { 2 private int money; 3 4 public int getMoney() {return money; } 5 public void setMoney(int money) {this.money = money; } 6 7 public void printInfo() {System.out.println("This is Father");} 8 } 9 10 class Son extends Father{ 11 public void printInfo() {System.out.println("This is son");} 12 public void playGame(){System.out.println("This is son");} 13 } 14 15 class Daughter extends Father{ 16 public void printInfo() {System.out.println("This is Father");} 17 } 18 19 20 public class Cnv { 21 public static void main (String args[]) { 22 Son son = new Son(); 23 Daughter daughter = new Daughter(); 24 25 Father f = son; 26 f.printInfo(); 27 //f.playGame(); 28 29 f = daughter; 30 f.printInfo(); 31 } 32 }
编译运行:
2、JAVA向下转换的例子,在进行对象的向下转换前,必须首先发生对象的向上转换.否则会编译不过.
1 class Father { 2 private int money; 3 4 public int getMoney() {return money; } 5 public void setMoney(int money) {this.money = money; } 6 7 public void printInfo() {System.out.println("This is Father");} 8 } 9 10 class Son extends Father{ 11 public void printInfo() {System.out.println("This is son");} 12 public void playGame(){System.out.println("This is son");} 13 } 14 15 class Daughter extends Father{ 16 public void printInfo() {System.out.println("This is Father");} 17 } 18 19 20 public class Cnv2 { 21 public static void main (String args[]) { 22 Father f = new Son(); 23 Son son = (Son)f; 24 } 25 }
3、看一下,下面的例子,假如有一千个类继承了father这个类,如果我们要打印他们的信息,那样我们岂不是要写1千个print函数,下面的第二个代码则通过向上转换这个技巧实现.
1 class Father { 2 private int money; 3 4 public int getMoney() {return money; } 5 public void setMoney(int money) {this.money = money; } 6 7 public void printInfo() {System.out.println("This is Father");} 8 } 9 10 class Son extends Father{ 11 public void printInfo() {System.out.println("This is son");} 12 public void playGame(){System.out.println("This is son");} 13 } 14 15 class Daughter extends Father{ 16 public void printInfo() {System.out.println("This is Dauhter");} 17 } 18 19 20 public class Cnv3 { 21 public static void main (String args[]) { 22 Father f = new Father(); 23 Son s = new Son(); 24 Daughter d = new Daughter(); 25 26 print(f); 27 print(s); 28 print(d); 29 } 30 31 public static void print(Father f){ 32 f.printInfo(); 33 } 34 35 36 public static void print(Son s){ 37 s.printInfo(); 38 } 39 40 public static void print(Daughter d){ 41 d.printInfo(); 42 } 43 44 45 46 }
通过向上转换实现:
1 class Father { 2 private int money; 3 4 public int getMoney() {return money; } 5 public void setMoney(int money) {this.money = money; } 6 7 public void printInfo() {System.out.println("This is Father");} 8 } 9 10 class Son extends Father{ 11 public void printInfo() {System.out.println("This is son");} 12 public void playGame(){System.out.println("This is son");} 13 } 14 15 class Daughter extends Father{ 16 public void printInfo() {System.out.println("This is Dauhter");} 17 } 18 19 20 public class Cnv4 { 21 public static void main (String args[]) { 22 Father f = new Father(); 23 Son s = new Son(); 24 Daughter d = new Daughter(); 25 26 print(f); 27 print(s); 28 print(d); 29 } 30 31 32 public static void print(Father f){ 33 f.printInfo(); 34 } 35 }
上述两份代码编译运行结果一样:
4、instanceof: 用来判断一个对象是不是某个类的实例
1 class Father { 2 private int money; 3 4 public int getMoney() {return money; } 5 public void setMoney(int money) {this.money = money; } 6 7 public void printInfo() {System.out.println("This is Father");} 8 public void drink(){System.out.println("drink");} 9 } 10 11 class Son extends Father{ 12 public void printInfo() {System.out.println("This is son");} 13 public void playGame(){System.out.println("playGame");} 14 } 15 16 class Daughter extends Father{ 17 public void printInfo() {System.out.println("This is Dauhter");} 18 public void dance(){System.out.println("dance");} 19 } 20 21 22 public class Cnv5{ 23 public static void main (String args[]) { 24 Father f = new Father(); 25 Son s = new Son(); 26 Daughter d = new Daughter(); 27 28 printAction(f); 29 printAction(s); 30 printAction(d); 31 } 32 33 34 public static void printAction(Father f){ 35 if (f instanceof Son){ 36 Son s = (Son)f; 37 s.playGame(); 38 }else if (f instanceof Daughter){ 39 Daughter d = (Daughter)f; 40 d.dance(); 41 }else if(f instanceof Father){ 42 f.drink(); 43 } 44 } 45 }
编译运行结果:
相关代码存放在github,可以下载https://github.com/zzb2760715357/100ask