目录
抽象类
抽象类语法
被abstract修饰的类就是抽象类,抽象类中被abstract修饰的方法就是抽象方法
抽象类也是类,内部可以包含方法和属性
abstract class Animal {
//普通方法和类
private int age;
public void methpd () {
System.out.println("普通方法");
}
//抽象方法:被abstract修饰,没有方法体
abstract public void eat();
}
抽象类的特性
1.抽象类不能实例化
Animal animal = new Animal();//编译出错Animal是抽象的,无法实例化
2.抽象类不一定包含抽象方法,但抽象方法一定在抽象类中
3.抽象方法不能是private的,抽象方法没有加访问权限限定符时,默认为public
abstract class Animal {
abstract private void eat();//编译出错
}
4.抽象方法不能被final和static修饰
abstract class Animal {
abstract final void eat();//编译出错
abstract public static void sleep();//编译出错
}
5.如果抽象类被普通类继承,那么子类必须重写父类所有的抽象方法
abstract class Animal {
//普通方法和类
private int age;
public void methpd () {
System.out.println("普通方法");
}
//抽象方法:被abstract修饰,没有方法体
public abstract void eat();
}
class Cat extends Animal {
@Override//需要重写
public void eat () {
System.out.println("猫吃鱼");
}
}
抽象类的作用
抽象类存在的最大意义就是被继承
抽象类本身不能被实例化,要想使用只能通过创建抽象类的子类
使用抽象类时在重写时可以增加一重编译器的校验
接口
接口就是多个类的公共规范,使用关键字interface
接口的语法
interface 接口名{
}
interface USB {
public void start( );
public void stop( );
}
接口的使用
接口一般不能直接使用,必须有一个类来实现该接口,实现接口中的所有抽象方法
class 类名 implements 接口名{
}
interface USB {
public void start( );
public void stop( );
}
class Udisk implements USB {
@Override
public void start() {
System.out.println("U盘已插上可以读写");
}
@Override
public void stop() {
System.out.println("U盘已拔掉无法读写");
}
}
class Printer implements USB {
@Override
public void start () {
System.out.println("打印机已连接可以打印");
}
@Override
public void stop () {
System.out.println("打印机已关闭无法打印");
}
}
class Computer {
public void useDevice(USB usb) {
if (usb instanceof Printer) {
Printer printer = (Printer) usb;
printer.start();
printer.stop();
}
if (usb instanceof Udisk) {
Udisk udisk = (Udisk) usb;
udisk.start();
udisk.stop();
}
}
}
public class Main {
public static void main(String[] args) {
Computer computer = new Computer();
computer.useDevice(new Printer());
computer.useDevice(new Udisk());
}
}
接口的特性
1.接口类型是一种引用类型,不能直接new接口对象
interface USB {
}
public class Main {
public static void main(String[] args) {
USB usb = new USB();//编译错误
}
}
2.接口中不能有静态代码块,构造代码块和构造方法
interface USB {
USB () {
}
{
System.out.println("构造代码块");
}
static {
System.out.println("静态代码块");
}
}
3.接口中的方法都是public修饰的抽象方法,都会被隐式指定为public abstract方法,其他修饰符都会报错
interface USB {
private void start ();
protected void over ();
//编译错误
}
4.接口中的方法不能在接口中实现,不过接口中的静态方法可以有具体实现,default修饰的方法也可以有具体实现,同时default修饰的方法可以重写,也可以不进行重写
interface USB {
//default修饰的方法可以进行具体实现
default void start () {
System.out.println("启动USB");
}
//静态方法可以进行具体实现
private static void over() {
System.out.println("USB拔出");
}
// void method () {
// System.out.println("e");
// }//编译错误
}
class Printer implements USB {
}
5.重写接口方法时,要使用public权限修饰符
interface USB {
void start();
void stop();
void over();
default void method() {
System.out.println("ee");
}
}
class Udisk implements USB {
@Override
void start() {
System.out.println("U盘已插上可以读写");
}//编译错误
@Override
private void stop() {
System.out.println("U盘已拔掉无法读写");
}//编译错误
@Override
void method () {
}//编译错误
@Override
public void over () {
System.out.println("USB已弹出");
}
}
6.接口虽然不是类,但编译完成后字节码文件的后缀格式也是.class
7.如果该类没有实现接口中所有的抽象方法,该类要设置为抽象类
8.接口中可以含有变量,但接口中的变量会被隐式指定为public static final修饰
interface A {
private int a;//编译错误
protected int b;//编译错误
public int c;//编译错误
default int d;//编译错误
public static final int e;//编译错误
public static final int f = 3;
int g;//编译错误
int h = 4;
}
java中类和类是单继承的,但是一个类可以实现多个接口,接口与接口之间可以继承
interface A {
}
interface B {
}
interface C extends B{
}
class D implements A,C {
}
三种常用接口
Comparable
用于引用类型比较大小
import java.util.Arrays;
class Student implements Comparable<Student> {
private String name;
public Student(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public int compareTo(Student o) {
return this.name.compareTo(o.name);
}
}
public class Main {
public static void main(String[] args) {
Student[] students = new Student[] {
new Student("张三"),
new Student("王五"),
new Student ("李四")
};
System.out.println("排序前");
for (Student student : students) {
System.out.print(student.getName() + " ");
}
System.out.println();
Arrays.sort(students);
System.out.println("排序后");
for (Student student : students) {
System.out.print(student.getName() + " ");
}
}
}
Comparator
用于引用类型比较大小
import java.util.Arrays;
import java.util.Comparator;
class Student {
private String name;
public Student(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
class nameComparator implements Comparator<Student> {
@Override
public int compare(Student o1, Student o2) {
return o1.getName().compareTo(o2.getName());
}
}
public class Main {
public static void main(String[] args) {
Student[] students = new Student[] {
new Student("张三"),
new Student("王五"),
new Student ("李四"),
};
System.out.println("排序前");
for (Student student : students) {
System.out.print(student.getName() + " ");
}
System.out.println();
Arrays.sort(students,new nameComparator());
System.out.println("排序后");
for (Student student : students) {
System.out.print(student.getName() + " ");
}
}
}
Cloneable
这个接口是空接口(标记接口),代表当前类是可以被克隆的
不过要想实现clone需要重写Object中的clone方法
浅拷贝
class A implements Cloneable {
int age = 10;
@Override
protected Object clone () throws CloneNotSupportedException {
return super.clone();
}
}
class B implements Cloneable {
public String name = "hh";
public A a = new A();
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class Main {
public static void main(String[] args) {
B b = new B ();
B c = null;
try {
c= (B)b.clone();
} catch (CloneNotSupportedException e) {
;
}
System.out.println(c.name);//hh
System.out.println(c.a.age);//10
//改动b的值
b.a.age = 20;
b.name = "ee";
System.out.println(c.name);//hh
System.out.println(c.a.age);//20
}
}
深拷贝
class A implements Cloneable {
int age = 10;
@Override
protected Object clone () throws CloneNotSupportedException {
return super.clone();
}
}
class B implements Cloneable {
public String name = "hh";
public A a = new A();
@Override
protected Object clone() throws CloneNotSupportedException {
B b = (B)super.clone();
b.a = (A)((B) super.clone()).a.clone();
return b;
}
}
public class Main {
public static void main(String[] args) {
B b = new B ();
B c = null;
try {
c= (B)b.clone();
} catch (CloneNotSupportedException e) {
;
}
System.out.println(c.name);//hh
System.out.println(c.a.age);//10
//改动b的值
b.a.age = 20;
b.name = "ee";
System.out.println(c.name);//hh
System.out.println(c.a.age);//10
}
}