Java-Composition for Code Reuse
来源:互联网 发布:8080端口号被占用 编辑:程序博客网 时间:2024/06/05 18:20
复用代码(代码重用机制)
java问题解决是围绕着类展开的. 可以通过创建新类来复用代码(利用现有类形成新类). 有两种方法可以达到这一目的:
(1)在新类中产生现有类的对象. 由于新类是由现有类的对象所组成, 所以这种方法称为组合. 该方法只是复用了现有程序代码的功能, 而非它的形式.
(2)按照现有类的形式来创建新类. 采用现有类的形式并在其中添加新代码. 称为继承.
/** *//**
* Title: Composition for code reuse<br>
* Description: 组合语法, 只需将对象引用置于新类中即可<br>
* Copyright: (c) 2008 Thinking in Java<br>
* Company: (c) 2008 Augmentum.Inc<br>
* @author Forest He
* @version 1.0
*/
class WaterSource ...{
private String s;
WaterSource() ...{
System.out.println("WaterSource()");
s = new String("Constructed");
/**//*创建了两个对象, 一个是"Constructed", 一个是指向"Constructed"的引用对象s*/
}
public String toString() ...{return s;}
}
public class SprinklerSystem ...{
private String value1, value2, value3, value4;
private String
s1 = new String("aaaaaaaaaaa"),
s2 = new String("bbbbbbbbbbb"),
s3, s4;
/**//*学会上诉批量定义方法*/
private WaterSource source = new WaterSource();
private int i;
private float f;
public String toString() ...{
if(s3 == null) s3 = new String("ccccccccccc");
if(s4 == null) s4 = new String("ccccccccccc");
return
"value1 = " + value1 + " " +
"value2 = " + value2 + " " +
"value3 = " + value3 + " " +
"value4 = " + value4 + " " +
"s1 = " + s1 + " " +
"s2 = " + s2 + " " +
"s3 = " + s3 + " " +
"s4 = " + s4 + " " +
"i = " + i + " " +
"f = " + f + " " +
"source = " + source;
/**//*这个return语句真的很别致, 学习*/
}
public static void main(String[] args) ...{
SprinklerSystem sprinklers = new SprinklerSystem();
System.out.println(sprinklers);
}
}
/**//* toString(): 每一个非基本类型的对象都有一个toString()方法, 而且当编译器需要一个String而你却只有一个对象时, 给方法会被调用.
* SprinklerSystem.toString()中"source = " + source;指的是: 由于只能将一个String对象和另一个String对象相加, 因此编译器会告诉你: "我将调用toString(), 把source转换成一个String!"(自动类型转换). 每当你想要使所创建的类具备这样的行为时, 编写一个toString()方法覆盖掉默认toString()方法即可.
*/
* Title: Composition for code reuse<br>
* Description: 组合语法, 只需将对象引用置于新类中即可<br>
* Copyright: (c) 2008 Thinking in Java<br>
* Company: (c) 2008 Augmentum.Inc<br>
* @author Forest He
* @version 1.0
*/
class WaterSource ...{
private String s;
WaterSource() ...{
System.out.println("WaterSource()");
s = new String("Constructed");
/**//*创建了两个对象, 一个是"Constructed", 一个是指向"Constructed"的引用对象s*/
}
public String toString() ...{return s;}
}
public class SprinklerSystem ...{
private String value1, value2, value3, value4;
private String
s1 = new String("aaaaaaaaaaa"),
s2 = new String("bbbbbbbbbbb"),
s3, s4;
/**//*学会上诉批量定义方法*/
private WaterSource source = new WaterSource();
private int i;
private float f;
public String toString() ...{
if(s3 == null) s3 = new String("ccccccccccc");
if(s4 == null) s4 = new String("ccccccccccc");
return
"value1 = " + value1 + " " +
"value2 = " + value2 + " " +
"value3 = " + value3 + " " +
"value4 = " + value4 + " " +
"s1 = " + s1 + " " +
"s2 = " + s2 + " " +
"s3 = " + s3 + " " +
"s4 = " + s4 + " " +
"i = " + i + " " +
"f = " + f + " " +
"source = " + source;
/**//*这个return语句真的很别致, 学习*/
}
public static void main(String[] args) ...{
SprinklerSystem sprinklers = new SprinklerSystem();
System.out.println(sprinklers);
}
}
/**//* toString(): 每一个非基本类型的对象都有一个toString()方法, 而且当编译器需要一个String而你却只有一个对象时, 给方法会被调用.
* SprinklerSystem.toString()中"source = " + source;指的是: 由于只能将一个String对象和另一个String对象相加, 因此编译器会告诉你: "我将调用toString(), 把source转换成一个String!"(自动类型转换). 每当你想要使所创建的类具备这样的行为时, 编写一个toString()方法覆盖掉默认toString()方法即可.
*/
编译器并不是简单的为每一个引用都创建缺省对象, 这一点是很有意义的. 否则内存负担会很重. 可以在代码的下列位置初始化这些引用:
(1)在定义对象的地方. 这意味着它们总是能够在构造器被调用之前被初始化.
(2)在类的构造器中.
(3)在正要使用这些对象之前, 这种方式称为"惰性初始化". 在生成对象不值得及不必每次都生成对象的情况下, 这种方式可以减少额外的负担.
- Java-Composition for Code Reuse
- Java-Inheritance for Code Reuse
- Code Review for Java code
- Java Composition toString()
- BPEL: Service composition for SOA
- Chromium Anatomy & Code Reuse Series: Hello, World!
- Code Reuse in Google Chrome Browser
- Code Reuse in Google Chrome Browser
- CARP(Composition/Aggregation Reuse Principle)合成/聚合复用原则
- 设计模式之 Putting Reuse Mechanisms to Work and Inheritance versus Composition
- Composition
- 【TensorFlow_Fold】深度探究 Blocks for Composition
- XML for Data Architects: Designing for Reuse and Integration
- Code Conventions for the Java Programming Language
- use FindBugs for Java static code checking
- Code Conventions for the Java Programming Language
- Dynamic Code Evolution for Java dcevm 原理
- Dynamic Code Evolution for Java dcevm 原理
- jbuildre打不开问题
- SPI和I2C的区别
- 44BINIT.S引导程序注释-转
- C#的四种排序算法
- c#时间格式
- Java-Composition for Code Reuse
- Visual Studio 2005 通过静态变量及可系列化的HASHTABLE变量优化工厂模式(Factory)的效率,提升性能(E8.Net工作流应用系统优化系列四)
- struts1和struts2的比较
- bea weblogic workflow worklist 介绍(4)
- Use Conditional Attributes instead of #if - Effective C#学习笔记(4)
- int 0x80 Definition
- 国内各大域名注册机构域名管理面板登陆地址
- 编译器原理及相关
- IIs服务没能及时响应启动或控制请求