组合语法
目前为止,已经多次使用组合技术。只需将对象引用置于新类中即可,例如,假设你需要某个对象,他要具有多个String对象,几个基本类型数据,以及另一个类的对象。对于非基本类型的对象,必须将其引用置于新类中,但可以直接定义基本类型数据。
class WaterSource {
private String s;
WaterSource() {
System.out.println("WaterSource()");
s = "Constructed";
}
public String toString() {
return s;
}
}
public class SprinklerSystem {
private String valve1, valve2, valve3, valve4;
private WaterSource source = new WaterSource();
private int i;
private float f;
public String toString() {
return
"valve1 = " + valve1 + " " +
"valve2 = " + valve2 + " " +
"valve3 = " + valve3 + " " +
"valve4 = " + valve4 + "\n" +
"i = " + i + " " + "f = " + f + " " +
"source = " + source;
}
public static void main(String[] args) {
SprinklerSystem sprinklers = new SprinklerSystem();
System.out.println(sprinklers);
}
}
继承语法
组合的语法比较平实,但是继承使用的是一种特殊的语法,关键字extends实现。
class Cleanser {
private String s = "Cleanser";
public void append(String a) {
s += a;
}
public void scrub() {
append(" scrub()");
}
public String toString() {
return s;
}
}
public class Detergent extends Cleanser {
public void scrub() {
append(" Detergent.scrub()");
super.scrub();
}
public static void main(String[] args) {
Detergent x = new Detergent();
x.scrub();
System.out.println(x);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/e0ca97394d6d591c5636d5736d04c1d4.png)
初始化基类
class Art {
Art() {
System.out.println("Art constructor");
}
}
class Drawing extends Art {
Drawing() {
System.out.println("Drawing constructor");
}
}
public class Cartoon extends Drawing {
public Cartoon() {
System.out.println("Cartoon constructor"); }
public static void main(String[] args) {
Cartoon x = new Cartoon();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/0b4eabc043d052a7573926572da80e08.png)
通过打印结果看,构建过程是有基类“向外”扩散的,所以基类在导出类构造器可以访问之前就已经初始化了。
带参数的构造器
如果想要调用带参数的基类构造器,就
必须使用super显示的编写调用基类的语句,并且配以适当的参数列表。
class Game {
Game(int i) {
System.out.println("Game constructor");
}
}
class BoardGame extends Game {
BoardGame(int i) {
super(i);
System.out.println("BoardGame constructor");
}
}
public class Chess extends BoardGame {
Chess() {
super(11);
System.out.println("Chess constructor");
}
public static void main(String[] args) {
Chess x = new Chess();
}
}
代理
第三种关系成为代理,java并没有提供对他的直接支持。这是继承和组合之间的中庸之道。因为我们将一个成员对象置于所要构造的类中(就像组合),与此同时我们在新类中暴露了该成员的所有方法(就像继承),例如太空船需要一个控制模块:
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 turboBoost() {}
}
构造太空船的一种方式是继承:
public 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 protector = new SpaceShip("NSEA Protector");
protector.forward(100);
}
}
然而,SpaceShip并非真正的SpaceShipControls类型,即便你可以调用SpaceShipControls的方法。跟准确的讲,SpaceShip包含SpaceShipControls,与此同时,SpaceShipControls的所有方法在SpaceShip中都暴露了出来,代理解决此难题。
public class SpaceShipDelegation {
private String name;
private SpaceShipControls controls = new SpaceShipControls();
public SpaceShipDelegation(String name) {
this.name = name;
}
// Delegated methods:
public void back(int velocity) {
controls.back(velocity);
}
public void down(int velocity) {
controls.down(velocity);
}
public void forward(int velocity) {
controls.forward(velocity);//这是重点
}
public void left(int velocity) {
controls.left(velocity);
}
public void right(int velocity) {
controls.right(velocity);
}
public void turboBoost() {
controls.turboBoost();
}
public void up(int velocity) {
controls.up(velocity);
}
public static void main(String[] args) {
SpaceShipDelegation protector = new SpaceShipDelegation("NSEA Protector");
protector.forward(100);
}
}
上述代码 个人理解为加了一个保护类,有保护类(代理类)提供方法,可避免父类中的所有方法在实现中暴露
(关于代理,后面章节会有讲解)
结合使用组合和继承
class Utensil {
Utensil(int i) {
System.out.println("Utensil constructor");
}
}
class Spoon extends Utensil {
Spoon(int i) {
super(i);
System.out.println("Spoon constructor");
}
}
class Custom {
Custom(int i) {
System.out.println("Custom constructor");
}
}
public class PlaceSetting extends Custom {
private Spoon sp;
public PlaceSetting(int i) {
super(i + 1);
sp = new Spoon(i + 2);
System.out.println("PlaceSetting constructor");
}
public static void main(String[] args) {
PlaceSetting x = new PlaceSetting(9);
}
}
final关键字
引用一篇文章吧
https://www.cnblogs.com/lwbqqyumidi/p/3513047.html