设为首页 加入收藏

TOP

2011年计算机二级考试JAVA知识点整理(34)
2014-11-01 09:00:10 】 浏览:277
Tags:2011年 计算机 二级 考试 JAVA 知识点 整理

  1.2.6 内部类使用的其它的问题


  通过以上,我们可以清楚地看出内部类的一些使用方 法,同时,在许多时候,内部类是在如Java的事件处理、或做为值对象来使用的。同时,我们需注意最后一个问题,那就是,内部类同其它类一样被定义,同样 它也可以继承外部其它包的类和实现外部其它地方的接口。同样它也可以继承同一层次的其它的内部类,甚至可以继承外部类本身。下面我们给出最后一个例子做为 结束:


  public class Layer {


  //Layer类的成员变量


  private String testStr = "testStr";


  //Person类,基类


  class Person{


  String name;


  Email email;


  public void setName(String nameStr){


  this.name = nameStr;


  }


  public String getName(){


  return this.name;


  }


  public void setEmail(Email emailObj){


  this.email = emailObj;


  }


  public String getEmail(){


  return this.email.getMailStr();


  }


  //内部类的内部类,多层内部类


  class Email{


  String mailID;


  String mailNetAddress;


  Email(String mailId,String mailNetAddress){


  this.mailID = mailId;


  this.mailNetAddress = mailNetAddress;


  }


  String getMailStr(){


  return this.mailID +"@"+this.mailNetAddress;


  }


  }


  }


  //另一个内部类继承外部类本身


  class ChildLayer extends Layer{


  void print(){


  System.out.println(super.testStr);//访问父类的成员变量


  }


  }


  //另个内部类继承内部类Person


  class OfficePerson extends Person{


  void show(){


  System.out.println(name);


  System.out.println(getEmail());


  }


  }


  //外部类的测试方法


  public void testFunction(){


  //测试第一个内部类


  ChildLayer childLayer = new ChildLayer();


  childLayer.print();


  //测试第二个内部类


  OfficePerson officePerson = new OfficePerson();


  officePerson.setName("abner chai");


  //注意此处,必须用 对象.new 出来对象的子类对象


  //而不是Person.new Email(...)


  //也不是new Person.Email(...)


  officePerson.setEmail(officePerson.new Email("josserchai","yahoo.com"));


  officePerson.show();


  }


  public static void main(String[] args) {


  Layer layer = new Layer();


  layer.testFunction();


  }


  }


  编辑推荐:


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇2011年计算机二级考试JAVA知识点.. 下一篇2011年计算机二级考试JAVA知识点..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目