12初级 - 面向对象:组合与继承-练习

1,167 阅读2分钟

12

  • 1
package com.github.hcsp.inheritance;

public class Animal {
    String name;


    public Animal(String name) {
        this.name = name;
    }

    public void sayMyName() {
        System.out.println("我的名字是" + name);
    }

}
  • 2
package com.github.hcsp.inheritance;

public class Animal {
    private String name;
    private int age;

    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Animal(String name) {
        this.name = name;
    }

    public void sayMyName() {
        System.out.println("我的名字是" + name);
    }

    public void sayMyAge() {
        System.out.println("我的年龄是" + age);
    }
}
  • 3
package com.github.hcsp.inheritance;

import java.util.Objects;

public class User {
    private Integer id;
    private String name;

    public User(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public static void main(String[] args) {
        System.out.println(new User(1, "user1") == new User(1, "user1"));
        System.out.println(new User(1, "user1").equals(new User(1, "user1")));
        System.out.println(new User(1, "user1").equals(new User(2, "user2")));
    }

    // 请在这里覆盖equals方法,使得两个相同ID的用户equals返回true


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return Objects.equals(id, user.id) && Objects.equals(name, user.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}
  • 4
package com.github.hcsp.inheritance;

public class User {
    private Integer id;
    private String name;

    public User(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public static void main(String[] args) {
        System.out.println(new User(1, "user1"));
    }

    // 请在这里覆盖toString方法,使得在打印用户时能将用户的id和name打印出来

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}
  • 5
package com.github.hcsp.inheritance;

public class MonsterStory extends Story {
    // 请补全本类,使得main方法可以输出以下内容:
    //
    // 开始讲故事啦
    // 从前有个老妖怪
    // 故事讲完啦
    // 你还想听吗
    public static void main(String[] args) {
        new MonsterStory().tellStory();
    }

    public MonsterStory() {
    }

    public void story() {
        System.out.println("从前有个老妖怪");
    }

    @Override
    public void endStory() {
        super.endStory();
        System.out.println("你还想听吗");
    }

}
  • 6
package com.github.hcsp.inheritance;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Classifier {
    public static void main(String[] args) {
        List<Object> list = Arrays.asList("0", 1, 2L, "3", new Object());
        List<Number> numbers = new ArrayList<>();
        List<String> strings = new ArrayList<>();
        List<Object> others = new ArrayList<>();

        classify(list, numbers, strings, others);

        System.out.println("numbers = " + numbers);
        System.out.println("strings = " + strings);
        System.out.println("others = " + others);
    }

    /**
     * 给定一个包含任意对象的列表,将其按照以下方式分类: 如果对象是Number类型,将其放入numberList; 如果对象是String类型,将其放入stringList;
     * 否则,将其放入otherList。
     *
     * @param list       给定的包含任意对象的列表
     * @param numberList 用于接收所有Number对象的列表
     * @param stringList 用于接收所有String对象的列表
     * @param otherList  用于接收其余所有类型对象的列表
     */
    public static void classify(
            List<Object> list,
            List<Number> numberList,
            List<String> stringList,
            List<Object> otherList) {
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
//            if(list.indexOf(i))
            if (list.get(i) instanceof Number) {
                numberList.add((Number) list.get(i));
            } else if (list.get(i) instanceof String) {
                stringList.add((String) list.get(i));
            } else {
                otherList.add(list.get(i));
            }
        }

    }
}

  • 7
package com.github.hcsp.inheritance;

public class Base {
    public void sayHello() {
        System.out.println("我是父类!");
    }
}
  • 8
package com.github.hcsp.inheritance;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;

public class CountingSet {
    /**
     * 统计"有史以来"向该集合中添加过的元素个数
     */
    HashSet<Object> set = new HashSet<>();
    private int count = 0;

    public boolean remove(Object obj) {
        return set.remove(obj);
    }

    public boolean add(Object obj) {
        count++;
        return set.add(obj);
    }

    public boolean addAll(Collection c) {
        count += c.size();
        return set.addAll(c);
    }

    public int size() {
        return set.size();
    }

    public Boolean removeAll(Collection c) {
        return set.removeAll(c);
    }

    public int getCount() {
        return count;
    }

    // 我们希望创建一个Set,能够统计"有史以来"添加到其中去的元素个数
    // 但是,现在结果明显不对
    // 请尝试修复此问题
    public static void main(String[] args) {
        CountingSet countingSet = new CountingSet();
        countingSet.add(new Object());
        countingSet.addAll(Arrays.asList(1, 2, 3));

        System.out.println(countingSet.getCount());
    }
}