/**
* Created by Sandy.Liu on 2018/7/28.
* Thinking in java version 4, chapter 5, practice 2
* Create a class which includes two string values, one is initialized at the point of defination,
* another is initialized by constructor. What is the different between the two approach.
*/
public class Chap5Prac2 {
Chap5Prac2(){
s3 = "good bye";
}
String s1;
String s2 = "hello";
String s3;
public static void main(String[] args){
Chap5Prac2 t = new Chap5Prac2();
P.print("t.s1: "+t.s1);
P.print("t.s2: "+t.s2);
P.print("t.s3: "+t.s3);
} }
/**
* Created by Sandy.Liu on 2018/7/29.
* Thinking in java version 4, chapter 5, practice 3
* Create a constructor which prints message. Create a new object.
*/
public class Chap5Prac3DefaultConstructor{
Chap5Prac3DefaultConstructor(){
P.print("this is a default constructor");
}
public static void main(String[] args){ Chap5Prac3DefaultConstructor dc = new Chap5Prac3DefaultConstructor();
}
}
/**
* Created by Sandy.Liu on 2018/7/29.
* Thing in java version 4, chapter 5, practice 4
* Add a overload constructor for the class in practice 3 that takes a string argument and prints it
* along with your message.
*/
public class Chap5Prac4 {
Chap5Prac4(){
P.print("this is a default constructor");
}
Chap5Prac4(String s){
P.print("This is an overload constructor with input parameter: "+s);
}
public static void main(String[] args){
Chap5Prac4 t1 = new Chap5Prac4();
Chap5Prac4 t2 = new Chap5Prac4("Hello");
}
}
/**
* Created by Sandy.Liu on 2018/7/29.
* Thing in java version 4, chapter 5, practice 5
* Create a class named Dog that has overload method bark(). This method will be overloaded by different
* data type. Print all kind of barking() and howling() message. Write a main method to use all these
* overload methods.
*/
public class Chap5Prc5Dog {
void bark(){P.print("no parameter, quiet");}
void bark(byte b){P.print("byte parameter: miao");}
void bark(short s){P.print("short bark: wang");}
void bark(int i){P.print("int bark: wangwang");}
void bark(char c){P.print("char bark: chachacha");}
void bark(float f){P.print("float bark: fafafa");}
void bark(long l){P.print("long bark: lalala");}
void bark(double d){P.print("double bark: doudoudou");}
public static void main(String[] args){
byte b = 1;
short s = 2;
char c = 'c';
Chap5Prc5Dog dog = new Chap5Prc5Dog();
dog.bark();
dog.bark(b);
dog.bark(s);
dog.bark(1);
dog.bark(c);
dog.bark(1L);
dog.bark(1.0f);
dog.bark(1.0);
}
}
/**
* Created by Sandy.Liu on 2018/7/29.
* Thinking in java version 4, chapter 5, practice 6
* Modify previous exercise, make two overloaded methods taking two parameters with different type but
* reverse order, verify that works correctly.
*/
public class Chap5Prac6Dog1 {
void bark(int i, String s){
P.print("The first dog's name is " + s + ", it is "+i+" years old.");
}
void bark(String s, int i){
P.print("The second dog's name is "+ s+", it is "+i+" years old.");
}
public static void main(String[] args){
Chap5Prac6Dog1 dog = new Chap5Prac6Dog1();
dog.bark(1, "dog1");
dog.bark("dog2", 2); }
}
/**
* Created by Sandy.Liu on 2018/7/29.
* Thinking in java version 4, chapter 5, practice 7
* Create a class which hasn't constructor, and then create a object of that class in main() to verify
* if the default constructor is added automatically.
*/
public class Chap5Prac7Dog3 {
void bark(){
P.print("woof");
}
public static void main(String[] args){
Chap5Prac7Dog3 dog = new Chap5Prac7Dog3();
dog.bark();
}
}
/**
* Created by Sandy.Liu on 2018/7/29.
* Thinking in java version 4, chapter 5, practice 8
* Write a class which has two methods. Within the first method, call the second method twice:
* this first time don't use this, and the second time use this.
*/
public class Chap5Prac8 {
void method1(){
method2();
this.method2();
}
void method2(){
P.print("this is method 2");
}
public static void main(String[] args){
Chap5Prac8 test = new Chap5Prac8();
test.method1();
}
}
/**
* Created by Sandy.Liu on 2018/7/30.
* Thinking in java version 4, chapter 5, practice 9
* Write a class with two overload constructors, and then call the second constructor by this in the
* first constructor.
*/
public class Chap5Prac9This {
Chap5Prac9This(int i){
P.print("This is the first constructor, the give number is: "+i);
}
Chap5Prac9This(String s){
this(7);
P.print("this is the second constructor, the string is: "+s);
}
public static void main(String[] args){
Chap5Prac9This ch = new Chap5Prac9This("sandy");
}
}
/**
* Created by Sandy.Liu on 2018/7/30.
* Thinking in java version 4, chapter 5, practice 10
* Write a class with finalize method which types a message. Create a new object in main{} for this
* class, and explain the behavior of the program.
*/
public class Chap5Prac10Finalize {
boolean logout = false;
Chap5Prac10Finalize(boolean logout){
logout = logout;
}
void checkOut(){
logout = false;
}
protected void finalize(){
if(logout)
P.print("error, logout");
}
public static void main(String[] args){
Chap5Prac10Finalize ch = new Chap5Prac10Finalize(true);
ch.checkOut();
new Chap5Prac10Finalize(true);
System.gc(); }
}
												

Thing in java 第5章,初始化和清理,练习题答案的更多相关文章

  1. 《Java编程思想》——初始化与清理(一)读书笔记

    第一次写这个,这一章都用word写的,结果复制过来没图片....只能上传word文档了.以后改用markdown比较好 word文档地址:<Java编程思想>--初始化与清理(一)读书笔记

  2. 20190816 On Java8 第六章 初始化和清理

    第六章 初始化和清理 利用构造器保证初始化 在 Java 中,类的设计者通过构造器保证每个对象的初始化. 构造器名称与类名相同. 在 Java 中,对象的创建与初始化是统一的概念,二者不可分割. 方法 ...

  3. Java编程思想之五初始化与清理

    随着计算机革命的发展,"不安全"的编程方式已经逐渐称为编程代价高昂的主因之一. 初始化和清理正是涉及安全的两个问题. 5.1 用构造器确保初始化 通过提供构造器,类的设计者可确保每 ...

  4. 《java编程思想》 初始化与清理

    1.初始化与清理的重要性: 1.许多C程序的错误都源于程序员忘记初始化变量,特别是使用程序库时,如果不知道如何初始化库的构件更容易出错 2.当使用完一个元素时,这个元素就不会有什么影响了,所以很容易就 ...

  5. 初读"Thinking in Java"读书笔记之第五章 --- 初始化与清理

    用构造器确保初始化 构造器可以确保每个对象都会得到初始化,Java毁在创建对象时自动调用构造器. 构造器采用与类名相同的名称,因此并不适合"每个方法首字母小写的风格". 构造器默认 ...

  6. 《Java编程思想》笔记 第五章 初始化与清理

    1.构造器 因为创建一个类的对象构造器就会自动执行,故初始化某些东西特好 2.方法重载 方法名相同,参数列表不同. 2.1 区分重载方法 方法重载后区别不同方法的就是方法签名 -->参数类型和个 ...

  7. java编程思想第五章初始化与清理

    5.1使用构造器确保初始化: 构造器与一般方法一样,但是没有返回值,且其方法名与类名完全相同. 不接受任何参数的构造器成为默认构造器,也叫无参构造器. 5.2 方法重载: 为什么会有方法重载? 构造器 ...

  8. 【图文+视频新手也友好】Java一维数组详细讲解(内含练习题答案+详解彩蛋喔~)

    目录 视频讲解: 一.数组的概述 二.一维数组的使用 三.Arrays工具类中的sort方法(sort方法用的多,我们具体讲一下) 四.数组中的常见异常 五.一维数组练习题 六.彩蛋(本期视频使用的P ...

  9. Java编程思想学习(五)----第5章:初始化与清理

    随着计算机革命的发展,“不安全”的编程方式已逐渐成为编程代价高昂的主因之一. C++引入了构造嚣(constructor)的概念,这是一个在创建对象时被自动调用的特殊方法.Java中也采用了构造器,并 ...

  10. JAVA 入门第二章 (面对对象)

    本渣渣鸽了一个月终于有时间更新.因为有c++基础,学起来这章还是比较简单的,本章我觉得是程序猿质变课程,理解面向对象的思想,掌握面向对象的基本原则以及 Java 面向对象编程基本实现原理,熟练使用封装 ...

随机推荐

  1. Spring 基础知识(一)基本概念 DI、IOC、AOP

    DI(依赖注入) 和IOC(控制反转)都是一种设计思想,要理解他们,让我们从coding中的一些痛点入手. 依赖注入 Dependency Injection : 如果A类要使用B类的一个方法,首先必 ...

  2. Oracle查询和过滤重复数据

    对数据库某些意外情况,引起的重复数据,如何处理呢? ----------------查重复: select * from satisfaction_survey s and s.project_no ...

  3. 【转】git - 简易指南

    原文链接:http://www.bootcss.com/p/git-guide/ 作者:罗杰·杜德勒 感谢:@tfnico, @fhd and Namics 其他语言 english, deutsch ...

  4. 性能测试LR学习笔录2am pm -3

    回顾day1: 1.什么是性能测试? 模拟真实的生产环境,以各种不同的压力(模拟大量用户)去测试被测系统,去”攻击“测试系统.同时 记录下被测系统中各台服务器的各种重要资源情况,包括cpu.内存.磁盘 ...

  5. 精进之路之AQS及相关组件

    AQS ( AbstractQueuedSynchronizer)是一个用来构建锁和同步器的框架,使用AQS能简单且高效地构造出应用广泛的大量的同步器,比如我们提到的ReentrantLock,Sem ...

  6. 精进之路之CAS

    CAS (Compare And Swap) 即比较交换, 是一种实现并发算法时常用到的技术,Java并发包中的很多类都使用了CAS技术,本文将深入的介绍CAS的原理. 其算法核心思想如下 执行函数: ...

  7. OGNL表达式的一个坑!

    我在写Spring整合JDBC框架的时候用了properties文件去设置JDBC的参数.但是发现了一个问题先付上代码 properties文件的代码 db.driverClass=com.mysql ...

  8. linux 计划任务 访问某个URL

    1.进入crontab文件的编写状态: crontab -e 2.进入编辑器后,按下 “ i ” 键,进入编辑模式,在编辑模式下,我们写上我们这次需要访问执行的脚本: 59 23 * * * /usr ...

  9. leetcode题解 7.Reverse Integer

    题目: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 E ...

  10. java使用poi导出excel

    继上一篇导出pdf,这篇导出excel. 1.导入依赖 <dependency> <groupId>org.apache.poi</groupId> <art ...