innerclassexam1.java

来自「Java程序设计实用教程源代码 本书源代码按章分别放置在不同的文件夹中,所有程」· Java 代码 · 共 37 行

JAVA
37
字号
package exam1;
class Pen {
  String color;
  int length;
  Pen(String color, int length) {
    this.color = color;
    this.length = length;
  }
  void setColor(String color) {
    this.color = color;
  }
  String getColor() {
    return color;
  }
  void setLength(int length) {
    this.length = length;
  }
  int getLength() {
    return length;
  }
  void test() {
    myPen mp = new myPen(); //创建一个内部类对象
    mp.display(); //使用内部类对象的方法
  }
  class myPen { //声明一个内部类myPen
    void display() {
      System.out.println("the length of My " + color + "Pen is " + length);
    }
  }
}
public class InnerClassExam1 {
  public static void main(String args[]) {
    Pen p = new Pen("blue", 20);
    p.test();
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?