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

📄 simpleinheritance.java

📁 180个针对Java初学者的简单实例180个针对Java初学者的简单实例180个针对Java初学者的简单实例
💻 JAVA
字号:
class A {
  int i, j;
  void showij() {
    System.out.println("i and j: " + i + " " + j);
  }
}
//创建一个继承超类A的子类B 
class B extends A {
  int k;
  void showk() {
    System.out.println("k: " + k);
  }
  void sum() {
    System.out.println("i+j+k: " + (i+j+k));
  }
}
class SimpleInheritance {
  public static void main(String args[]) {
    A superOb = new A();
    B subOb = new B();
    // The superclass may be used by itself.
    superOb.i = 10;
    superOb.j = 20;
    System.out.println("Contents of superOb: ");
    superOb.showij();
    System.out.println();
    /* The subclass has access to all public members of
     its superclass. */
    subOb.i = 7;
    subOb.j = 8;
    subOb.k = 9; 
    System.out.println("Contents of subOb: ");
    subOb.showij();
    subOb.showk();
    System.out.println();
    System.out.println("Sum of i, j and k in subOb:");
    subOb.sum();
  }
}

⌨️ 快捷键说明

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