flower.java

来自「俄罗斯方块游戏java」· Java 代码 · 共 34 行

JAVA
34
字号
//: Flower.java
public class Flower {
  private int petalCount = 0;
  private String s = new String("null");	//复杂类型必须初始化
  Flower(int petals) {		//仅带有(int)的初始化函数
    petalCount = petals;
    System.out.println( "Constructor w/ int arg only, petalCount= "
      + petalCount);
  }
  Flower(String ss) {		//仅带有(String)的初始化函数
    System.out.println( "Constructor w/ String arg only, s=" + ss);
    s = ss;
  }
  Flower(String s, int petals) {	//带有(String,int)的初始化函数
    this(petals);
//!    this(s); 	//尽管可用this调用一个构建器,但不可调用两个
    this.s = s; 		// 另外的一种this的使用方法
    System.out.println("String & int args");
  }
  Flower() {		//无参数的初始化函数
    this("hi", 47);	//用this调用带有(String,int)的初始化函数
    System.out.println( "default constructor (no args)");
  }
  void print() {
//!    this(11); 		// 没有如此的构造函数。
    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 + =
减小字号Ctrl + -
显示快捷键?