Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

branch tutorial #1

Open
wants to merge 41 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
bcf7f45
branch tutorial
luanxiangming Jun 24, 2017
e94117c
init tutorial
luanxiangming Jun 25, 2017
5a1487a
loops
luanxiangming Jun 25, 2017
bc5738e
IfElseSwitch, Numbers
luanxiangming Jun 25, 2017
16b5110
Character, String
luanxiangming Jun 26, 2017
0a8bd0d
StringBuffer
luanxiangming Jun 26, 2017
0a94b2e
Array
luanxiangming Jun 27, 2017
9d34e53
Date
luanxiangming Jun 27, 2017
217b34f
Calendar
luanxiangming Jun 28, 2017
c4b11ba
Regex
luanxiangming Jun 28, 2017
2fb275d
Method
luanxiangming Jun 28, 2017
f9efa29
IO
luanxiangming Jun 28, 2017
d533590
File
luanxiangming Jun 28, 2017
ec0a105
Scanner
luanxiangming Jun 28, 2017
f5337c1
Exception
luanxiangming Jun 29, 2017
7023f08
Extends
luanxiangming Jun 29, 2017
1f7ba2e
Override, Overload
luanxiangming Jun 29, 2017
e6ec5a2
Abstract
luanxiangming Jun 29, 2017
dbb9725
Encapsulation
luanxiangming Jun 29, 2017
769e08b
Interface
luanxiangming Jun 30, 2017
18ae951
Package
luanxiangming Jun 30, 2017
dd2289c
BitSet, Enumeration, Stack, Vector
luanxiangming Jun 30, 2017
295ee52
Map, Hashtable, Properties
luanxiangming Jun 30, 2017
bd581d4
Collection
luanxiangming Jul 1, 2017
5e71044
Generic<T>
luanxiangming Jul 1, 2017
3ad8de5
Serializable
luanxiangming Jul 2, 2017
2804af0
JavaMail
luanxiangming Jul 4, 2017
3e8a98a
Runnable, Callable, Thread
luanxiangming Jul 5, 2017
8e030b7
Array, String
luanxiangming Jul 6, 2017
5304f91
Date
luanxiangming Jul 6, 2017
ccb1583
Collections
luanxiangming Jul 8, 2017
1f0efff
method
luanxiangming Jul 9, 2017
ccb9406
File
luanxiangming Jul 10, 2017
7876758
network, socket
luanxiangming Jul 11, 2017
a28de1e
thread
luanxiangming Jul 12, 2017
4d20e73
method reference, functional interface, lambda
luanxiangming Jul 13, 2017
174bffe
default method, stream
luanxiangming Jul 13, 2017
c074eed
Optional
luanxiangming Jul 13, 2017
89da3b9
java.time
luanxiangming Jul 14, 2017
571a0a6
Base64
luanxiangming Jul 14, 2017
4f67fb9
mysql
luanxiangming Jul 14, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
method
luanxiangming committed Jul 9, 2017
commit 1f0efff071c1133c49ad68f8446cb1fc5ede1d4d
142 changes: 142 additions & 0 deletions src/tutorial/examples/MethodExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package examples;

import java.util.ArrayList;
import java.util.List;

/**
* Created by Oliver on 08/07/2017.
*/
public class MethodExample {
public static void main(String[] args) {
/* 斐波那契数列 */
System.out.println("fibonacci: " + fibonacci(2));

/* 阶乘 */
System.out.println("factorial: " + factorial(5));

/* instanceOf 关键字用法 */
instanceOf(3.14f);
instanceOf("String");

/* continue 关键字用法 */
continues();

/* enum 和 switch 语句使用 */
Language language = Language.Java;
enum_switch(language);

/* Enum(枚举)构造函数及方法的使用 */
try_enum();

/* Varargs 可变参数使用 */
int[] intArrays = {1, 3, 5};
System.out.println("Sum of variable args: " + varargs(intArrays));

/* 重载(overloading)方法中使用 Varargs */
double[] doubleArrays = {1.1, 2.2, 3.3};
System.out.println("Sum of variable args: " + varargs(doubleArrays));
}

public static List<Long> fibonacci(long count) {
List<Long> list = new ArrayList<>();
if (count <= 1) {
list.add(0l);
return list;
} else {
list.add(0l);
list.add(1l);
long next;
for (int i = 2; i < count; i++) {
next = list.get(i - 2) + list.get(i - 1);
list.add(next);
}
}
return list;
}

public static long factorial(long num) {
long result = 1;
if (num > 1) {
for (int i = 1; i <= num; i++) {
result = result * i;
}
}
return result;
}

public static void instanceOf(Object o) {
if (o instanceof Integer) {
System.out.println("Object is instanceOf Integer");
} else if (o instanceof Float) {
System.out.println("Object is instanceOf Float");
} else if (o instanceof String) {
System.out.println("Object is instanceOf String");
}
}

public static void continues() {
StringBuffer searchstr = new StringBuffer("hello how are you. ");
int found = 0;
for (int i = 0; i < searchstr.length(); i++) {
if (searchstr.charAt(i) == 'h') {
found++;
continue;
}
}
System.out.printf("%d 'h' found in 'hello how are you. '%n", found);
}

enum Language {
Java(1), Python(2), Go(3), Javascript(4);
private int id;

Language(int id) {
this.id = id;
}

public int getId() {
return this.id;
}
}

public static void enum_switch(Language language) {
switch (language) {
case Java:
System.out.println("Language: Java");
break;
case Python:
System.out.println("Language: Python");
break;
case Go:
System.out.println("Language: Go");
break;
case Javascript:
System.out.println("Language: Javascript");
break;
}
}

public static void try_enum() {
for (Language l : Language.values()) {
System.out.printf("%s: %d", l, l.getId());
System.out.println();
}
}

public static int varargs(int... varargs) {
int sum = 0;
for (int i = 0; i < varargs.length; i++) {
sum += varargs[i];
}
return sum;
}

public static double varargs(double... varargs) {
double sum = 0;
for (double d : varargs) {
sum += d;
}
return sum;
}
}