二者的区别快忘得差不多了,从别处看到这文章感觉写的不错,正好复习复习,也给大家分享一下。
抽象类 abstract class
接口 interface
- public class Child extends Children implements Lover{
- public Child(String name) {
- super(name);
- }
- public void printName() {
- System.out.println(super.getName());
- }
- public void love(String name) {
- System.out.println(name + ", I love you!");
- }
- public static void main(String[] args) {
- Child boy = new Child("Charley");
- System.out.println(boy.getName());
- Child girl = new Child("Queenie");
- girl.printName();
- boy.love(girl.getName());
- girl.love(boy.getName());
- }
- }
- abstract class Children {
- private String name;
- public Children(String name) {
- this.name = name;
- }
- //private then Child obj can not access this method
- //private String getName() {
- public String getName() {
- return name;
- }
- abstract void printName();
- //haha()在子类中没有被实现
- //abstract void haha();
- }
- interface Lover{
- void love(String name);
- }
抽象类和接口的应用
- abstract class Door {
- abstract void open();
- abstract void close();
- }
- interface Alarm {
- void alarm();
- }
- class AlarmDoor extends Door implements Alarm {
- void open() { … }
- void close() { … }
- void alarm() { … }
- }
479




被折叠的 条评论
为什么被折叠?



