Thing in java 第5章,初始化和清理,练习题答案
Posted Me-Newbie
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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章,初始化和清理,练习题答案的主要内容,如果未能解决你的问题,请参考以下文章