ThinkJava-复用类
- package com.cy.reusing;
- import static com.java.util.Print.*;
- class Cleanser {
- private String s = "Cleanser";
- public void append(String a) { s += a; }
- public void dilute() { append(" dilute()"); }
- public void apply() { append(" apply()"); }
- public void scrub() { append(" scrub()"); }
- public String toString() {
- return s;
- }
- public static void main(String[] args) {
- Cleanser x = new Cleanser();
- x.dilute(); x.apply(); x.scrub();
- print(x);
- }
- }
- public class Detergent extends Cleanser {
- // Change a method:
- public void scrub() {
- append(" Detergent.scrub()");
- super.scrub(); // Call base-class version
- }
- // Add methods to the interface:
- public void foam() { append(" foam()"); }
- // Test the new class:
- public static void main(String[] args) {
- Detergent x = new Detergent();
- x.dilute();
- x.apply();
- x.scrub();
- x.foam();
- print(x);
- print("Testing base class:");
- Cleanser.main(args);
- }
- }
- /* Output:
- Cleanser dilute() apply() Detergent.scrub() scrub() foam()
- Testing base class:
- Cleanser dilute() apply() scrub()
- *///:~
- package com.cy.reusing;
- class Gizmo {
- public void spin() {}
- }
- public class FinalArguments {
- void with(final Gizmo g) {
- //! g = new Gizmo(); // Illegal -- g is final
- }
- void without(Gizmo g) {
- g = new Gizmo(); // OK -- g not final
- g.spin();
- }
- // void f(final int i) { i++; } // Can't change
- // You can only read from a final primitive:
- int g(final int i) {
- return i + 1;
- }
- public static void main(String[] args) {
- FinalArguments bf = new FinalArguments();
- bf.without(null);
- bf.with(null);
- }
- } ///:~
- package com.cy.reusing;
- import static com.java.util.Print.*;
- class WithFinals {
- // Identical to "private" alone:
- private final void f() { print("WithFinals.f()"); }
- // Also automatically "final":
- private void g() { print("WithFinals.g()"); }
- }
- class OverridingPrivate extends WithFinals {
- private final void f() {
- print("OverridingPrivate.f()");
- }
- private void g() {
- print("OverridingPrivate.g()");
- }
- }
- class OverridingPrivate2 extends OverridingPrivate {
- public final void f() {
- print("OverridingPrivate2.f()");
- }
- public void g() {
- print("OverridingPrivate2.g()");
- }
- }
- public class FinalOverridingIllusion {
- public static void main(String[] args) {
- OverridingPrivate2 op2 = new OverridingPrivate2();
- op2.f();
- op2.g();
- // You can upcast:
- OverridingPrivate op = op2;
- // But you can't call the methods:
- //! op.f();
- //! op.g();
- // Same here:
- WithFinals wf = op2;
- //! wf.f();
- //! wf.g();
- }
- }
- /* Output:
- OverridingPrivate2.f()
- OverridingPrivate2.g()
- *///:~
- package com.cy.reusing;
- class SmallBrain {}
- final class Dinosaur {
- int i = 7;
- int j = 1;
- SmallBrain x = new SmallBrain();
- void f() {}
- }
- //! class Further extends Dinosaur {}
- // error: Cannot extend final class 'Dinosaur'
- public class Jurassic {
- public static void main(String[] args) {
- Dinosaur n = new Dinosaur();
- n.f();
- n.i = 40;
- n.j++;
- }
- } ///:~
- package com.cy.reusing;
- import static com.java.util.Print.*;
- class Insect {
- private int i = 9;
- protected int j;
- Insect() {
- print("i = " + i + ", j = " + j);
- j = 39;
- }
- private static int x1 = printInit("static Insect.x1 initialized");
- static int printInit(String s) {
- print(s);
- return 47;
- }
- }
- public class Beetle extends Insect {
- private int k = printInit("Beetle.k initialized");
- public Beetle() {
- print("k = " + k);
- print("j = " + j);
- }
- private static int x2 = printInit("static Beetle.x2 initialized");
- public static void main(String[] args) {
- print("Beetle constructor");
- Beetle b = new Beetle();
- }
- }
- /* Output:
- static Insect.x1 initialized
- static Beetle.x2 initialized
- Beetle constructor
- i = 9, j = 0
- Beetle.k initialized
- k = 47
- j = 39
- *///:~
ThinkJava-复用类的更多相关文章
- [THINKING IN JAVA]复用类
7 复用类 7.1 组合 即在一个类中使用另一个类作为成员变量,这是复用了现有程序代码的功能,而非形式. 7.2 继承 关键字:extends,这种复用是形式的复用,是一种可扩展和限制的复用: 复用: ...
- java编程思想-复用类总结
今天继续读<java 编程思想>,读到了复用类一章,看到总结写的很好,现贴上来,给大家分享. 继承和组合都能从现有类型生成新类型.组合一般是将现有类型作为新类型底层实现的一部分来加以复用, ...
- Java编程思想学习(五) 复用类
1.继承与组合 复用类的方法有两种:继承与组合.继承就不多说了,组合就是直接在类中new一个对象. 数组也是对象,使用数组也是组合的一种. 2.初始化基类 当创建一个导出类的对象时,该对象包含一个基类 ...
- java复用类
java复用类英文名叫reusing classes ,重新使用的类,复用的意思就是重复使用的类,其实现方法就是我们平常使用的组合和继承: 1.组合: has-a 的关系 (自我理解:组合就是我们 ...
- Java基础 -- 复用类(组合和继承)
复用类有两种实现方式. 在新的类中产生现有类的对象,由于新的类是由现有类的对象所组成,所以这种方法称之为组合. 采用继承实现. 一 组合语法 下面创建两个类WaterSource和Sprinkler ...
- 【Thinking in java, 4e】复用类
mark一篇09年的<Thinking in Java>笔记:here --> https://lawrence-zxc.github.io/2009/11/07/thinking- ...
- Java基础—复用类
复用代码是Java众多引人注目的功能之一. 一般而言,实现代码重用java提供了两种方式:组合以及继承. 组合:新的类由现有类的对象所组成.(复用现有代码的功能,而非它的形式) 继承:按照现有类的类型 ...
- 《Think in Java》(七)复用类
Java 中复用代码的方式就是复用类,复用类的方式有: 组合 继承 代理(并没有啥高深的含义,只是在使用类A前,新增了类B,让类B的每个方法去调用类A中对应的方法,也就是说类B代理了类A...不过我还 ...
- Java编程思想(四) —— 复用类
看了老罗罗升阳的专訪,不由自主地佩服,非常年轻,我之前以为和罗永浩一个级别的年龄.也是见过的不是初高中编程的一位大牛之中的一个,专訪之后.发现老罗也是一步一个脚印的人. 别说什么难做,做不了.你根本就 ...
- [Java编程思想] 第七章 复用类
第七章 复用类 第一种方法非常直观:只需在新的类中产生现有类的对象(组合). 第二种方法更细致一些:它按照现有类的类型来创建新类(继承). 7.1 组合语法 只需将对象引用置于新类中即可. cla ...
随机推荐
- java web中的多条件查询
转自:http://blog.csdn.net/xulu_258/article/details/46623317 所谓多条件查询即为用户输入想要查询的条件,然后根据用户输入的条件进行查询. 当用户有 ...
- jQuery 禁用select和取消禁用之disabled
jQuery1.5及以前: 禁用select: $('#groupId').attr('disabled','disabled'); 取消禁用: $('#groupId').removeAttr('d ...
- OSI七层网络模型与TCP/IP四层模型介绍
目录 OSI七层网络模型与TCP/IP四层模型介绍 1.OSI七层网络模型介绍 2.TCP/IP四层网络模型介绍 3.各层对应的协议 4.OSI七层和TCP/IP四层的区别 5.交换机工作在OSI的哪 ...
- SQL SERVER 压缩数据库日志文件
ALTER DATABASE DBNAME SET RECOVERY SIMPLE --设置为简单恢复模式 GO DBCC SHRINKFILE (DBNAME_log, 1) GO ALTER DA ...
- 揭秘Keras推荐系统如何建立模型、获取用户爱好
你是否有过这样的经历?当你在亚马逊商城浏览一些书籍,或者购买过一些书籍后,你的偏好就会被系统学到,系统会基于一些假设为你推荐相关书目.为什么系统会知道,在这背后又藏着哪些秘密呢? 荐系统可以从百万甚至 ...
- Swift 编程语言入门教程
1 简介 今天凌晨Apple刚刚发布了Swift编程语言,本文从其发布的书籍<The Swift Programming Language>中摘录和提取而成.希望对各位的iOS& ...
- 重构Tips
一,重新组织函数1.首先找出局部变量和参数. 1>任何不会被修改的变量都可以当作参数传入.2.去掉临时变量Replace Temp with Query.用查询函数代替临时变量3.Extract ...
- 【c++基础】int转string自动补零
前言 使用to_string函数可以将不同类型的数据转换为string类,请参考here和here.如果string的位数固定,如何进行自动补零呢?请看本文实例! 代码 确定位数,to_string ...
- UnicodeDammit
UnicodeDammit 是BS内置库, 主要用来猜测文档编码. 编码自动检测 功能可以在Beautiful Soup以外使用,检测某段未知编码时,可以使用这个方法: from bs4 import ...
- 51Nod:1265 四点共面
计算几何 修改隐藏话题 1265 四点共面 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出三维空间上的四个点(点与点的位置均不相同),判断这4个点 ...