菜鳥譯文(一)——Java中的繼承和組合,譯文java

來源:互聯網
上載者:User

菜鳥譯文(一)——Java中的繼承和組合,譯文java

閱讀英文的能力對於程式員來說,是很重要的。這幾年也一直在學習英文,今天心血來潮,就在網上找了一篇簡短的博文翻譯一下。水平一般,能力有限,還請各位看官多多指點。


譯文:

本文將會舉例說明Java中繼承和組合的概念。首先舉一個繼承的例子,然後展示一下如何用組合來改善繼承的設計。最後概括一下如何在它們之間做出選擇。

1. 繼承

假設我們有一個Insect類。這個類包含兩個方法:一個是move(),一個是attack()。

class Insect {private int size;private String color; public Insect(int size, String color) {this.size = size;this.color = color;} public int getSize() {return size;} public void setSize(int size) {this.size = size;} public String getColor() {return color;} public void setColor(String color) {this.color = color;} public void move() {System.out.println("Move");} public void attack() {move(); //assuming an insect needs to move before attackingSystem.out.println("Attack");}}


現在你想定義一個Bee類,它是Insect類型的,但是有著不同實現的attack()方法和move()方法。我們可以用繼承來設計,如下所示:

class Bee extends Insect {public Bee(int size, String color) {super(size, color);} public void move() {System.out.println("Fly");} public void attack() {move();super.attack();}}public class InheritanceVSComposition {public static void main(String[] args) {Insect i = new Bee(1, "red");i.attack();}}



類階層關係圖就是如此簡單:


輸出:

FlyFlyAttack


"Fly"被列印了兩次,表示move()被調用了兩次。但是它應該被調用了一次才對。

問題出在super.attack()方法上。Insect的attack()方法調用move()方法。當子類調用super.attack()時,總是會調用重寫的move()方法。


我們可以用下面的方法解決這個問題:

這個繼承的設計不好,因為子類依賴父類的具體實現,如果父類發生變化,子類將被破壞。

2. 組合

與繼承相反,組合可以用於這種情況。讓我們先看看使用組合的解決方案。

attack方法被抽象為一個介面。

interface Attack {public void move();public void attack();}

可以對Attack介面進行多種不同的實現。

class AttackImpl implements Attack {private String move;private String attack; public AttackImpl(String move, String attack) {this.move = move;this.attack = attack;} @Overridepublic void move() {System.out.println(move);} @Overridepublic void attack() {move();System.out.println(attack);}}

將attack方法抽出來,Insect就不再與attack相關聯了。

class Insect {private int size;private String color; public Insect(int size, String color) {this.size = size;this.color = color;} public int getSize() {return size;} public void setSize(int size) {this.size = size;} public String getColor() {return color;} public void setColor(String color) {this.color = color;}}

Bee是一個Insect的類型,它可以攻擊。

// This wrapper class wrap an Attack objectclass Bee extends Insect implements Attack {private Attack attack; public Bee(int size, String color, Attack attack) {super(size, color);this.attack = attack;} public void move() {attack.move();} public void attack() {attack.attack();}}



類圖:




public class InheritanceVSComposition2 {public static void main(String[] args) {Bee a = new Bee(1, "black", new AttackImpl("fly", "move"));a.attack(); // if you need another implementation of move()// there is no need to change Insect, we can quickly use new method to attack Bee b = new Bee(1, "black", new AttackImpl("fly", "sting"));b.attack();}}

flymoveflysting

3. 何時用繼承,何時用組合?

下面兩條內容,可以告訴我們如何在繼承與組合之間做出選擇:

總之,繼承和組合都有其用途,和理解他們的優缺點是很有必要的。


最後說一點自己的感受吧,小弟自打初中開始學英語,成績就沒好過,最好成績也就剛及格吧。記得當年高考的時候lz的英語成績是55分(足以載入史冊的成績),我的英文水平有多差,大家可想而知了吧。後來承蒙恩師的諄諄教誨,一直沒有放棄英語的學習,現在依然每天都在學(雖然沒有掌握其精髓,但是學總比不學強)。以前遇到外國人根本張不開嘴,不知道說什麼,現在好多了,之前經常跟老外一起踢球,沒事瞎白話幾句,感覺也挺好玩的。以前看到英文的文章,直接關掉,現在也能靜下心來看下去了。


總之,學英語心態很重要,只要你不怕它,它就沒什麼好怕的。毛主席曾說過:“All the reactionaries are the Papertiger(一切反動派都是紙老虎)”。英語沒什麼好怕的,遇到老外你就跟他瞎扯唄,最不濟你倆打個平手——誰也聽不懂誰說什麼。還有更壞的結果嗎?不管怎樣咱都不會輸,那你還怕啥?看英文文章、書籍看不懂,那就更不用怕了,大不了弄個詞典唄,我大有道在手,還怕治不了你個小英文了。別猶豫了,上吧,少年!


原文連結: Inheritance vs. Composition in Java




菜鳥中的菜鳥助一道對於java繼承的題目

我寫了一個,不知道符不符合你的要求
class Person {
String name;
String birth;
Person(String n,String b){
name = n;
birth =b;
}
public void getName() {
System.out.println("name=" + name);
}
public void getBirth(){
System.out.println("birth=" + birth);
}
}
class Student extends Person{
String major;
Student(String n,String b,String m){
super(n,b);
major = m;
}
public void getMajor(){
System.out.println("major=" + major);
}
}
class Instructor extends Person {
double salary;
Instructor(String n,String b,double s) {
super(n,b);
salary = s;
}
public void getSalary() {
System.out.println("salary=" + salary);
}
}

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

Student s = new Student("xiao li","1990-05-18","math");
Instructor i = new Instructor("lao wang","1980-11-24",3425.78);
System.out.println("Student Information:");
s.getName();
s.getBirth();
s.getMajor();
System.out.println("Instructor Information:");
i.getName();
i.getBirth();
i.getSalary();
}

}
 
問java中繼承與介面有什不同?將仔細點,菜鳥了

繼承是extended命令,一個類只能繼承一個父類,class A extended B,C這種是錯誤的。繼承後,子類可以使用父類的方法。
介面是implements命令,一個類可以有很多介面,class A implements B,C,D是可以的。定義了介面之後,子類需要覆寫掉介面中的方法,因為介面方法是沒有具體實現的。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.