/**
* 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. Problem 8: Largest product in a series

    先粘实现代码,以后需要再慢慢补充思路 s = ''' 73167176531330624919225119674426574742355349194934 9698352031277450632623 ...

  2. 如何在linux环境安装数据库

    1.1      获取oracle 数据库安装包: 注意:获取的是database的安装包,不是客户端的安装包 1.2      以root用户登陆云主机,修改主机名 Hostname 1.2.1   ...

  3. python模块之_pip_其它

    这些模块都是在讲OOP时讲到的. 都是类中内置的. #!/usr/bin/env python # coding:utf-8 from lib.aa import C c1 = C() print(c ...

  4. jsp标签库选择框示例

    <select onchange="yearchange(this);" name="year" id="year"> < ...

  5. vue-router 重难点总结笔记

    1,使用动态路由配置的(如:‘:id’),可以在this.$router.params.id获得. 官网例子: 模式 匹配路径 $route.params /user/:username /user/ ...

  6. Mysql 设置远程连接

    一.问题分析 有时候使用数据库远程连接工具连接MySQL的时候总是连接不上,确认过账号密码正确,端口telnet端口又是通的. Navicat Premium报错如下: 1130 - Host '19 ...

  7. java ftp上传文件 工具类

    package com.learning.spboot.utils; import com.jcraft.jsch.*; import org.apache.commons.net.ftp.FTPCl ...

  8. TP5 生成二维码

    首先下载这个类:http://phpqrcode.sourceforge.net/ 把下载的文件放到vendor下面 public function getWchatQrcode($users_id= ...

  9. canvas绘制随机验证码

    效果图: 思路: 1, 绘制canvas画布,进行基础设置 2.绘制一个矩形 3.设置验证码的随机数 4.设置验证码随机数的随机颜色 5.绘制随机干扰线 6,绘制随机干扰点 经过以上六个步骤,验证码的 ...

  10. struts2+mybatis3+log4j2+bulma+mysql8元工程

    https://pan.baidu.com/s/1Z1YklkKEKrzhS7uM9w6ewQ https://pan.baidu.com/s/1Z1YklkKEKrzhS7uM9w6ewQ