14
1
package com.github.hcsp.polymorphism;
import java.util.Arrays;
import java.util.List;
public class World {
public static List<Object> objects =
Arrays.asList(new 麻雀(), new 喜鹊(), new 蝴蝶(), new 飞机(), new 救护车(), new 猫(), new 狗());
public static void 会飞的东西飞() {
for (Object obj : objects) {
if (obj instanceof 会飞的东西) {
((会飞的东西) obj).飞();
}
}
}
public static void 会叫的东西叫() {
for (Object obj : objects) {
if (obj instanceof 会叫的东西) {
((会叫的东西) obj).叫();
}
}
}
public static void 动物都能新陈代谢() {
for (Object obj : objects) {
if (obj instanceof 动物) {
((动物) obj).新陈代谢();
}
}
}
static class 麻雀 implements 会飞的东西, 会叫的东西, 动物 {
@Override
public void 叫() {
System.out.println("叽叽喳喳");
}
}
static class 喜鹊 implements 会飞的东西, 会叫的东西, 动物 {
@Override
public void 叫() {
System.out.println("叽叽喳喳");
}
}
static class 蝴蝶 implements 会飞的东西, 动物 {
@Override
public void 飞() {
System.out.println("蝴蝶飞");
}
}
static class 飞机 implements 会飞的东西 {
@Override
public void 飞() {
System.out.println("飞机飞");
}
}
static class 救护车 implements 会叫的东西 {
@Override
public void 叫() {
System.out.println("哇呜哇呜");
}
}
static class 猫 implements 会叫的东西, 动物 {
@Override
public void 叫() {
System.out.println("喵喵喵");
}
}
static class 狗 implements 会叫的东西, 动物 {
@Override
public void 叫() {
System.out.println("汪汪汪");
}
}
interface 动物 {
default void 新陈代谢() {
System.out.println("新陈代谢");
}
}
interface 会飞的东西 {
default void 飞() {
System.out.println("鸟儿飞");
}
}
interface 会叫的东西 {
void 叫();
}
}
2
package com.github.hcsp.polymorphism;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.TreeSet;
public class User implements Comparable<User> {
private final Integer id;
private final String name;
public User(Integer id, String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
User person = (User) o;
return Objects.equals(id, person.id);
}
@Override
public int hashCode() {
return id != null ? id.hashCode() : 0;
}
@Override
public int compareTo(User o) {
if (name.compareTo(o.name) == 0) {
return id.compareTo(o.id);
} else {
return name.compareTo(o.name);
}
}
public static void main(String[] args) {
List<User> users =
Arrays.asList(
new User(100, "b"),
new User(10, "z"),
new User(1, "a"),
new User(2000, "a"));
TreeSet<User> treeSet = new TreeSet<>(users);
System.out.println(treeSet.size());
}
}
package com.github.hcsp.polymorphism;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class Point implements Comparable<Point> {
private final int x;
private final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Point point = (Point) o;
if (x != point.x) {
return false;
}
return y == point.y;
}
@Override
public int hashCode() {
int result = x;
result = 31 * result + y;
return result;
}
@Override
public String toString() {
return String.format("(%d,%d)", x, y);
}
public static List<Point> sort(List<Point> points) {
Collections.sort(points);
return points;
}
public static void main(String[] args) throws IOException {
List<Point> points =
Arrays.asList(
new Point(2, 0),
new Point(-1, 1),
new Point(1, -1),
new Point(2, 1),
new Point(2, -1));
System.out.println(Point.sort(points));
}
@Override
public int compareTo(Point that) {
if (this.x < that.x) {
return -1;
} else if (this.x > that.x) {
return 1;
}
if (this.y < that.y) {
return -1;
} else if (this.y > that.y) {
return 1;
}
return 0;
}
}
3
package com.github.hcsp.polymorphism;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class Point implements Comparable<Point> {
private final int x;
private final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Point point = (Point) o;
if (x != point.x) {
return false;
}
return y == point.y;
}
@Override
public int hashCode() {
int result = x;
result = 31 * result + y;
return result;
}
@Override
public String toString() {
return String.format("(%d,%d)", x, y);
}
public static List<Point> sort(List<Point> points) {
Collections.sort(points);
return points;
}
public static void main(String[] args) throws IOException {
List<Point> points =
Arrays.asList(
new Point(2, 0),
new Point(-1, 1),
new Point(1, -1),
new Point(2, 1),
new Point(2, -1));
System.out.println(Point.sort(points));
}
@Override
public int compareTo(Point that) {
if (this.x < that.x) {
return -1;
} else if (this.x > that.x) {
return 1;
}
if (this.y < that.y) {
return -1;
} else if (this.y > that.y) {
return 1;
}
return 0;
}
}
4
package com.github.hcsp.polymorphism;
import com.sun.org.apache.xpath.internal.operations.Bool;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
public class User {
private final Integer id;
private final 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 List<User> filter(List<User> users, Predicate<User> predicate) {
List<User> results = new ArrayList<>();
for (User user : users) {
if (predicate.test(user)) {
results.add(user);
}
}
return results;
}
public static void main(String[] args) {
filterUsersWithEvenId(Arrays.asList(new User(1, "a"), new User(2, "b")));
}
public static List<User> filterUsersWithEvenId(List<User> users) {
return filter(users, new Predicate<User>() {
@Override
public boolean test(User user) {
return user.id % 2 == 0;
}
});
}
public static List<User> filterZhangUsers(List<User> users) {
return filter(users, user -> user.name.startsWith("张"));
}
public static List<User> filterWangUsers(List<User> users) {
return filter(users, new Predicate<User>() {
@Override
public boolean test(User user) {
return user.name.startsWith("王");
}
});
}
}
5
package com.github.hcsp.polymorphism;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
public class FileFilter {
public static void main(String[] args) throws IOException {
Path projectDir = Paths.get(System.getProperty("user.dir"));
Path testRootDir = projectDir.resolve("test-root");
if (!testRootDir.toFile().isDirectory()) {
throw new IllegalStateException(testRootDir.toAbsolutePath().toString() + "不存在!");
}
List<String> filteredFileNames = filter(testRootDir, ".csv");
System.out.println(filteredFileNames);
}
public static List<String> filter(Path rootDirectory, String extension) throws IOException {
List<String> names = new ArrayList<>();
Files.walkFileTree(rootDirectory, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
System.out.println(file);
if (file.getFileName().toString().endsWith(extension)) {
names.add(file.getFileName().toString());
}
return FileVisitResult.CONTINUE;
}
});
return names;
}
}
6
package com.github.hcsp.polymorphism;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
public class User {
private final Integer id;
private final 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 List<String> collectNames(List<User> users) {
NameCollector collector = new NameCollector() {
private final List<String> names = new ArrayList<>();
@Override
public void accept(User user) {
names.add(user.getName());
}
@Override
public List<String> getNames() {
return names;
}
};
users.forEach(collector);
return collector.getNames();
}
public static void main(String[] args) {
List<User> users = Arrays.asList(new User(1, "a"), new User(2, "b"));
System.out.println(collectNames(users));
}
public static class NameCollector implements Consumer<User> {
@Override
public void accept(User user) {
}
private final List<String> names = new ArrayList<>();
public List<String> getNames() {
return names;
}
}
}
7
package com.github.hcsp.polymorphism;
public class Cat {
private final String name;
public Cat(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
package com.github.hcsp.polymorphism;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
public class Home {
List<Cat> cats = new ArrayList<>();
public List<String> getCatNames() {
CatNameCollector collector = new CatNameCollector();
cats.forEach(collector);
return collector.getCatNames();
}
private void log(Cat cat) {
System.out.println("Collecting cat " + cat.getName());
}
class CatNameCollector implements Consumer<Cat> {
private List<String> catNames = new ArrayList<>();
@Override
public void accept(Cat cat) {
log(cat);
catNames.add(cat.getName());
}
private List<String> getCatNames() {
return catNames;
}
}
}
package com.github.hcsp.polymorphism;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
public class Home2 {
List<Cat> cats = new ArrayList<>();
public List<String> getCatNames() {
CatNameCollector collector = new CatNameCollector(this);
cats.forEach(collector);
return collector.getCatNames();
}
private void log(Cat cat) {
System.out.println("Collecting cat " + cat.getName());
}
static class CatNameCollector implements Consumer<Cat> {
private Home2 this$0;
private List<String> catNames = new ArrayList<>();
public CatNameCollector(Home2 this$0) {
this.this$0 = this$0;
}
@Override
public void accept(Cat cat) {
this$0.log(cat);
catNames.add(cat.getName());
}
private List<String> getCatNames() {
return catNames;
}
}
}