JAVA -interface 接口笔记

package com.xiaoqiang.study.oop.interfaceDemo;

/**
 * 接口就是多个类的公共规范
 * 接口是一种引用数据类型,最重要的内容就是其中的抽象方法
 * 定义接口使用 interface 关键字替换class关键字
 * 
 * 
 * 
 * @author xiaoqiang
 *
 */
public class InterfaceDemo {
	public static void main(String[] args) {
//		InterfaceAbstract interfaceAbstract = new InterfaceAbstract(); // 错误,接口不能被直接实例化
		InterfaceAbstractImpl in = new InterfaceAbstractImpl();//正确,使用接口的实现类实例化
		System.out.println("=======实现类实现接口方法=========");
		in.method1();
		in.method2();
		String method3 = in.method3();
		System.out.println(method3);
		
		System.out.println("=======实现类调用接口默认方法=========");
		in.method4();//调用默认方法
		
		System.out.println("======实现类重写接口默认方法=======");
		InterfaceAbstractImpl2 in2 = new InterfaceAbstractImpl2();
		in2.method4();
		
		System.out.println("======接口静态方法调用=======");
//		in2.staticMethod();//错误:实现类调用接口方法是错误的,必须使用接口名调用
		InterfaceAbstract.staticMethod();//正确
		
//		System.out.println("=========jdk1.9 私有方法调用=======");
//		in2.callPrivateMethod();
		
		System.out.println("==========接口常量调用=======");
		int num = InterfaceAbstract.NUM;
		System.out.println(num);
	}
}

/**
 * 接口中可以拥有的内容
 * 	JDK 1.7
 * 		1.常量
 * 		2.抽象方法(任何版本)
 * 	jdk 1.8
 * 		1.默认方法
 * 		2.静态方法
 * 	jdk 1.9
 * 		私有方法
 * @author xiaoqiang
 *
 */
interface InterfaceAbstract{
	
	/**
	 * 接口中的变量只能定义常量
	 * 	格式: public static final 变量类型 变量名 = 值;
	 * 调用方式:使用接口名字调用
	 * public static final 在接口中可有省略不写,默认常量
	 * 接口中的常量必须手动赋值,常量赋值后是不允许修改的
	 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值