接口2

接口类:

package com.daxing_02;
/*
Inter接口名
接口中的成员默认为常量,接口没有构造方法,因为接口主要是对行为进行抽象,默认继承自父类Object类
成员方法默认修饰符public abstract
*/

public interface Inter {
//接口中的成员变量默认被final修饰,不可重新赋值
public int age = 23;
public final int num = 2;
/* 以下两种写法等价
public static final int num1 = 90;
int num2 = 21;*/

//接口里面的方法默认自带public abstract
void show();
}

package com.daxing_02;
//该接口的实现类Impl
public class InterImpl implements Inter{
@Override
public void show() {
System.out.println("show");
}
}

测试类:
package com.daxing_02;

public class InterfaceDemo {
public static void main(String[] args) {
Inter i = new InterImpl();
// i.num=12;
System.out.println(i.num);
System.out.println(i.age);
//可以提供接口名直接访问
System.out.println(Inter.num);
i.show();
}
}

运行结果:

2
23
2
show

Process finished with exit code 0

posted @ 2021-11-23 15:39  小风扇呜呜呜  阅读(42)  评论(0)    收藏  举报