12
package com.github.hcsp.inheritance;
public class Animal {
String name;
public Animal(String name) {
this.name = name;
}
public void sayMyName() {
System.out.println("我的名字是" + name);
}
}
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);
}
}
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")));
}
@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);
}
}
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"));
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
package com.github.hcsp.inheritance;
public class MonsterStory extends Story {
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("你还想听吗");
}
}
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);
}
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.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));
}
}
}
}
package com.github.hcsp.inheritance;
public class Base {
public void sayHello() {
System.out.println("我是父类!");
}
}
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;
}
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());
}
}