Java 语法 索引 ----- 构造函数(constructor)

重载

class MyRectangle{
    int x, y;
    public MyRectangle()             { x = 10; y = 20; }
    public MyRectangle(int a)        { x = a; y = a;   }
    public MyRectangle(int a, int b) { x = a; y = b;   }
}

Constructor chaining

public MyRectangle()             { this(10,20);  }
public MyRectangle(int a)        { this(a,a);    }
public MyRectangle(int a, int b) { x = a; y = b; }

Java 语法 索引 ----- 构造函数(constructor)

时间: 2024-12-25 15:24:08

Java 语法 索引 ----- 构造函数(constructor)的相关文章

Java 语法 索引 ----- 继承(Inheritance) 和重写(Overriding)

// Superclass (parent class) class Fruit{ public String flavor; } // Subclass (child class) class Apple extends Fruit { public String variety; } //downcasting Apple a = new Apple(); Fruit f = a; Apple c = (f instanceof Apple) ? (Apple)f : null; 重写 cl

Java 语法 索引 ----- 主函数 (Main)

public class MyApp { public static void main(String[] args) { System.out.print("Hello World"); } } Java 语法 索引 ----- 主函数 (Main),布布扣,bubuko.com

Java 语法 索引 ----- 变量-----数据类型

数据类型 类型 bits/byte 范围 默认值 byte 8/1 -128 +127 0 short 16/2 -32,768+32,767 0 int 32/4 -2,147,483,648 = -231+2,147,483,647 = 231-1 0 long 64/8 -9,223,372,036,854,775,808 = -263+9,223,372,036,854,775,807 = 263-1 0L float 32/4 1.40129846432481707e-45  = 2-

Java 语法 索引 ----- 条件语句(If Else,Switch)

if (x < 1) System.out.print(x + " < 1"); else if (x > 1) System.out.print(x + " > 1"); else System.out.print(x + " == 1"); Switch switch (y) { case 0: System.out.print(y + " is 0"); break; case 1: System

Java 语法 索引 ----- 运算符

算术运算符 float x = 3+2; // 5 // addition 加 x = 3-2; // 1 // subtraction 减 x = 3*2; // 6 // multiplication 乘 x = 3/2; // 1 // division 除 x = 3%2; // 1 // modulus (division remainder) 余数 Combined assignment operators int x = 0; x += 5; // x = x+5; x -= 5;

Java 语法 索引 ----- 数组(Arrays)

数组声明,分配, 赋值 int y[] = new int[3]; y[0] = 1; y[1] = 2; y[2] = 3; int[] x = new int[] {1,2,3}; int[] x = {1,2,3}; 二维数组 String[][] x = {{"00","01"},{"10","11"}}; String[][] y = new String[2][2]; y[0][0] = "00"

Java 语法 索引 ----- 循环(loop)

While 和  Do-While //while int i = 0; while (i < 10) { System.out.print(i++); } //do - while int i = 0; do { System.out.print(i++); } while ( i < 10 ); For 和 Foreach for (int i = 0; i < 10; i++){ System.out.print(i); } for (int k = 0, l = 10; k &l

Java 语法 索引 ----- 字符串(String)

组合吃烤串的各种方法 String a = "Hello"; String b = new String(" World"); //在循环里面的话要小心用 String c = a+b; // Hello World a += b; // Hello World String x = "Hello " + "World"; 比较字符串 boolean x = a.equals(b); // compares string bo

Java 语法 索引 ----- 枚举(Enum)

enum Speed { STOP, SLOW, NORMAL, FAST } Speed s = Speed.SLOW; switch(s) { case SLOW: break; } 参考文献: Java Quick Syntax Reference by Mikael Olsson Java 语法 索引 ----- 枚举(Enum)