剩余参数、instanceof运算符、区间for循环、switch语句块、数组、this、static语句块、final关键字等的基本使用。
package _00_EasySeries;
import java.util.ArrayList;
// 1. 剩余参数..args
class Arg {
public static double variableParamsMaxValue(double ...args) {
if(args.length == 0) {
return 0.0;
} else {
double max = args[0];
for(int i = 1; i < args.length; i++) {
if(max < args[i]) {
max = args[i];
}
}
return max;
}
}
public static double maxValue(double value, double ...args) {
if(args.length == 0) {
return value;
} else {
double
max = value;
for(int i = 0; i < args.length; i++) {
if(max < args[i]) {
max = args[i];
}
}
return max;
}
}
public static void show(int...args) {
for(int value: args) {
System.out.print(value + " ");
}
System.out.println("");
}
}
// 2. instanceof 运算符
// 该运算符用于操作对象实例,检查该对象是否是一个特定类型(类类型或接口类型)。
class Father {}
class Son extends Father {}
class Other {
static void test() {
String name = "Hi";
System.out.println(name instanceof String); // true
Father father = new Son();
System.out.println(father instanceof Son); // true
}
}
// 3. 区间for循环
class For {
static void test() {
ArrayList<String> list = new ArrayList<String>();
list.add("Hello");
list.add("World");
list.add("hello");
list.add("world");
for (String str : list)
System.out.print(str + " ");
System.out.println("");
}
}
// 4. Switch
// default作为没有匹配成功的默认操作, 但是并没有强制一定要写在最后面。
// Switch语句的表达式在Java5之前只能是byte, short, int或者char类型,
// Java5中增加了enum,Byte,Short,Integer,Character,Java7中增加了String的支持。
class Switch {
static void test() {
int value = 3;
switch(value) {
case 1:
System.out.println("1");
break;
default:
System.out.println("default");
break;
case 2:
System.out.println("2");
break;
}
}
}
// 5.数组
class _Array {
static void array1() {
int[] arr1 = new int[3];
arr1[0] = arr1[1] = arr1[2] = 100;
int[] arr2 = { 111, 222 };
int[] arr3 = new int[] { 111, 222 }; // 不要指定维数
for(int num: arr3) {
System.out.println("arr3:>" + num);
}
}
static void array2() {
int[][] arr1 = {{1,2}, {3,4}, {5,6}}; // 3行2列
int[][] arr2 = new int[][] {{1,2},{3,4},{5,6}}; // 不要指定维数
int[][] arr3 = new int[][] {{1}, {2,3}, {4,5,6}}; // 不规则数组
for(int[] row: arr3) {
for(int col: row) {
System.out.print(col + " ");
}
}
System.out.println("");
}
}
// 6. this
// 构造函数This()中的 this(2,3)相当于调用This的构造函数This(int,int)完成对x,y的初始化;
// 必须是通过this来调用, 而不能直接通过构造函数名来调用;
// 此类写法可能并不具实际意义,此处只是演示一下。
class This {
private int x;
private int y;
This() {
this(2, 3);
}
This(int x, int y) {
this.x = x;
this.y = y;
}
void show() {
System.out.println("x:> " + x + " " + "y:> " + y);
}
}
// 7. static语句块
// 构造函数是在new对象时被调用进而用来对对象进行一些初始化操作;
// 而static语句块则是在类加载时被调用, 用于初始化一些全局地方设置;
// 说白了, static语句块就是在new对象之前先被调用;
class Static {
static {
System.out.println("initialize int static statement block.");
}
Static() {
System.out.println("in Static constructor.");
}
}
// 8. final常量
// C/C++中用const来定义常变量的; 那么在Java中是用final来定义常变量的;
// final常量必须进行初始化操作, 否则报错;
// 可以在定义时直接给其赋初值,也可以直接在构造函数中初始化;
// 切记, 重载构造函数时, 在重载的构造函数中也必须对其进行初始化, 否则报错;
class Final {
final double PI;
Final() {
PI = 32.4;
}
Final(int num) {
PI = 32.4; // must init it, or error.
}
Final(int num, double value) {
PI = 32.4; // must init it, or error.
}
}
class App {
public static void main(String[] args) {
//
double value = Arg.variableParamsMaxValue(1121,424,534,234,664,23424);
double _value = Arg.maxValue(12342, 23,23,242,12314,422413,4131,412);
System.out.println("the max value is:> " + value);
System.out.println("the max _value is:> " + _value);
Arg.show(new int[]{3,2,4,5});
//
Other.test();
//
For.test();
//
Switch.test();
//
_Array.array1();
_Array.array2();
//
This _this = new This();
_this.show();
//
Static _static = new Static();
}
}

2007

被折叠的 条评论
为什么被折叠?



