JAVA基础概念

owofile Lv5

JAVA基础概念

概念理解

变量

可变的量、值,通常是数字(int,double),小数(double),整数(int),字符串(String),字符(char)。

常量

不可变的量,通常是一些常用的,比如一天24小时,1小时60分钟, 修饰符为 final。

判断

通过不同的条件语句进行对变量的判断,为true继续执行 为false结束执行。

数组

用来存储一段数字 或者 字符串

一种类型,比如跑车类,越野车类。

一个归类的文件夹,在同一个包中,他们在同一个路径上,可以互相传递,倘若在不同包中,则需要其他方法进行链接和允许。

方法

一个类中拥有,若干的方法,这是一种行为动作 其中main()方法作为程序执行的主入口。

构建方法

(快捷键: alt + ins:Constructor)

当New 对象的时候 会在类中执行 有参构造方法和无参构造方法

属性

一个类中拥有若干个属性,这是基础构成,特征。

静态

一个修饰符 被static关键词修饰后 这条语句或者变量会变成类变量或者类方法 静态关键词拥有先执行的特点 执行顺序为 静态>构造方法>main方法入口。

封装

(快捷键: alt + ins : Getter and Setter)

封装类后 属性会变为私有 只有当前类可调用 通过快捷键 alt + Ins 创建快速针对 属性的 set选择器方法 or get选择器方法 然后通过这两个方法去对属性做操作 在方法中可以在变量时针对做出不同条件做出限制。

重载

当一个类中 两个方法名重复 但参数类型不重复的时候 形成重载 这种情况遵循重载规则不会报错。

修饰符

不同的修饰符他对于环境的访问权限各不相同,他分为四项 默认修饰符 私有修饰符 公共修饰符 和一个针对外包不可访问的修饰符。

继承

继承使用关键字 extends [类名] 继承一般是父级继承给子级,被继承后 子级可以使用父级的属性或方法。new 对象时 一般new 子级的类名 来运行 tip:如果是在不同包中的继承,在继承前需要导包 import. 通过点去点到你需要的包的文件 tip:继承具有唯一性单继承性和传承性(儿子可以用爸爸也可以用爷爷的属性和方法)

重写

(快捷键:alt + ins : Override Methods)

在原有方法的基础上 进行改进 在super() 后面进行续写

多态

一个方法中要用到多个不同的对象(类)时,通过参数调用父级直接实现方法,称作多态(父类当做形参)、还有另一种多态是将父类作为返回值的方法,也可以实现多态。

tip:

1.从下向上转型可以直接转(低阶武者升级高阶武者可以顺其自然)

2.从上向下转型需要强转(高阶武者想要降级则要付出代价)

三个条件:

1、继承的存在(继承是多态的基础,没有基础就没有多态)。

2、子类重写父类的方法(多态下调用子类重写后的方法)。

3、父类引用变量指向子类对象(向上转型)。

快捷键

Alt + Ins //快速创建 构造类

Ctrl + Alt + T //调出所有的判断 循环 报错标准语句

基础语句

构建一个方法

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
//默认系统自带无参构造方法
//快捷键生成构造方法 alt + Ins
//构建了一个无参方法
public Corn() {
}
//构建了一个有参方法
public Corn(String name, int growTime, int harvestTime, int numsOfFruits, String status, double harvestCost, boolean isHarvested) {
this.name = name;
this.growTime = growTime;
this.harvestTime = harvestTime;
this.numsOfFruits = numsOfFruits;
this.status = status;
this.harvestCost = harvestCost;
this.isHarvested = isHarvested;
}
//类的属性
public String name = "玉米"; //名称
public int growTime = 8; //生长期
public int harvestTime = 3; //采摘期
public int numsOfFruits = 200; //果实数量
public String status = "生长期"; // 生长状态
public double harvestCost = 50; //收割费用
public boolean isHarvested = false; //是否已采摘
//类的方法(行为)
public void print(){
System.out.println("\*\*\*\*\*\*\*作物特性\*\*\*\*\*\*\*");
System.out.println("您种植了:" + this.name + "。");
System.out.println("生长日期:" + this.growTime + "。");
System.out.println("采摘日期:" + this.harvestTime + "。");
System.out.println("目前果实数量:" + this.numsOfFruits + "。");
System.out.println("生长状态:" + this.status + "。");
System.out.println("收割费用:" + this.harvestCost + "。");
}

静态

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
package com.dbqn.demo05;
public class Student {
//静态变量
public static String name;
//实例变量
public int age;
//静态方法
//静态方法中不可以调用实例方法
//静态方法可以调用静态方法
//静态方法不可以调用静态变量
//静态方法不能使用 this
//静态方法不能调用实例变量
public static void Print(){}
//实例方法
//实例方法中可以调用静态方法
public void Print(int age){}
//静态代码块
static { // 当启动项加载时,会先执行静态代码块 如果执行顺序不对,可能是 new 对象的顺序不对
name = "小明";
//静态代码块 只能给静态变量赋值
}
//常量
//常量一般用 静态关键词修饰
//常量名字一般 大写
//常量 要加关键字 final
public static final double PI = 3.14;
//
}

封装

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
package com.dbqn.demo03;
public class AppleTtee {
//封装
//第一步 属性私有化
private String name;
private int age;
private String date;
//第二步 构建方法 获取或者 改变属性 快捷键 alt + Ins
public String getName() {
return name;
}
public void setName(String name) {
if (name.equals("苹果")){ // 私有化的时候 如果是this.name 就会报错 所以必须直接写名字 进行判断
this.name = name;
}else {
throw new RuntimeException("您输入的名字不合理"); // 报错语句
}
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
//在 set 方法中添加控制语句,设定条件 在上述更改
}

重载

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.dbqn.demo06;
public class Pande {
// 重载
//同一个类中 方法名相同 参数类型不同 1.参数数据类型不同 2.参数个数不同 3.顺序不同 和返回值无关
//参数个数不同
public void print(){}
public void print(String a){}
//参数数据类型不同
public void print(int a){}
//顺序不同
public void print(int a ,String b){}
public void print(String b, int a){}
}

继承

注意点: 写父级的时候要写有参构造 否则 子级继承的时候回报错

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
package com.dbqn.demo06\_继承;
public class Corp {
//注意事项
//不能被继承
//1.当父类的属性或方法的修饰符是Priveate
//2.当父类的属性或方法的修饰符是默认的,且不再同一个包中
//3.构造方法不能被继承,但可以被使用
private String name; //名字
private int growTime; //生长期
private int harvestTime; //采摘期
private int numsOfFruits; //果实数量
private String status; //生长状态
private boolean isHarvested; //是否采摘
//构造参数
//有参构造
public Corp(String name, int growTime, int harvestTime, int numsOfFruits, String status, boolean isHarvested) {
this.name = name;
this.growTime = growTime;
this.harvestTime = harvestTime;
this.numsOfFruits = numsOfFruits;
this.status = status;
this.isHarvested = isHarvested;
}
//无参构造
public Corp() {
}
//封装
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getGrowTime() {
return growTime;
}
public void setGrowTime(int growTime) {
this.growTime = growTime;
}
public int getHarvestTime() {
return harvestTime;
}
public void setHarvestTime(int harvestTime) {
this.harvestTime = harvestTime;
}
public int getNumsOfFruits() {
return numsOfFruits;
}
public void setNumsOfFruits(int numsOfFruits) {
this.numsOfFruits = numsOfFruits;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public boolean isHarvested() {
return isHarvested;
}
public void setHarvested(boolean harvested) {
isHarvested = harvested;
}
//展示
public void print(){
System.out.println("\*\*\*\*\*\*\*作物特性\*\*\*\*\*\*\*");
System.out.println("您种植了:" + this.name + "。");
System.out.println("生长日期:" + this.growTime + "。");
System.out.println("采摘日期:" + this.harvestTime + "。");
System.out.println("目前果实数量:" + this.numsOfFruits + "。");
System.out.println("生长状态:" + this.status + "。");
}
}

tip:当子类继承了父类,它的构造方法会默认父类的无参构造,在调用父类的构造方法时,先进行属性的赋值

多态

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
package com.dbqn.demo06\_继承;
public class Land {
//判断土地是否被种植
public boolean flag = true;
//种植苹果树
public void plant(AppleTree AppleTree) {
if (flag) {
System.out.println("恭喜你,成功种植了苹果树");
AppleTree.print();
flag = false;
} else {
System.out.println("抱歉,该土地已经被种植");
}
}
//种植玉米
public void plant(Corn Corn) {
if (flag) {
System.out.println("恭喜你,成功种植了玉米");
Corn.print();
flag = false;
} else {
System.out.println("抱歉,该土地已经被种植");
}
}
//种植西红柿
public void plant(Tomato Tomato) {
if (flag) {
System.out.println("恭喜你,成功种植了玉米");
Tomato.print();
flag = false;
} else {
System.out.println("抱歉,该土地已经被种植");
}
}
//在以下三种种植的时候,都是传入了子类的对象 以便可以引用 ,这种情况下有大量的重复性代码.
//同一个方法中,用到不同的对象 ,简称为 多态
//官方: 同一个方法中,不用的对象展现的方法,简称为 多态
//所以直接传入父类对象作为参数 就可以简化代码 这被称之为多态
//父类是也可以用子类的属性和方法的,他们是相同的
public void plant(Corp corp) { // new对象的时候直接New Corp也可以,因为子父类继承,构造方法也有子类的特殊属性的传递 所以不会报错
if (flag) {
System.out.println("恭喜你,成功种植了" + corp.getName());
corp.print();
flag = false;
} else {
System.out.println("抱歉,该土地已经被种植");
}
}
/\*
\* 多态应用中(1.让父类作为形参传入方法)
\*向上转型(自动转化)
\*
\* \*/
}

tip:这是以形参为父类的多态方法,以下是以返回值为父类的多态方法

强制转换

在Java中,强制类型转换(也称为类型转换或类型转换)是将一个数据类型的值转换为另一个数据类型的过程。这通常是因为你需要在不同的数据类型之间进行操作或赋值,但Java的类型系统要求你明确地告诉编译器你的意图,因为一些类型转换可能导致数据丢失或不可预测的结果。

要执行强制类型转换,你可以使用以下语法:

1
java复制代码目标数据类型 变量名 = (目标数据类型) 原始数据;

这是一个简单的示例,演示如何将一个整数转换为浮点数:

1
2
int intValue = 42;
double doubleValue = (double) intValue;

在这个示例中,(double) 是强制类型转换操作符,它告诉编译器将整数值 intValue 转换为双精度浮点数,并将结果存储在 doubleValue 变量中。

需要注意的几点事项:

  1. 强制类型转换可能会导致数据损失。例如,将一个大范围的整数转换为一个小范围的整数时,可能会导致溢出。
  2. 强制类型转换应该谨慎使用,因为它可能会导致运行时错误或不确定的行为。你应该确保转换是安全的,不会导致数据丢失或错误的结果。
  3. 在某些情况下,Java会自动执行隐式类型转换,但在其他情况下,你需要显式地使用强制类型转换。
  • Title: JAVA基础概念
  • Author: owofile
  • Created at : 2023-12-11 10:33:08
  • Updated at : 2025-04-11 21:18:26
  • Link: https://owofile.github.io/blog/2023/12/11/java/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments