向上转换:只能定义被子类覆写的方法,不能调用在子类中定义的方法。
class Father {
private int money;
public int getMoney() {return money; }
public void setMoney(int money) {this.money = money; }
public void printInfo() {System.out.println("This is Father");}
}
class Son extends Father{
public void printInfo() {System.out.println("This is son");}
public void playGame() {System.out.println("playGame ...");}
}
class Daughter extends Father{
public void printInfo() {System.out.println("This is daughter");}
}
public class Cnv {
public static void main (String args[]) {
Son son = new Son();
Daughter daughter = new Daughter();
Father f = son; /* */
f.printInfo();
//f.playGame();
f = daughter;
f.printInfo();
}
}
运行结果:
root@ubuntu:/home/topeet/guyilian# javac Cnv.java
root@ubuntu:/home/topeet/guyilian# java Cnv
This is son
This is daughter
JAVA向下转换的例子,在进行对象的向下转换前,必须首先发生对象的向上转换.否则会编译不过
class Father {
private int money;
public int getMoney() {return money; }
public void setMoney(int money) {this.money = money; }
public void printInfo() {System.out.println("This is Father");}
}
class Son extends Father{
public void printInfo() {System.out.println("This is son");}
public void playGame() {System.out.println("playGame ...");}
}
class Daughter extends Father{
public void printInfo() {System.out.println("This is daughter");}
}
public class Cnv2 {
public static void main (String args[]) {
Father f = new Son();
Son son = (Son)f; /* Son son = new Son(); */
}
}
运行结果:
root@ubuntu:/home/topeet/guyilian# javac Cnv2.java
root@ubuntu:/home/topeet/guyilian# java Cnv2
看一下,下面的例子,假如有一千个类继承了father这个类,如果我们要打印他们的信息,那样我们岂不是要写1千个print函数,下面的第二个代码则通过向上转换这个技巧实现.
class Father {
private int money;
public int getMoney() {return money; }
public void setMoney(int money) {this.money = money; }
public void printInfo() {System.out.println("This is Father");}
}
class Son extends Father{
public void printInfo() {System.out.println("This is son");}
public void playGame() {System.out.println("playGame ...");}
}
class Daughter extends Father{
public void printInfo() {System.out.println("This is daughter");}
}
public class Cnv3 {
public static void main (String args[]) {
Father f = new Father();
Son s = new Son();
Daughter d = new Daughter();
print(f);
print(s);
print(d);
}
public static void print(Father f) {
f.printInfo();
}
public static void print(Son s) {
s.printInfo();
}
public static void print(Daughter d) {
d.printInfo();
}
}
运行结果:
root@ubuntu:/home/topeet/guyilian# javac Cnv3.java
root@ubuntu:/home/topeet/guyilian# java Cnv3
This is Father
This is son
This is daughter
通过向上转换实现:
class Father {
private int money;
public int getMoney() {return money; }
public void setMoney(int money) {this.money = money; }
public void printInfo() {System.out.println("This is Father");}
}
class Son extends Father{
public void printInfo() {System.out.println("This is son");}
public void playGame() {System.out.println("playGame ...");}
}
class Daughter extends Father{
public void printInfo() {System.out.println("This is daughter");}
}
public class Cnv4 {
public static void main (String args[]) {
Father f = new Father();
Son s = new Son();
Daughter d = new Daughter();
print(f);
print(s);
print(d);
}
public static void print(Father f) {
f.printInfo();
}
}
运行结果:
root@ubuntu:/home/topeet/guyilian# javac Cnv4.java
root@ubuntu:/home/topeet/guyilian# java Cnv4
This is Father
This is son
This is daughter
instanceof: 用来判断一个对象是不是某个类的实例
class Father {
private int money;
public int getMoney() {return money; }
public void setMoney(int money) {this.money = money; }
public void printInfo() {System.out.println("This is Father");}
public void drink() {System.out.println("drink ...");}
}
class Son extends Father{
public void printInfo() {System.out.println("This is son");}
public void playGame() {System.out.println("playGame ...");}
}
class Daughter extends Father{
public void printInfo() {System.out.println("This is daughter");}
public void dance() {System.out.println("dance ...");}
}
public class Cnv5 {
public static void main (String args[]) {
Father f = new Father();
Son s = new Son();
Daughter d = new Daughter();
printAction(f);
printAction(s);
printAction(d);
}
public static void printAction(Father f) {
if (f instanceof Son) {
Son son = (Son)f;
son.playGame();
}
else if (f instanceof Daughter) {
Daughter d = (Daughter)f;
d.dance();
}
else if (f instanceof Father) {
f.drink();
}
}
}
运行结果:
root@ubuntu:/home/topeet/guyilian# javac Cnv5.java
root@ubuntu:/home/topeet/guyilian# java Cnv5
drink ...
playGame ...
dance ...