class Demo1_Interface { public static void main(String[] args) { //Inter i = new Inter(); //接口不能被实例化,由于调用抽象方法没有意义 Inter i = new Demo(); //多态:父类引用指向子类对象 i.print(); } } /* * A:接口概述 * 从狭义的角度讲就是指java中的interface * 从广义的角度讲对外提供规则的都是接口 * B:接口特色 * a:接口用关键字interface表示 * interface 接口名 {} * b:类实现接口用implements表示 * class 类名 implements 接口名 {} * c:接口不能实例化 * 那么,接口如何实例化呢? * 按照多态的方式来实例化。 * d:接口的子类 * a:能够是抽象类。可是意义不大。 * b:能够是具体类。要重写接口中的全部抽象方法。(推荐方案) * C:案例演示 * 接口特色 */ interface Inter { public abstract void print(); //接口中的方法都是抽象的 } class Demo implements Inter { public void print() { System.out.println("print"); } }
class Demo2_Interface { public static void main(String[] args) { Demo d = new Demo(); d.print(); System.out.println(Inter.num); } } /* * 成员变量;只能是常量,而且是静态的并公共的。 * 默认修饰符:public static final 三个关键字能够互相交换位置 * 建议:本身手动给出。 * 构造方法:接口没有构造方法。 * 成员方法:只能是抽象方法。 * 默认修饰符:public abstract * 建议:本身手动给出。 */ interface Inter { public static final int num = 10; //public Inter(){} 接口中没有构造方法 /*public void print() { 接口中不能定义非抽象方法 }*/ public abstract void print(); } class Demo /*extends Object*/ implements Inter { //一个类不写继承任何类,默认继承Object类 public void print() { //num = 20; System.out.println(num); } public Demo() { super(); } }
class Demo3_Interface { public static void main(String[] args) { System.out.println("Hello World!"); } } /* * A:类与类,类与接口,接口与接口的关系 * a:类与类: * 继承关系,只能单继承,能够多层继承。 * b:类与接口: * 实现关系,能够单实现,也能够多实现。 * 而且还能够在继承一个类的同时实现多个接口。 * c:接口与接口: * 继承关系,能够单继承,也能够多继承。 */ interface InterA { public abstract void printA(); } interface InterB { public abstract void printB(); } interface InterC extends InterB,InterA { } //class Demo implements InterA,implements InterB { //这么作不容许是非法的 class Demo extends Object implements InterA,InterB { public void printA() { System.out.println("printA"); } public void printB() { System.out.println("printB"); } }
A:成员区别java
B:关系区别this
C:设计理念区别spa
class Test1_Animal { public static void main(String[] args) { Cat c = new Cat("加菲",8); c.eat(); c.sleep(); JumpCat jc = new JumpCat("跳高猫",3); jc.eat(); jc.sleep(); jc.jump(); } } /* * A:案例演示 * 动物类:姓名,年龄,吃饭,睡觉。 * 猫和狗 * 动物培训接口:跳高 */ abstract class Animal { private String name; //姓名 private int age; //年龄 public Animal() {} //空参构造 public Animal(String name,int age) {//有参构造 this.name = name; this.age = age; } public void setName(String name) { //设置姓名 this.name = name; } public String getName() { //获取姓名 return name; } public void setAge(int age) { //设置年龄 this.age = age; } public int getAge() { //获取年龄 return age; } public abstract void eat(); //吃饭 public abstract void sleep(); //睡觉 } interface Jumping { //跳高的接口 public void jump(); } class Cat extends Animal { public Cat() {} //空参构造 public Cat(String name,int age) {//有参构造 super(name,age); } public void eat() { System.out.println("猫吃鱼"); } public void sleep() { System.out.println("侧着睡"); } } class JumpCat extends Cat implements Jumping { public JumpCat() {} //空参构造 public JumpCat(String name,int age) {//有参构造 super(name,age); } public void jump() { System.out.println("猫跳高"); } }