3.7、抽象类与接口的实际应用{百分百的重点}

开发原则:<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

       在类的开发之中,一个类不会去继承一个已经实现好的类,只会继承抽象类或实现接口。
       即:以下的代码是不应该出现在程序之中的:
class A{}

class B extends A{}

<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />3.7.1、抽象类实际应用

       抽象类本身是不能直接实例化的。因为其本身包含抽象方法。
       但是抽象类的时候符合继承关系,只要有继承关系,则肯定会存在对象的向上或向下转型的问题,一但对象发生了向上转型,则肯定一切的方法以被子类覆写过的方法为准。
abstract class A{

         public abstract void fun() ;

};

class B extends A{

         public void fun(){

                   System.out.println("Hello World!!!") ;

         }

};

public class Demo32{

         public static void main(String args[]){

                   A a = new B() ;

                   a.fun() ;

         }

};

       从以上代码之中可以发现,抽象类可以实例化,但是需要通过对象的多态性,通过子类进行实例化操作。
       那么能用抽象类做什么呢?
违纪卡

姓名:

李兴华
班级:

小班 1006

时间:

1990-1-2

事由:

上课吃饭

       以后要是取数据的时候,直接从用户填写的地方取,因为上面规定出了是一个模板。
       实际上抽象类就是提供了此种类型的代码结构。
例如:人分为工人和学生,工人和学生都有说话方法,但是说话的内容要由工人或学生决定,所以此时就可以使用抽象类完成此种功能。
abstract class Person{

         private String name ;

         private int age ;

         public Person(String name,int age){

                   this.name = name ;

                   this.age = age ;

         }

         public void say(){

                   System.out.println(this.getContent()) ;

         }

         public String getName(){

                   return this.name ;

         }

         public int getAge(){

                   return this.age ;

         }

         public abstract String getContent() ;

};

class Worker extends Person{

         private float salary ;

         public Worker(String name,int age,float salary){

                   super(name,age) ;

                   this.salary = salary ;

         }

         public String getContent(){

                   return " 工人说 --> 姓名: "+this.getName()+" ,年龄: "+this.getAge()+" ,工资: "+this.salary ;

         }

};

class Student extends Person{

         private float score ;

         public Student(String name,int age,float score){

                   super(name,age) ;

                   this.score = score ;

         }

         public String getContent(){

                   return " 学生说 --> 姓名: "+this.getName()+" ,年龄: "+this.getAge()+" ,成绩: "+this.score ;

         }

};

public class Demo33{

         public static void main(String args[]){

                   Person p = null ;

                   // p = new Student(" 张三 ",20,99.0f) ;

                   p = new Worker(" 张三 ",20,999.0f) ;

                   p.say() ;

         }

};

练习:

       只要是学生进入到班级里来,就一定要喊报告。
       只要是老师进入到班级里来,也一定要说同学们好。
abstract class Person{

         private String name ;

         public Person(String name){

                   this.name = name ;

         }

         public void call(){

                   System.out.println(this.getContent()) ;

         }

         public String getName(){

                   return this.name ;

         }

         public abstract String getContent() ;

};

class Teacher extends Person{

         public Teacher(String name){

                   super(name) ;

         }

         public String getContent(){

                   return " 同学们好,姓名: "+this.getName() ;

         }

};

class Student extends Person{

         public Student(String name){

                   super(name) ;

         }

         public String getContent(){

                   return " 报告,姓名: "+this.getName() ;

         }

};

class Class_{

         public void comeIn(Person p){

                   p.call() ;

         }

};

public class Demo34{

         public static void main(String args[]){

                   Class_ c = new Class_() ;

                   c.comeIn(new Student(" 张三 ")) ;

                   c.comeIn(new Teacher(" 李四 ")) ;

         }

};

       以上的设计模式就称为模板设计。

3.7.2、接口的实际应用

总的使用原则:接口的作用就是将方法名称暴露给远程用户。
       接口与抽象类一样,如果要想正确的时候也需要通过子类进行实例化操作。
如下代码所示:
interface A{

         public void print() ;

}

class B implements A{

         public void print(){

                   System.out.println("Hello World!!!") ;

         }

};

public class Demo35{

         public static void main(String args[]){

                   A a = new B() ;

                   a.print() ;

         }

};

接口的实际应用:定义标准。
       USB接口为例:
interface USB{

         public void start() ;

         public void stop() ;

}

class MainBoard{

         public void plugIn(USB usb){

                   usb.start() ;

                   usb.stop() ;

         }

};

class Mp3 implements USB{

         public void start(){

                   System.out.println("MP3 开始工作。 ") ;

         }

         public void stop(){

                   System.out.println("MP3 停止工作。 ") ;

         }

};

class Print implements USB{

         public void start(){

                   System.out.println(" 打印机开始工作。 ") ;

         }

         public void stop(){

                   System.out.println(" 打印机停止工作。 ") ;

         }

};

public class Demo36{

         public static void main(String args[]){

                   MainBoard mb = new MainBoard() ;

                   mb.plugIn(new Mp3()) ;

                   mb.plugIn(new Print()) ;

         }

};