抽象类和接口的应用

1.1 为抽象类和接口实例化

在Java中可以使用对象多态性,为抽象类和接口实例化,这样在使用抽象类和接口的时候,就可以调用本子类中覆写过的方法了。

之所以抽象类和接口不能直接实例化,是因为其内部包含了各个抽象方法,抽象方法本事是没有实现的方法,所以无法调用,
通过对象多态性发现,子类发生了向上转型之后,所调用的方法都是被覆写过的方法。
为抽象类实例化

abstract class A        //定义抽象类
{
    public abstract void print() ;  //定义抽象方法 在抽象类中abstract不能省略
                                    //在接口中可以abstract省略

}
class B extends A       //定义子类继承抽象类
{
    public void print()     //覆写父类中抽象方法
    {
        System.out.println("Hello World") ;
    }
}
public class AbstractCaseDemo01
{
    public static void main(String[] args)
    {
        A a = new B() ; //通过子类 为 抽象类实例化//向上转型   
        a.print() ;
    }
}

为接口实例化

interface A     //定义抽象类
{
    public abstract void print() ;  //定义抽象方法 在抽象类中abstract不能省略
                                    //在接口中可以abstract省略

}
class B implements A        //定义子类继承抽象类
{
    public void print()     //覆写父类中抽象方法
    {
        System.out.println("Hello World") ;
    }
}
public class InterfaceCaseDemo01
{
    public static void main(String[] args)
    {
        A a = new B() ; //通过子类 为 抽象类实例化//向上转型   
        a.print() ;
    }
}                                        

1.2 抽象类的应用—定义模板

abstract class Person       //定义抽象类
{
    private String name ;   //声明String属性
    private int age ;       //声明int属性

    public Person(String name ,int age) //构造方法
    {
        this.name = name ;
        this.age = age ;
    }
    public String getName() 
    {
        return this.name ;
    }
    public int getAge() 
    {
        return this.age ;
    }
    public void say()                   //定义一个方法
    {
        System.out.println(this.getCount()) ;
    }
    public abstract String getCount() ; //定义抽象方法 不用返回值

}
class Student extends Person    //定义子类继承抽象类
{
    private float score ;   //声明float属性表示 成绩
    public Student(String name,int age,float score)
    {
        super(name,age) ;
        this.score = score ;
    }
    public float getScore()
    {
        return this.score ;
    }
    public String getCount()    //覆写抽象类中抽象方法
    {
        return "姓名:"+this.getName()
                          +"年龄:"+this.getAge()
                          +"成绩:"+this.getScore() ;
    }
}
class Worker extends Person //定义子类继承抽象类
{
    private float salary ;  //声明float属性表示 成绩
    public Worker(String name,int age,float salary)
    {
        super(name,age) ;
        this.salary = salary ;
    }
    public float getSalary()
    {
        return this.salary ;
    }
    public String getCount()    //覆写抽象类中抽象方法
    {
        return ("姓名:"+this.getName()
                          +"年龄:"+this.getAge()
                          +"工资:"+this.getSalary()) ;
    }
}
public class AbstractDemo04
{
    public static void main(String[] args)
    {
        Person per1 = null ;    //声明抽象类对象
        Person per2 = null ;    //声明抽象类对象
        per1 = new Student("IronMan",20,29.1f) ;        //学生是一个人                                                
        per2 = new Worker("SpiderMan",30,3000.0f) ;  //工人是一个人
        per1.say() ;    //学生说学生的
        per2.say();     //工人说工人的
    }
}

1.3 接口的应用 —制定标准

interface USB   //定义USB接口
{
    public void start() ; //定义抽象方法 USB开始工作
    public void stop() ;  //定义抽象方法 USB停止工作
}
class Computer  //定义类
{
    public static void plugin(USB usb) //USB接口作为参数传入到plugin方法中
    {
        usb.start() ;
        System.out.println("----USB Start----") ;
        usb.stop() ;
    }
}
class Flash implements USB  //定义类实现USB接口
{
    public void start() //覆写接口中方法
    {
        System.out.println("U盘开始工作") ;
    }
    public void stop()  //覆写接口中抽象方法
    {
        System.out.println("U盘停止工作") ;
    }
}
public class InterfaceDemo05
{
    public static void main(String[] args)
    {
        Computer com = null ;   //声明Computer对象

        com.plugin(new Flash()) ;//对象.方法(子类实例)
    }
}

1.4 工厂设计模式

interface USB   //定义USB接口
{
    public void start() ; //定义抽象方法 USB开始工作
    public void stop() ;  //定义抽象方法 USB停止工作
}
class Computer  //定义类
{
    public static void plugin(USB usb) //USB接口作为参数传入到plugin方法中
    {
        usb.start() ;
        System.out.println("----USB Start----") ;
        usb.stop() ;
    }
}
class Flash implements USB  //定义类实现USB接口
{
    public void start() //覆写接口中方法
    {
        System.out.println("U盘开始工作") ;
    }
    public void stop()  //覆写接口中抽象方法
    {
        System.out.println("U盘停止工作") ;
    }
}
public class InterfaceDemo05
{
    public static void main(String[] args)
    {
        Computer com = null ;   //声明Computer对象

        com.plugin(new Flash()) ;//对象.方法(子类实例)
    }
}

1.5 代理设计模式

interface Network       //定义接口
{
    public void browse() ;  //定义抽象方法 浏览
}
class Real implements Network   //定义类实现接口
{
    public void browse()    //覆写抽象方法
    {
        System.out.println("上网浏览信息") ;
    }
}
class Proxy implements Network  //定义代理方法类
{
    private Network network ;   //声明代理对象 
    public Proxy(Network network)//接口作为参数传入代理方法中
    {
        this.network = network ;
    }
    public void check()         //定义方法 用于检查
    {
        System.out.println("检查用户") ;
    }
    public void browse()
    {
        this.check() ;          //调用检查方法
        this.network.browse() ; //调用Real中上网操作
    }
}
public class ProxyDemo01
{
    public static void main(String[] args)
    {
        Network net = null ;    //声明接口
        net = new Proxy(new Real()) ; //指定代理
        net.browse() ;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值