抽象类概述
在java中,一个没有方法体 的方法应该定义为抽象方法 ,而类中如果有抽象方法 ,该类必须为抽象类
1 2 3 4 5 6 7 8 9 10 11 12 13 package unit_14chouxianglei;public abstract class Animal { public abstract void eat () ; }
抽象类特点
抽象类和抽象方法必须使用abstract关键字修饰
public abstract class 类名 {}
public abstract void eat();
抽象类中不一定有抽象方法,有抽象方法的类不一定是抽象类
抽象类不能实例化
抽象类如何实现实例化:参照多态的方式,通过子类对象实例化,这叫抽象类多态
抽象类的子类
代码
1 2 3 4 5 6 7 8 9 10 11 12 package unit_14chouxianglei.test1;public abstract class Animal { public abstract void eat () ; public void sleep () { System.out.println("睡觉" ); } }
1 2 3 4 5 6 7 8 9 10 11 package unit_14chouxianglei.test1;public class Cat extends Animal { @Override public void eat () { System.out.println("猫吃鱼" ); } }
1 2 3 4 5 6 package unit_14chouxianglei.test1;public abstract class Dog extends Animal {}
1 2 3 4 5 6 7 8 9 10 11 12 package unit_14chouxianglei.test1;public class Demo { public static void main (String[] args) { Animal a = new Cat(); a.eat(); a.sleep(); } }
抽象类的成员特点
成员变量
构造方法
有构造方法,但是不能实例化
构造方法的作用:用于子类访问父类数据的初始化
成员方法
可以有抽象方法:限定子类必须完成某些动作
也可以有非抽象方法:提高代码复用率
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 package unit_14chouxianglei.test2;public abstract class Animal { private int age = 20 ; private final String city = "北京" ; public Animal () {} public Animal (int age) { this .age = age; } public void show () { age = 40 ; System.out.println(age); System.out.println(city); } public abstract void eat () ; }
1 2 3 4 5 6 7 8 9 10 11 package unit_14chouxianglei.test2;public class Cat extends Animal { @Override public void eat () { System.out.println("猫吃鱼" ); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 package unit_14chouxianglei.test2;public class AnimalDemo { public static void main (String[] args) { Animal a = new Cat(); a.eat(); a.show(); } }
案例:猫和狗
需求:请采用抽象类的思想实现猫和狗的案例,并在测试类中进行测试
思路:
定义动物类
成员变量:姓名,年龄
构造方法:无参,带参
成员方法:get/set方法,吃饭();
定义猫类(Cat),继承动物类
构造方法:无参,带参
成员方法:重写吃饭(){…}
定义狗类(Dog),继承动物类
构造方法:无参,带参
成员方法:重写吃饭(){…}
定义测试类(AnimalDemp),写代码测试
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 package unit_14chouxianglei.test3anli;public abstract class Animal { private String name; private int age; public Animal () { } public Animal (String name, int age) { this .name = name; this .age = age; } public String getName () { return name; } public void setName (String name) { this .name = name; } public int getAge () { return age; } public void setAge (int age) { this .age = age; } public abstract void eat () ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package unit_14chouxianglei.test3anli;public class Cat extends Animal { public Cat () { } public Cat (String name, int age) { super (name, age); } @Override public void eat () { System.out.println("猫吃鱼" ); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package unit_14chouxianglei.test3anli;public class Dog extends Animal { public Dog () { } public Dog (String name, int age) { super (name, age); } @Override public void eat () { System.out.println("狗吃屎" ); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 package unit_14chouxianglei.test3anli;public class AnimalDemo { public static void main (String[] args) { Animal c = new Cat(); c.setName("雅秀特拉" ); c.setAge(18 ); System.out.println(c.getName()+"," +c.getAge()); c.eat(); System.out.println("-------------------------------" ); c = new Cat("雅秀特拉" ,15 ); System.out.println(c.getName()+"," +c.getAge()); c.eat(); System.out.println("-------------------------------" ); Animal d = new Dog(); d.setName("aibo" ); d.setAge(5 ); System.out.println(d.getName()+"," +d.getAge()); d.eat(); System.out.println("-------------------------------" ); d = new Dog("aibo" ,5 ); System.out.println(d.getName()+"," +d.getAge()); d.eat(); } }