第18 章 : 抽象类的定义与使用
82 抽象类基本概念
类继承主要作用在于可以扩充已有类的功能,不过不能强制子类必须覆写哪些类
父类设计优先考虑抽象类
抽象类:对子类覆写方法进行约定
抽象方法:abstract关键字定义并且没有提供方法体的方法
抽象类:抽象方法所在的类必须为抽象类
抽象类不是完整的类,不能直接实例化
使用抽象类:
1、抽象类必须提供子类,子类使用extends继承抽象类
2、抽象类子类一定要覆写抽象类中的全部抽象方法
3、抽象类的对象实例化可以利用对象多态性通过子类向上转型的方式完成
抽象类只是比普通类增加了抽象方法,以及对子类的强制性覆写要求,使用和普通类完全相同
核心问题:抽象类无法直接实例化
主要目的:进行过渡操作,解决类继承问题中代码重复处理
// 定义抽象类 abstract class Database{ private String type ; // 抽象方法 public abstract void connect() ; // 普通方法 public void setType(String type){ this.type = type; } public String getType(){ return this.type; } } class MySQLDatabase extends Database{ @Override public void connect(){ System.out.println("MySQL数据库连接"); } } class Demo{ public static void main(String[] args) { Database db = new MySQLDatabase(); db.connect(); // MySQL数据库连接 } }
83 抽象类的相关说明
注意要点:
1、定义抽象类不能使用final 关键字定义,抽象类必须有子类
2、抽象类是普通类的加强版,抽象类可以提供构造方法
3、抽象类允许没有抽象方法,无法new实例化对象,必须由子类完成
4、抽象类可以提供static 方法,不受抽象类结构限制
static 方法永远可以通过类名调用
// 定义抽象类 abstract class Database{} // 子类继承 class MySQLDatabase extends Database{} class Demo{ public static void main(String[] args) { Database db = new MySQLDatabase(); } }
84 模板设计模式
抽象类的定义比普通类更高一层
抽象类好处
1、对子类方法统一管理
2、提供一些普通方法,并且普通方法可调用抽象方法
// 定义抽象类 abstract class Action{ public static final int EAT = 1; public static final int SLEEP = 2; public void command(int code){ switch (code){ case EAT:{ this.eat(); break; } case SLEEP: { this.sleep(); break; } case EAT + SLEEP: { this.eat(); this.sleep(); break; } } } public abstract void eat(); public abstract void sleep(); } class Dog extends Action{ public void eat(){ System.out.println("Dog eat"); } public void sleep(){ System.out.println("Dog sleep"); } } class Cat extends Action{ public void eat(){ System.out.println("Cat eat"); } public void sleep(){ System.out.println("Cat sleep"); } } class Demo{ public static void main(String[] args) { Action dog = new Dog(); dog.command(Action.EAT); // Dog eat Action cat = new Cat(); cat.command(Action.EAT + Action.SLEEP); // Cat eat // Cat sleep } }
85 包装类实现原理分析
包装类针对基本数据类型对象转换而实现
基本数据类型不是一个类
基本数据类型以类的形式进行处理,需要对其进行包装
装箱:将基本数据类型保存到包装类中
// 定义包装类 class Int{ private int data; public Int(int data){ this.data = data ; } public int intValue(){ return this.data ; } } class Demo{ public static void main(String[] args) { // 装箱 基本数据类型 => 对象类型 Object obj = new Int(12) ; // 拆箱 对象类型 => 基本数据类型 int x = ((Int)obj).intValue(); System.out.println(x) ; // 12 } }
基本数据类型进行包装类后,可以向对象一样进行引用传递
8种基本数据类型
Object -数值型Number{abstract} -Byte: byte-8 -Short: short-16 -Integer: int-32 -Long: long-64 -Float: float-32 -Double: double-64 -布尔型Boolean:boolean -字符型Character:char-16
Java中有两种包装类:
1、对象型包装类Object子类:Boolean,Character
2、数值型包装类Number子类:Byte,Short, Integer, Long, Float, Double
Number类提供的方法:
byte byteValue() short shortValue() abstract int intValue() abstract long longValue() abstract float floatValue() abstract double doubleValue()
86 装箱与拆箱
Integer
Integer obj = new Integer(12) ; int x = obj.intValue(); System.out.println(x) ; // 12
JDK >= 1.5 自动装箱拆箱
JDK >= 1.9 过期
Integer obj = 2 ; int x = obj; // 直接参与数学运算 System.out.println(x * obj) ; // 4
自动装箱好处是Object可直接接收基本数据类型
// 自动装箱为Integer,再自动向上转型为Object Object obj = 2 ; // 先向下转型为Integer,再自动拆箱 int x = (Integer)obj;
相等判断,一定使用equals完成
Integer x = 12 ; Integer y = 12 ; System.out.println(x == y ); // true Integer x = 127 ; Integer y = 127 ; System.out.println(x == y ); // true Integer x = 128 ; Integer y = 128 ; System.out.println(x == y ); // false System.out.println(x.equals(y)); // true Integer x = -128 ; Integer y = -128 ; System.out.println(x == y ); // true Integer x = -129 ; Integer y = -129 ; System.out.println(x == y ); // false