Best practices and common techniques for Java interface class design
In Java, interface is a specification that defines behavior and can help us achieve modularization of code. , and provides a flexible way to implement polymorphism. This article will introduce some best practices and common techniques for Java interface class design, and provide some specific code examples.
public interface Animal { void sound(); } public class Dog implements Animal { @Override public void sound() { System.out.println("汪汪汪"); } } public class Cat implements Animal { @Override public void sound() { System.out.println("喵喵喵"); } } public class Main { public static void main(String[] args) { Animal dog = new Dog(); Animal cat = new Cat(); dog.sound(); // 输出:汪汪汪 cat.sound(); // 输出:喵喵喵 } }
In the above example, the Animal interface defines a sound() method, and then two classes, Dog and Cat, implement the interface and implement their respective sound() method. By declaring an object using the Animal interface, we can dynamically choose to use an instance of the Dog or Cat class according to the actual situation, and call the same sound() method.
public interface Animal { void sound(); } public interface Flyable extends Animal { void fly(); } public class Bird implements Flyable { @Override public void sound() { System.out.println("叽叽喳喳"); } @Override public void fly() { System.out.println("飞翔中"); } } public class Main { public static void main(String[] args) { Flyable bird = new Bird(); bird.sound(); // 输出:叽叽喳喳 bird.fly(); // 输出:飞翔中 } }
In the above example, the Flyable interface inherits the Animal interface and declares a new method fly(). The Bird class implements the Flyable interface and implements the sound() and fly() methods. By declaring the bird object using the Flyable interface, we can call the sound() and fly() methods.
public interface Animal { void sound(); default void move() { System.out.println("动物在移动"); } } public class Dog implements Animal { @Override public void sound() { System.out.println("汪汪汪"); } } public class Main { public static void main(String[] args) { Animal dog = new Dog(); dog.sound(); // 输出:汪汪汪 dog.move(); // 输出:动物在移动 } }
In the above example, the Animal interface adds a default method move(). When the Dog class implements the Animal interface, it does not need to override the move() method, but you can choose to override the sound() method. By declaring the dog object using the Animal interface, we can call the sound() method and the default move() method.
public interface Animal { void sound(); static void eat() { System.out.println("动物在进食"); } } public class Dog implements Animal { @Override public void sound() { System.out.println("汪汪汪"); } } public class Main { public static void main(String[] args) { Animal dog = new Dog(); dog.sound(); // 输出:汪汪汪 Animal.eat(); // 输出:动物在进食 } }
In the above example, the Animal interface defines a static method eat(). We can call this static method through the interface name without creating an instance of the interface.
Summary:
This article introduces some best practices and common techniques for Java interface class design, including using interfaces to achieve polymorphism, appropriate inheritance of interfaces, default methods of interfaces, and static methods of interfaces. We hope that these examples can help you better understand and apply the concepts related to interface class design.
The above is the detailed content of Optimize the design of Java interface classes and apply common techniques. For more information, please follow other related articles on the PHP Chinese website!