Java接口【final、interface】
一、final(掌握)
1.1 final修饰变量
-
生活生产中有些数据是不可变的,做成常量
-
使用final修饰的变量成为常量,不可再次赋值
-
final修饰的变量必须在声明的后面直接赋值
常量
-
字面值常量
-
1/3/555aaa、hhh、hehehe
-
-
自定义常量
-
把变量使用final修饰
-
final int area = 960
-
命名自定义常量的时候字母全部大写,多个单词使用下划线连接
-
package com.qf.final0;
public class Demo01 {
public static void main(String[] args) {
double pi = Math.PI;
System.out.println(pi);
double pI2 = MathUtil.PI;
System.out.println(pI2);
// 这个圆周率可以被任意修改,不合理,做成常量不可修改
// MathUtil.PI = 6.666;
double pI3 = MathUtil.PI;
System.out.println(pI3);
}
}
class MathUtil{
// 生活生产中有些数据是不可变的,做成常量
public static final double PI = 3.141592653589793;
}
引用类型常量
-
引用地址不可以改变
-
对象中的内容可以改变
1.2 final修饰类
-
被final修饰的类成为最终的类
-
这个类不能被继承,没有子类
1.3 final修饰方法
-
final修饰的方法成为最终的方法
-
可以被所在类的子类继承使用
-
不能被重写
package com.qf.final0;
public class Demo05 {
public static void main(String[] args) {
double pi = Math.PI;
String str = "heiheihei";
System.out.println();
Students s01 = new Students();
// 子类可以继承到final修饰的方法
s01.bitCard();
}
}
final class Calculator{
public static int getSum(int...is) {
int sum = 0;
for (int i = 0; i < is.length; i++) {
sum += is[i];
}
return sum;
}
}
// 最终的类无法被继承,没有子类
// class Calculator000 extends Calculator{}
class Student{
String name;
int age;
// 被final修饰的方法能被继承,不能被重写
public final void bitCard() {
System.out.println("我们需要使用小程序在校区附近打卡....");
}
}
class Students extends Student{
/*
public final void bitCard() {
System.out.println("我们需要使用小程序在校区附近打卡....");
}
*/
}
1.4 final小结
二、接口(掌握)
2.1 定义
-
在java中使用interface声明的内容成为接口
-
接口中的变量只能是公开静态常量
-
接口中的方法只能是公开抽象方法
-
接口不能直接创建对象
2.2 验证接口中属性的修饰符
-
接口和测试类
package com.qf.inter;
public class Demo01 {
public static void main(String[] args) {
String field = MyInterface.FIELD;
System.out.println(field);
// 能使用类名直接调用说明是static修饰
String yourField = YourInterface.YOUR_FIELD;
System.out.println(yourField);
// 不能重新赋值说明是final修饰
// YourInterface.YOUR_FIELD = "嘿嘿嘿";
}
}
interface MyInterface{
// 属性:公开、静态、常量
public static final String FIELD = "接口中的FIELD";
// 方法:公开、抽象方法
public abstract void show();
}
interface YourInterface{
String YOUR_FIELD = "没有修饰符的FIELD";
}
-
验证public
package com.qf.inter;
public interface Demo02 {
String OUR_FIELD = "Demo02中的OUR_FIELD";
}
package com.qf.final0;
import com.qf.inter.Demo02;
public class Demo06 {
public static void main(String[] args) {
// 在其他包中也能访问到,说明默认修饰符是public
String ourField = Demo02.OUR_FIELD;
System.out.println(ourField);
}
}
三、接口和抽象类(掌握)
3.1 相同点
-
可以生产字节码文件
-
可以声明为引用类型
-
不能直接创建对象
-
拥有Object中所有的方法
-
接口中所有的属性都是公开、静态、常量,默认使用public static final修饰
-
接口中的方法都是公开、抽象方法,默认使用public abstract修饰
-
接口中没有构造方法、动态代码块、静态代码块
四、接口的基本使用(掌握)
4.1 定义
-
接口不能直接创建对象
-
可以让一个类实现接口,重写接口中的方法
-
类可以在继承其他类的同时,实现多个接口
-
然后创建这个类的实例,使用重写之后的方法
4.2 USB案例
package com.qf.inter;
public class Demo03 {
public static void main(String[] args) {
ACER acer = new ACER();
acer.power();
acer.transfer();
System.out.println("=================");
USB usb = new ACER();
usb.power();
usb.transfer();
}
}
/**
* USB 接口
* 定义了属性电压和版本
* 定义了方法充电和传输数据
* @author Dushine2008
*
*/
interface USB{
// 属性
double VOLTAGE = 5.0;
double VERSION = 3.0;
// 方法
public abstract void power();
public abstract void transfer();
}
/**
* 子类宏碁电脑
* 实现接口USB
* @author Dushine2008
*
*/
class ACER implements USB{
五、接口的多态(掌握)
5.1 接口多态的向上转型
-
接口可以用作变量的声明
-
声明接口的类型,引用指向实现接口的类创建的对象
-
这个是向上转型的多态
package com.qf.inter;
public class Demo04 {
public static void main(String[] args) {
/**
* Dog类
* 继承Animal
* 实现Runnable接口
* 实现Swiming接口
*/
Dog dog = new Dog("道哥", 2);
dog.run();
dog.swim();
dog.eat();
dog.sleep();
System.out.println("================================");
Animal animal = new Dog();
animal.eat();
animal.sleep();
System.out.println("================================");
Runnable runnable = new Dog();
runnable.run();
System.out.println("================================");
Swimable swimable = new Dog();
swimable.swim();
}
}
/**
* 动物类
* 定义了属性和方法
* @author Dushine2008
*
*/
class Animal{
String name;
int age;
public Animal() {
super();
}
public Animal(String name, int age) {
super();
this.name = name;
this.age = age;
}
public void eat() {
System.out.println("很多动物都是杂食性的...");
}
public void sleep() {
System.out.println("大部分动物晚上睡觉...");
}
}
/**
* 能奔跑的接口
* @author Dushine2008
*
*/
interface Runnable{
void run();
}
/**
* 能游泳的接口
* @author Dushine2008
*
*/
interface Swimable{
void swim();
}
/**
* Dog类
* 继承了Animal,获得了Animal中所有非私有的属性和方法
* 实现了Swimable接口,重写了游泳的方法
* 实现了Runnable接口,重写了奔跑的方法
* @author Dushine2008
*
*/
class Dog extends Animal implements Runnable,Swimable{
public Dog() {
super();
}
public Dog(String name, int age) {
super(name, age);
}
/**
* 来自Swimable中的方法
*/
@Override
public void swim() {
System.out.println("狗子天生就会游泳,狗刨这个姿势来来自狗子...");
}
@Override
public void run() {
System.out.println("狗子奔跑的时候用四条腿,速度比人要快一些....");
}
}
5.2 接口多态的向下转型
-
把声明的父类引用转换回原来的类型
package com.qf.inter;
public class Demo05 {
public static void main(String[] args) {
/**
* Dog类
* 继承Animal
* 实现Runnable接口
* 实现Swiming接口
*/
Dog5 dog = new Dog5("道哥", 2);
dog.run();
dog.swim();
dog.eat();
dog.sleep();
System.out.println("================================");
Animal5 animal = new Dog5();
animal.eat();
animal.sleep();
Dog5 dog5 = (Dog5) animal;
dog5.run();
dog5.swim();
dog5.eat();
dog5.sleep();
System.out.println("================================");
Runnable5 runnable = new Dog5();
runnable.run();
Dog5 dog05 = (Dog5) runnable;
dog05.run();
dog05.swim();
dog05.eat();
dog05.sleep();
System.out.println("================================");
Swimable5 swimable = new Dog5();
swimable.swim();
// Runnable r = (Runnable) swimable;
}
}
/**
* 动物类
* 定义了属性和方法
* @author Dushine2008