码迷,mamicode.com
首页 > 其他好文 > 详细

抽象类

时间:2016-10-13 21:37:48      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:

class Pencil
{
    String name;
    Pencil(){}
    Pencil(String name)
    {
        this.name = name;
    }
    public void write()
    {
        System.out.println("write quickly...");
    }
}

interface Eraser
{
    public static final String color = "白色";
    public abstract void clean();
}
//()
class PencilWithEraser extends Pencil implements Eraser
{
    PencilWithEraser(){}
    PencilWithEraser(String name)
    {
        super(name);
    }
    //public
    public void write()
    {
        System.out.println("super "+ super.name+ "PencilWithEraser‘s name...");
        System.out.println("this "+ this.name+ "PencilWithEraser‘s name...");
    }
    //void clean()
    public void clean()
    {
        System.out.println("ERROR, use Eraser...");
    }
}

public class jiekou 
{
    public static void main(String[] args) 
    {
        PencilWithEraser p = new PencilWithEraser("中华");
        p.write();
        p.clean();
        System.out.println(p.color);
        System.out.println(PencilWithEraser.color);
    }
}

运行结果:

super 中华PencilWithEraser‘s name...
this 中华PencilWithEraser‘s name...
ERROR, use Eraser...
白色
白色

 

  1. 接口中的所有属性 默认的修饰符是  public static final。
  2. 接口中的所有方法 默认的修饰符是  public abstract。
  3. 类实现接口可以通过implements实现,实现接口的时候必须把接口中的所有方法实现,一个类可以实现多个接口。
  4. 接口中定义的所有的属性默认是public static final的,即静态常量既然是常量,那么定义的时候必须赋值。
  5. 接口中定义的方法不能有方法体。接口中定义的方法默认添加public abstract
  6. 有抽象函数的不一定是抽象类,也可以是接口类。
  7. 由于接口中的方法默认都是抽象的,所以不能被实例化。
  8. 对于接口而言,可以使用子类来实现接口中未被实现的功能函数。
  9. 如果实现类中要访问接口中的成员,不能使用super关键字。因为两者之间没有显示的继承关系,况且接口中的成员成员属性是静态的。可以使用接口名直接访问。
  10. 接口没有构造方法。

 

抽象类

标签:

原文地址:http://www.cnblogs.com/Ant-/p/5958155.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!