|
1
2
3
4
5
6
|
switch() //()中可为char,byte,int,short,long { case 1: //书写可无序,执行先case,后default,另注意break case 2: default: } |
|
1
2
3
4
5
6
7
8
9
10
|
for(元素变量:遍历对象) { 循环体 //为了方便地遍历数组中的元素 } int array[]={1,2,3,8,9,5,6}; for(int i:array) { System.out.println(i); //"i"指的是第i个元素 } |
|
1
2
3
4
5
|
修饰符 返回值类型 函数名(参数) { 执行语句; return 返回值; //明确参数,返回值很重要 } |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
重载特点: 1、在同一类中;2、方法名相同; 3、参数列表不同(类型、个数、顺序) 4、与访问修饰符,返回值,抛出异常无关 class Test { public static void main(String[] args) { A a=new A(); System.out.println(a.add(4,5)); } } class A { int add(int a,int b) { return a+b; } double add(double a,double b) { return a+b; } } |
|
1
2
3
|
int [] array; //两种写法风格不一样而已,不过貌似也有点小不同, 或 //可参考http://blog.csdn.net/hlw881008/article/details/5503835 int array []; //大多数程序员习惯用前者 |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
class Thinking{ public static void main(String[]args) { int [] arr1={1,2,3,4,5,6,7,8,9}; int [] arr2=new int[10]; System.arraycopy(arr1,0,arr2,1,arr1.length);/*arraycopy(来源数组,来源数组起始位置,目标数组,目标数组起始位置,复制的元素个数)*/ for(int i=0;i<arr2.length;i++) { System.out.print(arr2[i]+" "); } }} |
|
1
2
3
4
5
6
7
8
9
10
11
12
|
import java.util.Arrays;public class Thinking{ public static void main(String[] args) { int[] s={4,5,61,3,2,4,26,21,2,-82,34}; Arrays.sort(s); for (int i :s) { System.out.println(i); } }} |
|
1
2
3
4
5
6
7
8
|
import java.util.Arrays;public class Thinking{ public static void main(String[] args) { int[] s={4,5,61,3,2,4,26,21,2,-82,34}; int[] y={}; System.out.println(Arrays.equals(s,y));} |
|
1
2
3
4
5
6
7
8
|
import java.util.Arrays;public class Thinking{ public static void main(String[] args) { int[] s={4,5,61,3,2,4,26,21,2,-82,34}; System.out.println(Arrays.binarySearch(s,21)); }} |
|
1
2
3
4
5
6
7
8
9
10
11
12
|
import java.util.Arrays;public class Thinking{ public static void main(String[] args) { int[] s={4,5,61,3,2,4,26,21,2,-82,34}; Arrays.fills(s,11); for (int i :s) { System.out.println(i); } }} |
|
1
2
3
4
5
6
7
8
9
|
class Test { public static void main(String[] args) { String a="paino"; String b="animal"; System.out.println(b.equals(a)); }} |
|
1
2
3
4
5
6
7
8
|
class Test { public static void main(String[] args) { String a="paino"; System.out.println(a.length()); }} |
|
1
2
3
4
5
6
7
8
9
|
class Test { public static void main(String[] args) { String a="painoPjagjaKSJ"; System.out.println(a.toUpperCase()); System.out.println(a.toLowerCase()); }} |
|
1
2
3
4
5
6
7
8
|
class Test { public static void main(String[] args) { String a="painoPjagjaKSJ"; System.out.println(a.indexOf("o",3)); }} //indexOf区分大小写,返回值为int |
|
1
2
3
4
5
6
|
/**需求:*思路:*步骤:*/程序编辑 |
|
1
2
3
4
5
6
7
8
|
[修饰符] class <类名> [extends 父类名] [implement 接口列表]{ [修饰符] [static] [final] [transient] [volatile] <变量类型> <变量名>; [修饰符] [返回值类型] <方法名> (参数列表) { [final] <变量类型> <变量名>; }} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
class Cat //对象的母版{ String name; //属性 int age; void speak() //方法 { System.out.println("Hello!") }}class Test //使用对象的类{ public static void main(String[]args) //程序主函数 { Cat c = new Cat(); //创建一个对象 c.speak(); //使用对象 }} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
class Cat{ void speak() { System.out.println("hello"); }}class Test{ public static void main(String[]args) { Cat c = new Cat(); //若表示为 method(new Cat());则使用的是匿名对象 method(c); } public static void method(Cat c) //将Cat类作为一个参数使用 { c.speak(); }} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
class Cat { int age; String name; Cat(int age,String name) { this.age=age; this.name=name; } Cat(String name,int age) //两参数改变一下顺序 { this.age=age; this.name=name; } } class Test { public static void main(String[]args) { Cat c=new Cat(4,小花); Cat d=new Cat(小黑,2); System.out.println(c.name+"的年龄"+c.age); System.out.println(d.name+"的年龄"+d.age); } } |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
this的用法 a、当全局变量跟局部变量重名时,表示使用全局变量(此时this指代本类对象) class A{ String name; void setName(String name){ this.name = name; } } b、构造方法相互调用,此时this指代本类类名。注意this只能放在构造方法第一句 class B{ String name; B(){ this("name");//会自动调用带String参数的构造方法 } B(String name){ this.name = name; } } c、this是指当前对象自己。 当在一个类中要明确指出使用对象自己的的变量或函数时就应该加上this引用。 public class Hello { String s = "Hello"; public Hello(String s){ System.out.println("s = " + s); System.out.println("1 -> this.s = " + this.s); this.s = s; System.out.println("2 -> this.s = " + this.s); } public static void main(String[] args) { Hello x=new Hello("HelloWorld!"); } } d. 把this作为参数传递 当你要把自己作为参数传递给别的对象时,也可以用this。 public class A { public A() { new B(this).print(); } public void print() { System.out.println("Hello from A!"); } } public class B { A a; public B(A a) { this.a = a; } public void print() { a.print(); System.out.println("Hello from B!"); } } e、在构造函数中,通过this可以调用同一class中别的构造函数,如 public class Flower{ Flower (int petals){} Flower(String ss){} Flower(int petals, Sting ss){ //petals++;调用另一个构造函数的语句必须在最起始的位置 this(petals); //this(ss);会产生错误,因为在一个构造函数中只能调用一个构造函数 } } 值得注意的是: 1:在构造调用另一个构造函数,调用动作必须置于最起始的位置。 2:不能在构造函数以外的任何函数内调用构造函数。 3:在一个构造函数内只能调用一个构造函数。 |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
a、特点: 1、用于修饰成员 2、随着类的加载而加载 3、优先于对象存在,被所有对象所共享 4、可直接被类名调用 b、修饰成员变量:当对象中所具备的成员变量的值都是相同的,可用。【类变量】 c、修饰方法:当函数无需访问对象特有数据时可用 d、静态代码块:只执行一次,给类进行初始化 static { System.out.println("静态代码块"); } e、构造代码块:对所有对象进行初始化 注:有关Java中普通代码块,构造代码块,静态代码块区别及代码示例详情可参考: http://www.cnblogs.com/sophine/p/3531282.html |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
class Fu { Fu() { //super(); show(); //return; } void show() { System.out.println("fu show"); } } class Zi extends Fu { int num=8; Zi() { //super(); //——》通过super初始化父类内容时,子类的成员变量并未显示初始化,等super() //父类初始化完毕后,才进行子类的成员变量显示初始化 System.out.println("zi cons run"); //return; } void show() { System.out.println("zi show ...."+num); } } class Test { public static void main(String[]args) { Zi z=new Zi(); z.show(); } } //运行结果 请解释为什么会出现这样的结果? zi show ....0 zi cons run zi show ....8 |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
abstract class M { abstract show (); } class A extends M { void show() { System.out.println("抽象类"); } } class Test { public static void main(String[]args) { A a=new A(); a.show(); } } |

原文:http://blog.csdn.net/androidmylove/article/details/41624913