Java编程思想第五章初始化与清理练习题解答

版权声明:博客为作者平时学习备忘,参考资料已在文尾列出一并表示感谢。如若转载,请列明出处。 https://blog.csdn.net/woai8339/article/details/84074781

1、构造器

练习1

创建一个类,它包含一个未初始化的string引用。验证该引用被初始化成了null

package ch5.P77;

public class Exercise1 {
    String s;
    public static void main(String[] args) {
        Exercise1 exercise1 = new Exercise1();
        System.out.println(exercise1.s);
    }
}

输出是

null

Process finished with exit code 0

练习2

练习2: 创建一个类,它包含一个在定义时就被初始化了的String域,以及另一个通过构造器初始化的String域。这两种方式有何差异。

答:一开始就被初始化的String域优先于通过构造器初始化的String域。

package ch5.P77;

public class Exercise2 {
    String s1 = "s1";
    String s2;
    {System.out.println("before constructor s1 = " + s1 + ", s2 = " + s2);} //必须要加 {}

    // constructor
    public Exercise2(String s2){
        this.s1 = s1;
        System.out.println("in constructor s1 = " + s1 + ", s2 = " + s2);
    }


    public static void main(String[] args) {
        Exercise2 exercise2 = new Exercise2("s2");
    }

}

输出结果是:

before constructor s1 = s1, s2 = null
in constructor s1 = s1, s2 = s2

Process finished with exit code 0

3、方法重载

简单地说,方法重载可以用于是同一个方法名不同参数类型的函数应用中。比如:

package ch5.override;


class Tree{
    int height;
    Tree(){
        System.out.println("planting a seedling");
        height = 0;
    }

    Tree(int initialHeight){
        height = initialHeight;
        System.out.println("Creating new Tree that is " + height + " feel tall");
    }

    void info(){
        System.out.println("Tree is " + height + " feet tall");
    }
    void info(String s){
        System.out.println(s + ":Tree is " + height + " feet tall");
    }
}

public class Overloading {
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++){
            Tree t = new Tree();  //constructor, output is planting a seedling
            t.info(); //info function,output is Tree is 0 feet tall
            t.info("over loaded method"); //override function,output is over loaded method:Tree is 0 feet tall
        }

        // overloaded constructor:
        new Tree();  //output: planting a seedling
    }
}

首先,构造了一个Tree的构造器,构造器里有两个方法,一个void方法,一个info方法,然后void重载了一个带String的参数,info方法也重载一个带String的参数。在main里先实例化Tree,并输出各个方法。

这个重载是依据参数来判别具体使用哪一个方法。
当然,也可以通过参数顺序判断,但不推荐!

package ch5.override;


import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

public class Overloading2 {
    static void f(String s, int i){
        System.out.println("String s: " + s + ", int i: " + i);
    }
    static void f(int i, String s){
        System.out.println("int i: " + i + ", String s: " + s);
    }

    public static void main(String[] args) {
        f(2, "s");
        f("s", 2);
    }

}
//output:
int i: 2String s: s
String s: s, int i: 2

Process finished with exit code 0

练习3

练习3: 创建一个带默认构造器(即无参构造器)的类,在构造器中打印一条消息。为这个类创建一条对象。

package ch5.P77;

class Birld{
    Birld() {
        System.out.println("Hello world");
    }
}

public class Excercise3 {
    public static void main(String[] args) {
        Birld birld = new Birld();
    }
}

练习4

package ch5.P77;


class Bird{
    String fly;
    Bird(){
        System.out.println("no para");
        }
    Bird(String fly){
        System.out.println("no para " + fly);
        }
}


public class Exercise4 {
    public static void main(String[] args) {
        Bird bird = new Bird("fly");
    }
}

练习5

package ch5.P77;

class Dog{
    Dog(){
        System.out.println(" ");
    }
    Dog(String s){
        System.out.println(s);
    }

    Dog(int i){
        System.out.println(i);
    }
}

public class Exercise5 {
    public static void main(String[] args) {
        Dog dog = new Dog();

    }

}

练习6

package ch5.P77;

    class test{
        test(String str, Integer i){
            System.out.println(String.format("String: %s, Integer:%s",str, String.valueOf(i)));
        }

        test(Integer i, String str){
            System.out.println(String.format("Integer:%s, String: %s", String.valueOf(i), str));
        }
    }

public class Exercise6 {
    public static void main(String[] args) {
        String str = "str";
        Integer i = 10;
        test t = new test(str, i);
        test t2 = new test(i, str);

    }

}

练习7

package ch5.P77;

class NonConstructor{
    void non(){
        System.out.println("hello world");
    }
}

public class Exercise7 {
    public static void main(String[] args) {
        NonConstructor nc = new NonConstructor();
        nc.non();
    }
}

练习8

package ch5.P77;

import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

class ex7{
    public String str = "";
    void ex(){
        System.out.println("ex");
    }
    void exthis(){
        ex();
        this.ex();
        System.out.println("exthis");
    }
}

class Doc {
    void intubate() {
        System.out.println("prepare patient");
        laryngoscopy();
        this.laryngoscopy();
    }
    void laryngoscopy() {
        System.out.println("use laryngoscope");
    }
}

public class Excercise8 {
    public static void main(String[] args) {
        new Doc().intubate();
    }
}

练习9

package ch5.P77;

class Animal{
    String str = "hello";
    Animal(){
        this("123");
        System.out.println("dog");
    }

    Animal(String str){
        str = this.str; //调用内部的str,hello而不是123
        System.out.println("String:" + str);
    }

    void ref(String str){
        System.out.println("String:" + str);
    }
}

public class Exercise9 {
    public static void main(String[] args) {
        String str = "world";
        Animal animal = new Animal();
        animal.ref(str);
    }
}

/**
 * result:
 * String:hello
 * dog
 * String:world
 * **/

练习10

package ch5.P77;

class Book{
    protected void finalize() throws Throwable {
//        super.finalize();
        System.out.println("finalize");
    }

}

public class Excercise10 {
    public static void main(String[] args) throws Throwable {
        Book book = new Book();
        book.finalize();
    }
}

练习14

package ch5.P77;

import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

class Cup{
    String str2 = "str2";
}

public class Exercise14 {
    public static void main(String[] args) {
        String str1 = "str1";
        System.out.println(str1);
        Cup cup = new Cup();
        System.out.println(cup.str2);
        System.out.println(str1 + cup.str2);
    }
}

//output:
/***
 * str1
 * str2
 * str1str2
 * */

练习16

package ch5.P77;

public class Excercise16 {
    public static void main(String[] args) {
        String[] a = {"a", "b", "c"};
        for (String str:a){
            System.out.println(str);
        }
    }
}

练习17

package ch5.P77;

class cls{
    cls(String string){
        System.out.println("String:" + string);
    }
}

public class Exercise17 {
    public static void main(String[] args) {
        cls[] c = new cls[10];
    }
}
//output:

Process finished with exit code 0

练习18

package ch5.P77;

class array{
    array(String string){
        System.out.println("String: " + string);
    }
}

public class Exercise18 {
    public static void main(String[] args) {
        array[] a = new array[3];
        for (int i = 0; i < a.length; i++){
            a[i] = new array(Integer.toString(i));
        }
        System.out.println(a);
    }
}

/**
 * output:
 * String: 0
 * String: 1
 * String: 2
 * [Lch5.P77.array;@677327b6
 * **/

练习19

package ch5.P77;

public class Exercise19 {
    static void f(String... strings){  //String... 表示多个字符串
        for (int i = 0; i < strings.length; i++){
            System.out.print(strings[i] + " ");
        }
    }

    public static void main(String[] args) {
        f("one", "two", "three");
        f(new String[]{"1", "2", "3"});
    }
}

练习20

package ch5.P77;

public class Exercise20 {
    public static void main(String... args) {
        for (String string:args){
            System.out.println(string);
        }

    }
}

Enumswitch结合的妙用

将以下代码命名为Spiciness.java

package ch5.P77.P106;

public enum Spiciness {
    NOT,
    MILD,
    MEDIUM,
    HOT,
    FLAMING
}

将以下代码命名为Burrito.java:

package ch5.P77.P106;

public class Burrito {
    Spiciness degree;
    public Burrito(Spiciness degree){
        this.degree = degree;
    } //用来和switch 结合

    public void describe(){
        System.out.print("The burrito is :");
        switch (degree){
            case NOT:
                System.out.println("not spicy at all");
                break;
            case MILD:
            case MEDIUM:
                System.out.println("a little not");
                break;
            case FLAMING:
            case HOT:
            default:
                System.out.println("may be too hot");
        }
    }

    public static void main(String[] args) {
        Burrito
                plain = new Burrito(Spiciness.NOT),
                greenChile = new Burrito(Spiciness.MEDIUM),
                jalapeno = new Burrito(Spiciness.HOT);
        plain.describe();
        greenChile.describe();
        jalapeno.describe();
    }
}
/**output:
 *The burrito is :not spicy at all
 * The burrito is :a little not
 * The burrito is :may be too hot
 *
 * Process finished with exit code 0
 * **/

练习21

ordinal.java:

package ch5.P77.Exercise21;

public enum ordinal {
    ONE,
    TWO,
    FIVE,
    TEN,
    FIFTY,
    HUNDRED
}

Ex21.java:

package ch5.P77.Exercise21;

public class Ex21 {
    public static void main(String[] args) {
        for (ordinal i:ordinal.values()){
            System.out.println("values: " + i + ", ordinal: " + i.ordinal());
        }
    }
}
/**
 * output:
 * values: ONE, ordinal: 0
 * values: TWO, ordinal: 1
 * values: FIVE, ordinal: 2
 * values: TEN, ordinal: 3
 * values: FIFTY, ordinal: 4
 * values: HUNDRED, ordinal: 5
 *
 * Process finished with exit code 0
 * **/

练习22

ord.java:

package ch5.P77.Exercise22;

public enum ord {
    ONE,
    TWO,
    FIVE,
    TEN,
    FIFTY,
    HUNDRED
}

Ex22.java:

package ch5.P77.Exercise22;

public class Ex22 {
    ord value;
    public Ex22(ord value){
        this.value = value;
    }
    public void describe(){
        switch (value){
            case ONE:
                System.out.println("This is one");
                break;
            case TWO:
                System.out.println("This is TWO");
                break;
            case FIVE:
                System.out.println("This is Five");
                break;
            case TEN:
                System.out.println("This is TEN");
                break;
            case FIFTY:
                System.out.println("This is fifty");
                break;
            case HUNDRED:
                System.out.println("This is hundred");
                break;
            default:
                System.out.println("Error");
                break;

        }
    }

    public static void main(String[] args) {
        Ex22
                one = new Ex22(ord.ONE),
                two = new Ex22(ord.TWO),
                five = new Ex22(ord.FIVE),
                ten = new Ex22(ord.TEN),
                fifty = new Ex22(ord.FIFTY),
                hundred = new Ex22(ord.HUNDRED);
        one.describe();
        two.describe();
        five.describe();
        ten.describe();
        fifty.describe();
        hundred.describe();
    }
}
/**
 * This is one
 * This is TWO
 * This is Five
 * This is TEN
 * This is fifty
 * This is hundred
 *
 * Process finished with exit code 0
 * **/

Ref:
1、Java编程思想
2、http://buptchj.iteye.com/blog/2247538
3、http://greggordon.org/java/tij4/solutions.htm

猜你喜欢

转载自blog.csdn.net/woai8339/article/details/84074781
今日推荐