Thing in java 第5章,初始化和清理,练习题答案
/**
* 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章,初始化和清理,练习题答案的更多相关文章
- 《Java编程思想》——初始化与清理(一)读书笔记
第一次写这个,这一章都用word写的,结果复制过来没图片....只能上传word文档了.以后改用markdown比较好 word文档地址:<Java编程思想>--初始化与清理(一)读书笔记
- 20190816 On Java8 第六章 初始化和清理
第六章 初始化和清理 利用构造器保证初始化 在 Java 中,类的设计者通过构造器保证每个对象的初始化. 构造器名称与类名相同. 在 Java 中,对象的创建与初始化是统一的概念,二者不可分割. 方法 ...
- Java编程思想之五初始化与清理
随着计算机革命的发展,"不安全"的编程方式已经逐渐称为编程代价高昂的主因之一. 初始化和清理正是涉及安全的两个问题. 5.1 用构造器确保初始化 通过提供构造器,类的设计者可确保每 ...
- 《java编程思想》 初始化与清理
1.初始化与清理的重要性: 1.许多C程序的错误都源于程序员忘记初始化变量,特别是使用程序库时,如果不知道如何初始化库的构件更容易出错 2.当使用完一个元素时,这个元素就不会有什么影响了,所以很容易就 ...
- 初读"Thinking in Java"读书笔记之第五章 --- 初始化与清理
用构造器确保初始化 构造器可以确保每个对象都会得到初始化,Java毁在创建对象时自动调用构造器. 构造器采用与类名相同的名称,因此并不适合"每个方法首字母小写的风格". 构造器默认 ...
- 《Java编程思想》笔记 第五章 初始化与清理
1.构造器 因为创建一个类的对象构造器就会自动执行,故初始化某些东西特好 2.方法重载 方法名相同,参数列表不同. 2.1 区分重载方法 方法重载后区别不同方法的就是方法签名 -->参数类型和个 ...
- java编程思想第五章初始化与清理
5.1使用构造器确保初始化: 构造器与一般方法一样,但是没有返回值,且其方法名与类名完全相同. 不接受任何参数的构造器成为默认构造器,也叫无参构造器. 5.2 方法重载: 为什么会有方法重载? 构造器 ...
- 【图文+视频新手也友好】Java一维数组详细讲解(内含练习题答案+详解彩蛋喔~)
目录 视频讲解: 一.数组的概述 二.一维数组的使用 三.Arrays工具类中的sort方法(sort方法用的多,我们具体讲一下) 四.数组中的常见异常 五.一维数组练习题 六.彩蛋(本期视频使用的P ...
- Java编程思想学习(五)----第5章:初始化与清理
随着计算机革命的发展,“不安全”的编程方式已逐渐成为编程代价高昂的主因之一. C++引入了构造嚣(constructor)的概念,这是一个在创建对象时被自动调用的特殊方法.Java中也采用了构造器,并 ...
- JAVA 入门第二章 (面对对象)
本渣渣鸽了一个月终于有时间更新.因为有c++基础,学起来这章还是比较简单的,本章我觉得是程序猿质变课程,理解面向对象的思想,掌握面向对象的基本原则以及 Java 面向对象编程基本实现原理,熟练使用封装 ...
随机推荐
- element-ui(或者说Vue的子组件)绑定的方法中传入自定义参数
比如el-upload中的 :on-success= fn,其实是给组件el-upload传递一个prop,这样写的话fn只能接受upload组件规定的参数,如果想自己传递父组件中的参数比如b,要写成 ...
- Java语法基础学习DayTwentyOne(网络编程)
一.IP地址和端口号 1.作用 通过IP地址,唯一的定位互联网上一台主机. 端口号标识正在计算机上运行的进程,不同进程有不同的端口号,被规定为一个16位的整数0~65535,其中0~1023被预先定义 ...
- LINUX 学习笔记 账号与群组的管理
LINUX 账号与群组的管理 UID:UserID 保存文件:/etc/passwd GID:GroupID 保存文件:/etc/group /etc/passwd 文件结构 一行代表一个账号,里面还 ...
- 文件传输协议(FTP)
文件传输协议(FTP)用于用户在两台主机之间进行远距离的文件传输,并保证传输的可靠性. FTP采用客户机/服务器的方式,由FTP服务器和FTP客户机两部分组成. FTP服务器中以目录结构保存着各种文件 ...
- Labview多列列表框
前面板创建多列列表框 如何写入数据: 右键 创建属性节点 项名 创建属性节点 项符号 创造自定义项符号: 右键 创建调用节点 自定义项符号 设置为自定义符号 然后添加索引号 利用图片与声音 ...
- 手机端flex、字体设置、快速点击
;(function flexible (window, document) { var docEl = document.documentElement ♥1 var dpr = window.de ...
- C# copy() 与 Clone()区别
copy() 与 Clone()都创建了一个新对象 DataTable dt=new DataTable();DataTable dtcopy=dt.copy(); //copy复制的是值和 ...
- Js/Session和Cookies的区别
1.cookies数据存放在客户的浏览器上面,session放在服务器上面.2.cookies不安全,别人可以分析浏览器的数据进行cookies的欺骗,考虑到安全性,应该使用cookie3.sessi ...
- ros 使用笔记
1. publishers/subscribers 常用指令 说明rosnode list 查看所有激活的节点rostopic list 查看所有激活的topicrostopic i ...
- temporal credit assignment in reinforcement learning 【强化学习 经典论文】
Sutton 出版论文的主页: http://incompleteideas.net/publications.html Phd 论文: temporal credit assignment i ...