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

📄 flower.java

📁 java的oop编程important example
💻 JAVA
字号:
//  Calling constructors with this  
 
 

// : c04:Flower.java
// Calling constructors with "this."
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.

public class Flower {
  int petalCount = 0;

  String s = new String("null");

  Flower(int petals) {
    petalCount = petals;
    System.out.println("Constructor w/ int arg only, petalCount= "
        + petalCount);
  }

  Flower(String ss) {
    System.out.println("Constructor w/ String arg only, s=" + ss);
    s = ss;
  }

  Flower(String s, int petals) {
    this(petals);
    //! this(s); // Can't call two!
    this.s = s; // Another use of "this"
    System.out.println("String & int args");
  }

  Flower() {
    this("hi", 47);
    System.out.println("default constructor (no args)");
  }

  void print() {
    //! this(11); // Not inside non-constructor!
    System.out.println("petalCount = " + petalCount + " s = " + s);
  }

  public static void main(String[] args) {
    Flower x = new Flower();
    x.print();
  }
} ///:~  

⌨️ 快捷键说明

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