抽象类的基本概念

来源:互联网 发布:淘宝旺旺上怎么发邮箱 编辑:程序博客网 时间:2024/05/23 00:19

本节目标:


抽象类的概念:包含一个抽象方法的类成为抽象类
抽象方法:只是声明而未被实现的方法称为抽象方法,抽象方法必须使用abstract关键字声明(没有方法体就是没有被实现)



abstract class A{ // 是定义了一个抽象类
      public static final String FLAG = "CHINA" ;      // 全局常量
      private String name = "李兴华" ;    // 定义一个普通的属性
      public void setName(String name){
             this. name = name ;
      }
      public String getName(){
             return this. name ;
      }
      public abstract void print() ;             // 定义抽象方法
};
abstract class A{ // 是定义了一个抽象类
      public static final String FLAG = "CHINA" ;      // 全局常量
      private String name = "李兴华" ;    // 定义一个普通的属性
      public void setName(String name){
             this. name = name ;
      }
      public String getName(){
             return this. name ;
      }
      public abstract void print() ;             // 定义抽象方法
};
class B extends A{      // 继承抽象类,因为B是普通类,所以必须覆写全部抽象方法
      public void print(){
            System. out. println("FLAG = " + FLAG) ;
            System. out. println("姓名 = " + super.getName()) ;
      }
};
public class AbstractDemo02{
      public static void main(String args[]){
            B b = new B() ;
             b.print() ;
      }
};

抽象类的定义跟普通类非常相似,但是却不能直接实例化
抽象类必须要有子类。子类必须覆写抽象类中的全部抽象方法


抽象类不能用final关键字来声明,因为final声明的类不能有子类,而抽象类是必须要有子类的。
实际上,在一个抽象类中是允许存在构造方法的,因为抽象类依然使用的是类的基础关系,而且抽象类也存在各种属性,所有子类在实例化之前肯定是先要对父类进行实例化的
abstract class A{ // 是定义了一个抽象类
      public A(){
            System. out.println( "A、抽象类中的构造方法。" ) ;
      }
};
class B extends A{      // 继承抽象类,因为B是普通类,所以必须覆写全部抽象方法
      public B(){
             super() ;
            System. out.println( "B、子类中的构造方法。" ) ;
      }
};
public class AbstractDemo03{
      public static void main(String args[]){
            B b = new B() ;
      }
};

当然,也可以通过super明确来指定要调用的构造方法
abstract class Person{
      private String name ;          // 定义name属性
      private int age ;              // 定义age属性
      public Person(String name, int age){
             this. name = name ;
             this. age = age ;
      }
      public void setName(String name){
             this. name = name ;
      }
      public void setAge(int age){
             this. age = age ;
      }
      public String getName(){
             return this. name ;
      }
      public int getAge(){
             return this. age ;
      }
      public abstract String getInfo() ;   // 抽象方法
};
class Student extends Person{
      private String school ;
      public Student(String name, int age,String school){
             super( name, age) ;  // 指定要调用抽象类中有两个参数的构造方法
             this. school = school ;
      }
      public void setSchool(String school){
             this. school = school ;
      }
      public String getSchool(){
             return this. school ;
      }
      public String getInfo(){
             return       "姓名:" + super .getName()  +
                         ";年龄:" + super .getAge() +
                         ";学校:" + this .getSchool() ;
      }
};
public class AbstractDemo04{
      public static void main(String args[]){
            Student stu = new Student( "张三",30,"清华大学" ) ;
            System. out.println( stu.getInfo()) ;
      }
};
总结:
  1. 一定要掌握抽象类定义格式及使用规则
  2. 抽象类使用的时候一定要有子类,子仍然使用extends关键字继承一个抽象类,同样会存在单继承的关系,一个子类不能同时继承多个抽象类
  3. 抽象类中绝对不能使用final关键字声明
  4. 抽象类中允许有构造方法,而且完全符合子类对象的实例化过程


































































0 0