/**
* 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. ecplise导入项目报错而文件不报错

    第一步 确认项目的jdk和tomcat版本,在Java Build Path的Libraries中查看包是否有报错,按照本机环境进行配置 第二步 检查 Java Compiler中的jdk版本,和ec ...

  2. 获取百度地图POI数据一(详解百度返回的POI数据)

    POI是一切可以抽象为空间点的现实世界的实体,比如餐馆,酒店,车站,停车场等.POI数据具有空间坐标和各种属性,是各种地图查询软件的基础数据之一.百度地图作为国内顶尖的地图企业,其上具有丰富的POI数 ...

  3. clusterware启动顺序——CRSD

    CRSD层面 1.启动过程 )导致">CRSD无法启动集群的应用程序资源的可能原因有:"> 原因:/etc/oracle/ocr.loc指向了错误的OCR文件 [gri ...

  4. element-ui(或者说Vue的子组件)绑定的方法中传入自定义参数

    比如el-upload中的 :on-success= fn,其实是给组件el-upload传递一个prop,这样写的话fn只能接受upload组件规定的参数,如果想自己传递父组件中的参数比如b,要写成 ...

  5. tcp的粘包现象与解决方案

    粘包现象: 粘包1:连续的小包,会被优化机制给合并 粘包2:服务端一次性无法完全就收完客户端发送的数据,第二再次接收的时候,会接收到第一次遗留的内容 模拟一个粘包现象 服务端 import socke ...

  6. 解决nim db_mysql could not load: libmysql.dll的问题

    title: 解决nim db_mysql could not load: libmysql.dll的问题 nim中使用db_mysql 操作数据库的代码看起来很简单: import db_mysql ...

  7. 用Webstrom搭建Vue项目

    一.首先要有Node.js   Webpack环境 1.Node.js:是一个能够在服务器端运行JavaScript的开放源代码,跨平台JavaScript运行环境.Node采用Google开发的V8 ...

  8. 第一次靶场练习:SQL注入(1)

    SQL注入1 本文章目的是对相关的黑客内容进一步了解,如有人违反相关的法律法规,本人概不负责 一.学习目的: 利用手工注入网站 利用sqlmab注入 二.附件说明 靶场网址:http://117.41 ...

  9. linux date使用

    Linux date 格式化时间和日期 [root@linuxidc ~]# date -d today +"%Y-%m-%d"  2016-11-26 [root@linuxid ...

  10. code about led_shake

    //write by:cyt //Project Name:Led on/off //Time:2017-2-10 #include<reg51.h> void delay(int c) ...