java:15-抽象类与内部类

来源:互联网 发布:java date 编辑:程序博客网 时间:2024/06/05 15:48

一、首先新建一个 Type Person,里面包含一个抽象方法

package javastudy;public abstract class Person {    public void eat()    {        System.out.println("动物吃饭!");    }    public abstract void study();    //①首先定义一个void study(),报错如下    //This method requires a body instead of a semicolon    //②还是报错    //The abstract method study in type Person can only    //be defined by an abstract class}

二、抽象类就是要做父类的,接下来用Type Student来继承,并实现其中的所有方法,就可以实例花了
①Student实现方法

package javastudy;public class Student extends Person {    //The type Student must implement the inherited abstract method Person.study()    //Student累必须    @Override    public void study()     {        //不是全部实现的话,也是抽象类;        //里面的方法可以不全部实现        System.out.println("学习");    }}

②接下来用TestIt进行测试

package javastudy;public class TestIt {    public static void main(String[] args)     {//      Person zhang = new Person();        //第一个报错Cannot instantiate the type Person        //抽象类不能实例化,不能创建对象;具体化之后才能干活;        //创建一个类来继承这个抽象类,抽象类就是为了当父类的;        Student s=new Student();        s.eat();        s.study();    }}

三、接口
①新建两个接口
1.Teacher

package javastudy;public interface Teacher {    public void teach();}

2.Stu

package javastudy;public interface Stu {    public void study();}

②通过Class Assist来实现其中部分的方法,这个方法也只能是抽象类

package javastudy;public abstract class Assist implements Stu, Teacher {    @Override    public void study()     {        // TODO Auto-generated method stub        System.out.println("学习");    }}

三、用一个案例来解释内部类的问题

package javastudy;public class Outter {    public int age;    class Inner    {        public int sex;    }}
原创粉丝点击