最近在看java编程思想,分享一下
之前只知道继承,原来继承不怎么可靠啊
1.组合
public class Bath {
private String s1
= "Happy",s2
= "Happy",s3
, s4
;
private Soap castille
;
private int i
;
private float toy
;
public Bath() {
System
.out
.println("Bath");
s3
= "joy";
toy
= 3.14f;
castille
= new Soap();
}
{ i
= 47;}
@Override
public String
toString() {
if (s4
== null
) {
s4
= "Joy";
}
return "Bath{" +
"s1='" + s1
+ '\'' +
", s2='" + s2
+ '\'' +
", s3='" + s3
+ '\'' +
", s4='" + s4
+ '\'' +
", castille=" + castille
+
", i=" + i
+
", toy=" + toy
+
'}';
}
}
class Soap{
private String s
;
Soap(){
System
.out
.println("Soap");
s
="Constructed";
}
public String
toString(){
return s
;
}
}
public static void Main(String
[] args
){
Bath bath
= new Bath();
System
.out
.println(bath
);
}
2.继承
这里的代码并非原书中的这部分代码,为代理部分的代码
public class SpaceShipControls {
void up(int velocity
){}
void down(int velocity
){}
void left(int velocity
){}
void right(int velocity
){}
void forward(int velocity
){}
void back(int velocity
){}
void turbBoost(){}
class SpaceShip extends SpaceShipControls{
private String name
;
public SpaceShip(String name
){
this.name
= name
;
}
public String
toString(){
return name
;
}
}
public static void Main(String
[] args
){
SpaceShip spaceShip
= new SpaceShip("NSEA Protector");
spaceShip
.forward(100);
}
}
3.代理
public class SpaceShipControls {
void up(int velocity
){}
void down(int velocity
){}
void left(int velocity
){}
void right(int velocity
){}
void forward(int velocity
){}
void back(int velocity
){}
void turbBoost(){}
class SpaceShipDelegation{
private String name
;
private SpaceShipControls controls
= new SpaceShipControls();
public SpaceShipDelegation(String name
){
this.name
= name
;
}
public void back(int velocity
){
controls
.back(velocity
);
}
public void right(int velocity
){
controls
.back(velocity
);
}
public void left(int velocity
){
controls
.back(velocity
);
}
public void down(int velocity
){
controls
.back(velocity
);
}
public void up(int velocity
){
controls
.back(velocity
);
}
public void forward(int velocity
){
controls
.forward(velocity
);
}
public void turbBoost(){
controls
.turbBoost();
}
}
public static void Main(String
[] args
){
SpaceShipDelegation spaceShipDelegation
= new SpaceShipDelegation("NSEA Protector");
spaceShipDelegation
.back(100);
}
}
组合:你要用可以,没问题,拿去,别人要用你得告诉我一声,你得负责管制着 继承:人皆可夫,给了你 就相当于给了世界 组合:当前A类含有另一个B类对象 使用A类是也只是能使用该类的属性和方法,要想用B里面的东西就在A类中去写一下,控制权在自己手里 继承:获取父类的所有属性和方法 使用该类可以调用父类的属性和方法,继承之后所有的东西都暴露了,破坏了封装
4.组合和继承混合使用
public class PlaceSetting {
static class Plate{
Plate(int i
){
System
.out
.println("Plate constructor");
}
}
static class DinnerPlate extends Plate{
DinnerPlate(int i
) {
super(i
);
System
.out
.println("DinnerPlate constructor");
}
}
static class Utensil{
Utensil(int i
){
System
.out
.println("Utensil constructor");
}
}
static class Spoon extends Utensil{
Spoon(int i
) {
super(i
);
System
.out
.println("Spoon constructor");
}
}
static class Fork extends Utensil{
Fork(int i
) {
super(i
);
System
.out
.println("Fork constructor");
}
}
static class Knife extends Utensil{
Knife(int i
) {
super(i
);
System
.out
.println("Knife constructor");
}
}
static class Custom{
Custom(int i
){
System
.out
.println("Custom constructor");
}
}
static class PlaceSettings extends Custom{
private Spoon sp
;
private Fork frk
;
private Knife kn
;
private DinnerPlate pl
;
public PlaceSettings(int i
) {
super(i
+1);
sp
= new Spoon(i
+2);
frk
= new Fork(i
+3);
kn
= new Knife(i
+4);
pl
= new DinnerPlate(i
+5);
System
.out
.println("PlaceSettings constructor");
}
}
public static void Main(String
[] args
){
PlaceSettings placeSettings
= new PlaceSettings(9);
}
}
– 代码与图片均来自《java编程思想》