Java基础

First Post:

Last Update:

Word Count:
3.4k

Read Time:
18 min

Java基础

简单的输入输出

1
2
3
4
5
6
7
8
9
10
11
12
import java.util.Scanner;

public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello you guys!");
System.out.println("I'm I0gan! 2020-05-05");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println("Your name is: " + name);
}
}

if else

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Enter an integer:");
Scanner in = new Scanner(System.in);
int n = in.nextInt();
if (n % 5 == 0 && n % 6 == 0) {
System.out.println(n + " is divisible by both 5 and 6");
} else if (n % 6 == 0 || n % 5 == 0) {
System.out.println(n + " is divisible by 5 or 6, but not both");
} else {
System.out.println(n + " is not divisible by either 5 or 6");
}
}
}

简单的递归计算阶层

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
import java.util.Scanner;

/*
这是一个计算阶层的一个比较功能的class,比较递归方式与普通循环的方式计算效率
*/
public class P3 {
public static void main(String[] args) {
System.out.print("Plz input a num:");
Scanner s = new Scanner(System.in);
int num = s.nextInt(); //获取输入为Int类型
//以递归方式进行运算
long t1 = System.currentTimeMillis();
System.out.println("Result: " + factorialLoop1(num));
long t2 = System.currentTimeMillis();
System.out.printf("递归耗时: %s %n", t2 - t1);

//以循环方式进行运算
t1 = System.currentTimeMillis();
System.out.println("Result: " + factorialLoop2(num));
t2 = System.currentTimeMillis();
System.out.printf("普通耗时: %s %n", t2 - t1);
}

//采用递归方式进行运算
static int factorialLoop1(int num) { //采用递归方式实现阶层运算
if(num == 1) {
return 1;
}
return num * factorialLoop1(num - 1);
}

//采用循环方式进行运算
static int factorialLoop2(int num) {
long result = 1;
while(num > 1) {
result *= num * (num - 1);
num -= 2;
}
return num;
}
}


类的基本用法

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
// 类的基本用法 1
public class MyClass {
int id;
String name;
int age;
MyClass() {
id = 0;
name = "none";
age = 0;
}
MyClass(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}

static { //在初始化类的时候,就先执行静态块,然后才能执行构造函数.
System.out.println("Class init...");
}

public static void main(String[] args) {
MyClass stu = new MyClass();
stu.id = 0;
stu.age = 17;
stu.name = "老李";

MyClass stu2 = new MyClass(1, "小二", 18);

System.out.printf("id: %d name: %s age: %d\n", stu.id, stu.name, stu.age);
System.out.printf("id: %d name: %s age: %d\n", stu2.id, stu2.name, stu2.age);

}
}

包的简单使用

MyPack/Package.java

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
package MyPack;
import pac1.HelloPack; //也可以采用: import pac1.* ,表示导入所有包

//导入包的可能会出现重名. 采用次方法: java.util.Date date = new java.util.Date(); 来指明
import java.util.Date;
import java.sql.*; //

/*在以后写项目的时候就一定要加包
* 包名命名规范: 域名倒着写, 再加上模块名, 便于管理内部管理类
* 例如: com.sun.test
* cn.sxt.gao.view.model
* java常用包:
* java.lang //核心类
* java.awt //用于图形界面
* java.net //网络相关
* java.io //输入输出
* java..util //工具类
*
* 技巧:
* 按住 Ctrl + 鼠标点击左键即可进入某个类中.
* */

//导入Math下的所有静态属性
import java.lang.Math.*;

public class Package {
public static void main(String[] args) {
//此方法不用声明 import pac1.HelloPack;
pac1.HelloPack p1 = new pac1.HelloPack(2, "I0gan");
p1.print();

//此方法需要: import pac1.HelloPack;
HelloPack p2 = new HelloPack(3, "Mike");
p2.print();

//不同包下类重名解决办法:
java.util.Date date = new java.util.Date();
java.sql.Date date2 = new java.sql.Date(1);
}
}

pac1/HelloPack.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package pac1;

public class HelloPack {
int id;
String name;
public HelloPack(int id, String name) {
this.id = id;
this.name = name;
}
public void print() {
System.out.printf("id: %d name: %s \n", id, name);
}
}

类的继承

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
package xyz.lyxf;
class Person {
//注意变量默认为public

private int nouse;
protected String name; //保护形式
public int age;
public void rest() {
System.out.println("I wanna a rest!");
}
}
class Student extends Person { //继承Person
String major;
public void study() {
System.out.println("I wanna study ten hours!");
}
}

public class MyClass {
public static void main(String[] args) {
Student std = new Student();
std.age = 16;
std.name = "I0gan";
std.major = "English";
// std.nouse = 1; //不能使用
std.study();
std.rest();

//instanceof 关键字: 判断某个对象是不是属于某个Class
System.out.println( std instanceof Student);
System.out.println( std instanceof Person);
System.out.println( new Person() instanceof Student);
}
}

overwrite重写

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
package xyz.lyxf;
/**
* 方法重写
* "==": 方法名, 形参列表相同
* "<=" 返回值类型和声明异常类型,子类小于等于父类
* ">=" 访问权限,子类大于等于父类
*/

class Person {
String name;
}
class Student extends Person {
String marjor;
}

class Vehicle {
public void run() {
System.out.println("run");
}
public void stop() {
System.out.println("stop");
}
public Person WhoDrivevehicle() {
return new Person();
}
}

class Horse extends Vehicle {
public void run() { //重写父类run函数.
System.out.println("四蹄翻飞");
}

//重写的返回参数大于基类, Student > person
public Student WhoDrivevehicle() {
return new Student();
}

}

public class MyClass {
public static void main(String[] args) {
Horse h = new Horse();
h.run();
h.stop();
Student s = h.WhoDrivevehicle();
s.marjor = "开马车..";

}
}

重写toString

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
package xyz.lyxf;
import java.lang.Object;
/*
* toString()
*
* */
class Obj {

}
class Obj2 {
public String toString() { //重写toString
return "My String";
}
}
public class MyObject {
public static void main(String[] args) {
MyObject obj = new MyObject();
//public String toString() {
// return getClass().getName() + "@" + Integer.toHexString(hashCode());
//}
System.out.println(obj.toString()); //toString是Object中的一个函数
System.out.println(new Obj());
System.out.println(new Obj2()); //重写后的toString
}
}

重写equals

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
package MyObj;

import java.util.Objects;

/*
* 重写==函数 (equal)
*public boolean equals(Object obj) {
* return (this == obj); //默认采用判断地址是否相等来判断对象是否相等
* }
*
* */
public class MyObj {
public static void main(String[] args) {
Object obj;
String str;

User u1 = new User(1, "老王", "123456");
User u2 = new User(1, "老王", "123456");
System.out.println(u1 == u2);
System.out.println(u1.equals(u2));

}
}

class User {
int id;
String name;
String pwd;
public User(int id, String name, String pwd) {
super();
this.id = id;
this.name = name;
this.pwd = pwd;
}
//@Override
public boolean equals(Object obj) {
System.out.println("equals调用");
if(this == obj) {
return true;
}
if(obj == null) {
return false;
}
if(getClass() != obj.getClass()) {
return false;
}
User other = (User)obj;
if(this.id == other.id) {
return true;
}
return true;
}
@Override
public int hashCode() {
return Objects.hash(id, name, pwd);
}
}

访问权限

Human.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Human {
public String name;
private int age;
protected int rev;

//外部类采用set get来操作private成员
public void setAge(int age) {
if(age < 4) {
age = 4;
}
this.age = age;
}
public int getAge() {
return this.age;
}
}

TestEncapsulation.java

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
/*
* 同一个类 同一个包 子类 所有类
* private: *
* default: * *
* protected: * * *
* public: * * * *
*
* 外部类采用set get来操作private成员
*
* */

public class TestEncapsulation {
public static void main(String[] args) {
Boy b = new Boy();
b.sayHello();
b.setAge(2);
System.out.println(b.getAge());
}
}

class Boy extends Human {
void sayHello () {
System.out.println(name);
System.out.println(rev);
}
}

super关键字

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
package lyxf.xyz;
class Father {
public int value;
public Father() {
System.out.println("create father class");
}
public void fun() {
value = 100;
System.out.println("Father class: " + value);
}
{
System.out.println("Fahter static code");
}
}
class Child extends Father{
public int value;
public Child() {
super(); //默认调用父类构造器, 没写编译器自动加入
System.out.println("create child class");
}
public void fun() {
super.fun(); //调用父类函数
value = 200; //没有对父类进行覆盖
System.out.println("Child class: " + value + "Father value: " + super.value);
}
{
System.out.println("Child static code");
}
}
public class Super {
public static void main(String[] args) {
Child c = new Child();
c.fun();
}
}

多态

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
/**
* 多态: 一个基类可以管理子类的重写函数
*/

public class Polymorphism {
public static void main(String[] args) {
Dog d = new Dog();
Cat c = new Cat();
animalShout(d);
animalShout(c);
}
static void animalShout(Animal a) {
a.shout();
}
}

class Animal {
public void shout() {
System.out.println("叫了一声");
}
}
class Dog extends Animal {
public void shout() {
System.out.println("狗叫了");
}
}
class Cat extends Animal {
public void shout() {
System.out.println("猫叫了");
}
}

多态相关的类型转换 && final

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
public class Casting {
public static void main(String[] args) {
Animal d = new Dog(); //自动向上转型
d.shout();
Dog d2 = (Dog)d; //强制转回来
d2.run();
Cat cat = (Cat)d;
//cat.eat(); //不能调用
}
}

class Animal {
public void shout() {
System.out.println("叫了");
}
}
class Dog extends Animal {
int age;
public void shout() {
System.out.println("狗叫了");
}
public void run() {
System.out.println("狗跑了");
}
}
class Cat extends Animal {
public void eat() {
System.out.println("猫吃东西");
}
}

final class TestFinal { //final 修饰类: 该类不能被继承
final int a = 0; //final 修饰变量: 变量只能被赋值一次
final void study() { //final 修饰函数: 该函数不能被子类重写
}
}

数组与遍历

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
/*
* 数组三个基本特点:
* 长度是确定的
* 元素类型相同
* 数组类型任意
* 数组就是对象
* 声明方式:
* 1: type[] arr_name; (推荐)
* 2: type arr_name[];
* */
public class Arr {
public static void main(String[] args) {
int[] arr1;
String[] arr2 = new String[3];
User[] arr3 = null; //声明
arr3 = new User[10]; //分配空间,并没有创建10个对象
arr2[0] = "老李";
arr2[1] = "老王";
arr2[2] = "老狗";
for(int i = 0; i < arr2.length; ++i) {
System.out.println(arr2[i]);
}

//遍历方法二
for (String i: arr2) {
System.out.println(i);
}

for(int i = 0; i < arr3.length; ++i) {
arr3[i] = new User();
arr3[i].setAge(i);
}
for (User u: arr3
) {
System.out.println(u.getAge());
}
}
}

class User {
private int age;
private int name;
public User () {
System.out.println("创建User");
}
public int getAge() {
return age;
}

public void setAge(int age) {
this.age = 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
public class Array {
public static void main(String[] args) {
//静态初始化
int[] a = {1, 2, 4 ,5};
User[] b = {new User(1), new User(2), new User(3)};
//动态初始化
int[] c = new int[4];
c[0] = 1;
c[2] = 0;
//默认初始化
int d[] = new int[2]; //默认值为0
boolean[] e = new boolean[3]; // 默认值为false
String[] s = new String[2]; //默认值为null
User[] u = new User[4]; //默认值为null

for(int i = 0; i < u.length; ++i) {
u[i] = new User(i);
}
//不能对对象进行真正的赋值
for (User i: u) {
System.out.println(i.getId());
}
}
}

class User {
int id;
User(int id) {
this.id = id;
}
int getId() {
return this.id;
}
}

三角角度计算

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
import java.util.Scanner;
import java.lang.*;

public class Main
{
public static class Triangle{
double x1,y1,x2,y2,x3,y3;
double a_length,b_length,c_length,average;
double a_angle, b_angle, c_angle;
public void cal_len(){
a_length = Math.sqrt(Math.pow((x2 - x3),2) + Math.pow((y2-y3),2));
b_length = Math.sqrt(Math.pow((x1 - x3),2) + Math.pow((y1-y3),2));
c_length = Math.sqrt(Math.pow((x1 - x2),2) + Math.pow((y1-y2),2));
}
public void cal_angel(){
a_angle=Math.toDegrees(Math.acos((Math.pow(a_length, 2) - Math.pow(b_length, 2)- Math.pow(c_length, 2))/(-2 * b_length * c_length)));
b_angle=Math.toDegrees(Math.acos((Math.pow(b_length, 2) - Math.pow(a_length, 2)- Math.pow(c_length, 2))/(-2 * a_length * c_length)));
c_angle=Math.toDegrees(Math.acos((Math.pow(c_length, 2) - Math.pow(b_length, 2)- Math.pow(a_length, 2))/(-2 * b_length * a_length)));
}
}

public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
Triangle first = new Triangle();
System.out.println("输入三角形顶点A的坐标: ");
first.x1 = sc.nextDouble();
first.y1 = sc.nextDouble();
System.out.println("输入三角形顶点B的坐标: ");
first.x2 = sc.nextDouble();
first.y2 = sc.nextDouble();1
System.out.println("输入三角形顶点C的坐标: ");
first.x3 = sc.nextDouble();
first.y3 = sc.nextDouble();
first.cal_len();
first.cal_angel();
System.out.printf("角A的角度为%.2f\n" , first.a_angle);
System.out.printf("角B的角度为%.2f\n" , first.b_angle);
System.out.printf("角C的角度为%.2f\n" , first.c_angle);
}
}

排序

bubule

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
import java.util.ArrayList;

public class bubule {
public static int size_ = 10;
public static int[] sort(int[] arr) {
for(int i = 0; i < arr.length; ++i) {
for(int j = 0; j < (arr.length - i); ++j) {
if(arr[i] < arr[j]) {
arr[i] += arr[j];
arr[j] = arr[i] - arr[j];
arr[i] = arr[i] - arr[j];
}
}
}
return arr;
}

public static void print(int[] arr) {
for(int i = 0; i < arr.length; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}

public static void main(String[] args) {
int[] arr1 = new int[size_];
int[] arr2 = null;
for(int i = 0; i < size_; ++i)
arr1[i] = (int)(100 +Math.random() * (100 + 1));

System.out.println("before order:");
print(arr1);

arr2 = sort(arr1);
System.out.println("after order:");
print(arr2);
}

};

insert

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
public class insert {
public static int size_ = 10;
public static int[] sort(int[] arr) {
for(int i = 1; i < arr.length; ++i) {
int t = arr[i]; // unorder arr
int j;
// move arr to back
for(j = i - 1; j >= 0 && t < arr[j]; j--)
arr[j + 1] = arr[j];
arr[j + 1] = t;
}
return arr;
}

public static void print(int[] arr) {
for(int i = 0; i < arr.length; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}

public static void main(String[] args) {
int[] arr1 = new int[size_];
int[] arr2 = null;

for(int i = 0; i < arr1.length; ++i)
arr1[i] = (int)(Math.random() * (100 + 1));

System.out.println("before sort");
print(arr1);

arr2 = sort(arr1);
System.out.println("after sort");
print(arr2);
}
};

select

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
public class select {
public static int size_ = 10;
public static int[] sort(int[] arr) {
for(int i = 0; i < arr.length; ++i) {
int min_p = i;
for(int j = i; j < arr.length; ++j) {
if(arr[min_p] > arr[j]) {
min_p = j;
}
}

//如果是最小是自己, 不用交换
if(i == min_p)
continue;

arr[i] += arr[min_p];
arr[min_p] = arr[i] - arr[min_p];
arr[i] = arr[i] - arr[min_p];
}
return arr;
}
public static void print(int[] arr) {
for(int i = 0; i < arr.length; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}

public static void main(String[] args) {
int[] arr1 = new int[size_];
int[] arr2 = null;


for(int i = 0; i < arr1.length; ++i)
arr1[i] = (int)(Math.random() * (100 + 1));

System.out.println("before sort");
print(arr1);

arr2 = sort(arr1);
System.out.println("after sort");
print(arr2);
}

};

quick

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
public class shell {
public static int size_ = 10;
public static int[] sort(int[] arr) {

return arr;
}

public static void print(int[] arr) {
for(int i = 0; i < arr.length; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}

public static void main(String[] args) {
int[] arr1 = new int[size_];
int[] arr2 = null;

for(int i = 0; i < arr1.length; ++i)
arr1[i] = (int)(Math.random() * (100 + 1));

System.out.println("before sort");
print(arr1);

arr2 = sort(arr1);
System.out.println("after sort");
print(arr2);
}
};

shell

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
public class shell {
public static int size_ = 10;
public static int[] sort(int[] arr) {

return arr;
}

public static void print(int[] arr) {
for(int i = 0; i < arr.length; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}

public static void main(String[] args) {
int[] arr1 = new int[size_];
int[] arr2 = null;

for(int i = 0; i < arr1.length; ++i)
arr1[i] = (int)(Math.random() * (100 + 1));

System.out.println("before sort");
print(arr1);

arr2 = sort(arr1);
System.out.println("after sort");
print(arr2);
}
};

打赏点小钱
支付宝 | Alipay
微信 | WeChat