Java编程思想第四版第十五章习题(一)

本文介绍了Java编程思想第四版第十五章的部分习题解答,涉及泛型、内部类、继承与组合、迭代器等概念。具体包括使用Holder3存储基类与派生类对象、创建三元组持有类、实现SixTuple、泛型化内部类Sequence、移除Node类的类型参数、使用RandomList处理不同类型的元素、用组合替代继承实现Fibonacci迭代、创建故事角色类层级、修改GenericMethods以接受不同类型的参数等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 Use Holder3 with the typeinfo.pets library to show that a Holder3 that is specified to hold a base type can also hold a derived type.

package job;
import typeinfo.pets.*;
class Holder<T> {
    private T a;

    Holder(T a) {
        this.a = a;
    }

    void f() {
        System.out.println(a);
    }

    void set(T a) {
        this.a = a;
    }
}
    public class Ja15_1 {
        public static void main(String[] args) {
            Holder<Pet> h = new Pet("abcd");
            h.f();
            h.set(new Dog("qwer"));
            h.f();
            h.set(new Cat("asdf"));
            h.f();
        }
    }

    

2 Create a holder class that holds three objects of the same type along with the methods to store and fetch those objects and a constructor to initialize all three.

package job;
import java.util.*;
class Holder<T> {
    private T a;
    private T b;
    private T c;
    void setA(T a) {
        this.a = a;
    }

    void setB(T b) {
        this.b = b;
    }

    void setC(T c) {
        this.c = c;
    }

    void getA() {
        System.out.println(a);
        System.out.println(a.getClass().getName());
    }

    void getB() {
        System.out.println(b);
        System.out.println(a.getClass().getName());
    }

    void getC() {
        System.out.println(c);
        System.out.println(a.getClass().getName());
    }
    Holder(T a,T b,T c){
        this.a=a;
        this.b=b;
        this.c=c;
    }
}
public class Main{
    public static void main(String args[]){
        Holder a=new Holder(2,4.0d,5.222f);
        a.getA();
        a.getB();
        a.getC();
    }
}

output:

2
java.lang.Integer
4.0
java.lang.Integer
5.222
java.lang.Integer

3 Create and test a SixTuple generic.

package job;
import java.util.*;
import net.mindview.util.*;
import typeinfo.pets.*;
import static net.mindview.util.Print.*;
public class SixTuple<A,B,C,D,E,F> extends FiveTuple<A,B,C,D,E> {

    public final F sixth;

    SixTuple(A a, B b, C c, D d, E e, F f) {

        super(a, b, c, d, e);

        sixth = f;

    }

    public String toString() {
        return "(" + first + ", " + second + ", " + third + ", " + fourth + ", " + fifth + ", " + sixth + ")";
    }
}
public class Main{
    public static void main(String[] args) {

        SixTuple<Cat, Dog, Pug, Pet, String, Integer> ja = new Ja15_3<Cat, Dog, Pug, Pet, String, Integer>(new Cat(), new Dog(), new Pug(), new Pet(), "das", 56);

        System.out.println(ja);
    }
}

4 Generify' innerclasses/Sequence.java.

package job;
interface Selector<TT> {

    boolean end();

    TT current();

    void next();

}

public class Main<TT> {

    private TT[] items;

    private int next = 0;

    public Main(TT[] mm) {
        items = mm;
    }

    public void add(TT x) {

        if (next < items.length)

            items[next++] = x;

    }

    private class A implements Selector<TT>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值