12

  • 1
package com.github.hcsp.inheritance;

public class Animal {
    String name;


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

    public void sayMyName(a) {
        System.out.println("My name is."+ name); }}Copy the code
  • 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(a) {
        System.out.println("My name is." + name);
    }

    public void sayMyAge(a) {
        System.out.println("My age is."+ age); }}Copy the code
  • 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(a) {
        return id;
    }

    public String getName(a) {
        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")));
    }

    // Override the equals method here so that equals returns true for two users with the same ID


    @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(a) {
        returnObjects.hash(id); }}Copy the code
  • 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(a) {
        return id;
    }

    public String getName(a) {
        return name;
    }

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

    // Override the toString method here so that the user id and name can be printed when the user is printed

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

public class MonsterStory extends Story {
    // Complete this class so that the main method can output the following:
    //
    // Start the story
    // Once upon a time there was an old monster
    That's the end of the story
    // Do you want to hear more
    public static void main(String[] args) {
        new MonsterStory().tellStory();
    }

    public MonsterStory(a) {}public void story(a) {
        System.out.println("Once upon a time there was an old monster.");
    }

    @Override
    public void endStory(a) {
        super.endStory();
        System.out.println("Do you want to hear more?"); }}Copy the code
  • 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);
    }

    /** * Given a list of arbitrary objects, classify them as follows: If the object is of type Number, place it in a numberList; If the object is a String, put it in a stringList; * Otherwise, put it into otherList. * *@paramList The given list of arbitrary objects *@paramNumberList is used to receive a list of all Number objects *@paramStringList is used to receive a list of all strings *@paramOtherList is used to receive a list of all other types of objects */
    public static void classify( List list, List
        
          numberList, List
         
           stringList, List
           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)); }}}}Copy the code
  • 7
package com.github.hcsp.inheritance;

public class Base {
    public void sayHello(a) {
        System.out.println("I'm a parent!"); }}Copy the code
  • 8
package com.github.hcsp.inheritance;

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

public class CountingSet {
    /** * Count the number of elements added to the collection "ever" */
    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(a) {
        return set.size();
    }

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

    public int getCount(a) {
        return count;
    }

    // We want to create a Set that counts the number of elements "ever" added to it
    // However, now the result is clearly wrong
    // Try to fix this problem
    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()); }}Copy the code