⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 protectedexam.java

📁 Java程序设计实用教程源代码 本书源代码按章分别放置在不同的文件夹中,所有程序均在JDK1.6环境下编译运行正常,除了第13章需要建立ODBC数据源之外,其他程序只要有Java运行环境即可直接运行
💻 JAVA
字号:
// protectedExam.Java
package mypackage2;
import mypackage1.*; //使用import包含p1包
class Pencil extends Pen { //Pencil类继承了Pen类
  private String type = "Pencil";
  Pencil() { //默认构造方法
  }
  Pencil(String color, int length) { //自定义构造方法
    this.color = color;
    this.length = length;
  }
  protected void setColor(String color) { //重载父类的setColor()方法
    this.color = color;
  }
  protected String getColor() { //重载父类的getColor()方法
    return this.color;
  }
  protected void setLength(int length) { //重载父类的setLength()方法
    this.length = length;
  }
  protected int getLength() { //重载父类的getLength()方法
    return this.length;
  }
  void Cut() {
    if ( (this.getLength() - 1) > 0) //子类可以使用父类中的Protected方法
      this.length = this.getLength() - 1;
    else {
      System.out.println("铅笔已用尽!");
      System.exit(1);
    }
  }
}
public class protectedExam {
  public static void main(String args[]) {
    Pencil myPencil = new Pencil("Blue", 20);
    System.out.println("我新买的铅笔的颜色是" + myPencil.getColor() + ",长度为" + myPencil.getLength());
    for (int i = 1; i <= 5; i++)
      myPencil.Cut();
    System.out.println("这支铅笔修了5次后,长度是" + myPencil.getLength());
    Pencil myPencil1 = new Pencil();
    myPencil1.setColor("Red");
    myPencil1.setLength(10);
    System.out.println("另一只铅笔的颜色是" + myPencil1.getColor() + ",长度为" + myPencil1.getLength());
  }
}

⌨️ 快捷键说明

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