抽象类(abstract)及接口(interface)试例

来源:互联网 发布:软件资质证书有哪些 编辑:程序博客网 时间:2024/06/11 21:30

一、抽象类(abstract)

自上而下的继承层次结构,作为派生其他类的基类。使用extends继承,可以进行扩展,具有通用性。包含一个或多个抽象方法的类本身必须被声名为抽象的。抽象类中可以包含具体数据和具体方法,抽象类不能直接被实例化。

意义:例如person抽象类,可以包含个人的基本信息域(姓名,年龄,性别......),及其设置、获取的方法。现在我们需要创建不同行业的人的信息,可以统统继承person抽象类。对于不同行业的人描述信息不一样,所以可以定义一个getDescription()用于返回描述信息,其实现交给实现此抽象方法的子类   例子:

新建Person抽象类(我的抽象类放在com.inter包的Person.java中):

package com.inter;public abstract class Person {public Person(String name){this.name = name;}public String getName(){return name;}public abstract void getDescription();//定义抽象方法private String name;}


新建Student类,用于实现Person抽象类(我放在com.bean包的Student,java中):
package com.bean;import com.inter.*;public class Student extends Person{public Student(String name,int tall){super(name);//调用父类的构造方法this.tall = tall;}public int getTall(){return tall;}public void getDescription(){System.out.println("攻城狮:"+this.getName()+"\n身高:"+this.getTall()+"cm");}private int tall;}

1、在主类中直接测试抽象类(我的放在com.test包的test.java下)

package com.test;  import com.bean.*;  public class test {      public static void main(String[] args){        Student s = new Student("小卓",165);        s.getDescription();    }}

运行显示:

攻城狮:小卓

身高:165cm


2、使用spring测试:

编辑applicationContext.xml配置文档在项目的src下:

<?xml version="1.0" encoding="UTF-8"?>  <beans      xmlns="http://www.springframework.org/schema/beans"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xmlns:p="http://www.springframework.org/schema/p"      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="student" class="com.bean.Student"> <constructor-arg index="0"> <value>小卓</value> </constructor-arg> <constructor-arg index="1"> <value>165</value> </constructor-arg> </bean></beans>

在主类中:
package com.test;    import org.springframework.context.ApplicationContext;  import org.springframework.context.support.FileSystemXmlApplicationContext;  import com.bean.*;  public class test {      public static void main(String[] args){        ApplicationContext ac=new FileSystemXmlApplicationContext("src/applicationContext.xml");//利用文件系统查询applicationContext.xml配置文件                  Student s = (Student)ac.getBean("student");        s.getDescription();    }}
运行结果同1


二、接口(interface)

接口中的所有方法自动属于public,所以声名方法时不用提供public关键字。接口中还有一个没有明确说明的附加要求:在调用x.compareTo(y)时返回一个数(java.lang.comparable)。接口中不能含有实例域也不能在接口中实现方法,但是可以有常量。接口可以被另一个接口所扩展,实现接口的类必须实现接口的所有方法。例子:

java.lang.Comparable<T>中的泛型接口Comparable<>,其代码如下:

public interface Comparable<T>{       int compareTo(T other);}

要实现该接口就必须实现compareTo方法,要想对类进行排序就必须实现compareTo方法,修改上面的Student类:

package com.bean;import com.inter.Person;public class Student extends Person implements Comparable<Student>{public Student(String name,int tall){super(name);//调用父类的构造方法this.tall = tall;}public int getTall(){return tall;}public void getDescription(){System.out.println("攻城狮:"+this.getName()+"\n身高:"+this.getTall()+"cm");}public int compareTo(Student other) {if(this.getTall() > other.getTall()) return 1;if(this.getTall() < other.getTall()) return -1;return 0;}private int tall;}

修改applicationContext.xml配置文档:

<?xml version="1.0" encoding="UTF-8"?>  <beans      xmlns="http://www.springframework.org/schema/beans"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xmlns:p="http://www.springframework.org/schema/p"      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="beauty" class="com.bean.HelloBeauty"></bean> <bean id="student" class="com.bean.Student"> <constructor-arg index="0"> <value>小牛</value> </constructor-arg> <constructor-arg index="1"> <value>165</value> </constructor-arg> </bean> <bean id="student2" class="com.bean.Student"> <constructor-arg index="0"> <value>小驴</value> </constructor-arg> <constructor-arg index="1"> <value>175</value> </constructor-arg> </bean> <bean id="student3" class="com.bean.Student"> <constructor-arg index="0"> <value>小鸡</value> </constructor-arg> <constructor-arg index="1"> <value>179</value> </constructor-arg> </bean></beans>

修改主类:

package com.test;    import java.util.Arrays;import org.springframework.context.ApplicationContext;  import org.springframework.context.support.FileSystemXmlApplicationContext;  import com.bean.*;  public class test {      public static void main(String[] args){        ApplicationContext ac=new FileSystemXmlApplicationContext("src/applicationContext.xml");//利用文件系统查询applicationContext.xml配置文件                Student[] student =new Student[3];        student[0] = (Student)ac.getBean("student");        student[1] = (Student)ac.getBean("student2");        student[2] = (Student)ac.getBean("student3");        Arrays.sort(student);        for(Student s:student){        s.getDescription();        System.out.println("----------------");        }    }}

运行结果:

攻城狮:小牛
身高:165cm
----------------
攻城狮:小驴
身高:175cm
----------------
攻城狮:小鸡
身高:179cm
----------------

小结:每个类只能扩展于一个类(abstract),当想要扩展多个类的时候就要用到接口(interface)例如上面的student类:

public class Student extends Person implements Comparable<Student>{}

假如有个A接口:public interface A{}

还可以  public class Student extends Person implements Comparable<Student>,A{}

接口可用于解决多继承问题。

0 0
原创粉丝点击