标签:
//Mosquito.java
//蚊子类,拥有一个唱歌的方法
package project.app.service;
public class Mosquito {
public String sing() {
return "You have to admit that I sing songs very well.";
}
}
//Single.java
//单身狗类,playWithMosquito依赖于蚊子类
package project.app.service;
import project.app.service.Mosquito;
public class Single {
private Mosquito mosquito;
public void setMosquito(Mosquito mosquito) {
this.mosquito = mosquito;
}
public void playWithMosquito() {
System.out.println(mosquito.sing());
}
}
//Test.java
package chris;
import project.app.service.*;
public class Test {
public static void main(String[] args) throws Exception {
Single single = new Single();
Mosquito mosquito = new Mosquito();
//设置single实例和mosquito实例的关系
single.setMosquito(mosquito);
single.playWithMosquito();
}
}
//Cockroach.java
//蟑螂类,有不同于蚊子的唱歌的方法
package project.app.service;
public class Cockroach {
public String sing() {
return "I sing when everybody is in sleep.";
}
}
- //Single.java
//单身狗类,playWithMosquito依赖于蟑螂类
package project.app.service;
import project.app.service.Cockroach;
public class Single {
private Cockroach cockroach;
public void setCockroach(Cockroach cockroach) {
this.cockroach = cockroach;
}
public void playWithCockroach() {
System.out.println(cockroach.sing());
}
}
//Test.java
package chris;
import project.app.service.*;
public class Test {
public static void main(String[] args) throws Exception {
Single single = new Single();
Cockroach cockroach = new Cockroach();
single.setCockroach(cockroach);
single.playWithCockroach();
}
}
//Pet.java
//宠物接口,有一个唱歌的方法
package project.app.service;
public interface Pet {
public String sing();
}
//Mosquito.java
//
package project.app.service.impl;
import project.app.service.Pet;
public class Mosquito implements Pet {
public String sing() {
return "You have to admit that I sing songs very well.";
}
}
//Single.java
//单身狗类,playWithMosquito依赖于Pet接口,注意,这个时候单身狗类只依赖于宠物接口,不依赖于具体的蚊子类了。
package project.app.service;
import project.app.service.Pet;
public class Single {
private Pet pet;
public void setPet(Pet pet) {
this.pet = pet;
}
public void playWithPet() {
System.out.println(pet.sing());
}
}
//Test.java
package chris;
import project.app.service.Single;
import project.app.service.Pet;
import project.app.service.impl.Mosquito;
public class Test {
public static void main(String[] args) throws Exception {
Single single = new Single();
Mosquito mosquito = new Mosquito();
single.setPet(mosquito);
single.playWithPet();
}
}
//Cockroach.java
//蟑螂类,有唱歌的方法
package project.app.service.impl;
import project.app.service.Pet;
public class Cockroach implements Pet {
public String sing() {
return "I sing when everybody is in sleep.";
}
}
//Test.java
package chris;
import project.app.service.Single;
import project.app.service.Pet;
import project.app.service.impl.Cockroach;
public class Test {
public static void main(String[] args) throws Exception {
Single single = new Single();
Cockroach cockroach = new Cockroach();
single.setPet(cockroach);
single.playWithPet();
}
}
标签:
原文地址:http://www.cnblogs.com/bottleondesk/p/5540148.html