接口是java中最重要的概念,接口可以理解为一种特俗的类,里面全部是由全局常量和公共的抽象方法所组成
package come.liuchen.javaextends;
interface Inter{
public static final int AGE=100;
public abstract void tell();
}
public class demo4 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
接口的格式:
interface interfaceName{
全局常量
抽象方法
}
接口的实现也可以通过子类,使用关键字implements,而且接口是可以多实现的
接口不能直接实例化!通过子类在实例化
package come.liuchen.javaextends;
interface Inter1{
public static final int AGE=100;
public abstract void tell();
}
class A implements Inter1{
public void tell() {
}
}
public class demo4 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
A a=new A();
a.tell();
System.out.println(Inter1.AGE);
}
}
一个类可以继承多个接口!
package come.liuchen.javaextends;
interface Inter1{
public static final int AGE=100;
public abstract void tell();
}
class A implements Inter1{
public void tell() {
}
}
public class demo4 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
A a=new A();
a.tell();
System.out.println(Inter1.AGE);
}
}
子类可以同时继承抽象类,接口
package come.liuchen.javaextends;
interface Inter1{
public static final int AGE=100;
public abstract void tell();
}
interface Inter2{
public abstract void say();
}
abstract class Abs1{
public abstract void print();
}
class A extends Abs1 implements Inter1,Inter2{
public void tell() {
}
public void say() {
}
public void print() {
}
}
public class demo4 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
A a=new A();
a.tell();
System.out.println(Inter1.AGE);
a.say();
}
}
一个接口可以同时继承多个接口(弥补了一个类不能同时继承多个类的不足)
package come.liuchen.javaextends;
interface Inter1{
public static final int AGE=100;
public abstract void tell();
}
interface Inter2{
public abstract void say();
}
abstract class Abs1{
public abstract void print();
}
class A extends Abs1 implements Inter1,Inter2{
public void tell() {
}
public void say() {
}
public void print() {
}
}
interface Inter3 extends Inter1,Inter2{
}
public class demo4 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
A a=new A();
a.tell();
System.out.println(Inter1.AGE);
a.say();
System.out.println(Inter3.AGE);
}
}