标签:自动完成 ebs 强制 子类 server class message 需要 特殊
1 class Message { 2 public void print() { 3 System.out.println("Message"); 4 } 5 } 6 class DatabaseMessage extends Message { 7 public void print() { 8 System.out.println("Connecting to the database..."); 9 } 10 } 11 class WebServerMessage extends Message { 12 public void print() { 13 System.out.println("Connecting to the web server..."); 14 } 15 }
使用转型:
1 class Solution { 2 public static void main(String[] args) { 3 Message msg=new DatabaseMessage(); 4 msg.print(); 5 } 6 }
也可写成:
1 class Solution { 2 public static void main(String[] args) { 3 func(new DatabaseMessage()); 4 func(new WebServerMessage()); 5 } 6 7 public static void func(Message msg) { 8 msg.print(); 9 } 10 }
使用重载(不易维护):
1 class Solution { 2 public static void main(String[] args) { 3 func(new DatabaseMessage()); 4 func(new WebServerMessage()); 5 } 6 7 public static void func(DatabaseMessage msg) { 8 msg.print(); 9 } 10 }
1 Person per=new Superman(); 2 Superman man=(Superman) per;
标签:自动完成 ebs 强制 子类 server class message 需要 特殊
原文地址:https://www.cnblogs.com/irisiscool/p/13083534.html