blob: 561fe9f29a7f9176dac3f6928fb01110075432b4 (
plain) (
blame)
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
74
|
---
abbrlink: 1091767210
author: yingyu5658
categories:
- 技术
cid: 98
customSummary: null
date: "2024-12-01 13:05:00"
layout: post
mathjax: auto
noThumbInfoEmoji: null
noThumbInfoStyle: default
outdatedNotice: false
parseWay: auto
reprint: standard
slug: 98
status: publish
tags:
- 黑历史
- Java
thumb: null
thumbChoice: default
thumbDesc: null
thumbSmall: null
thumbStyle: default
title: Java面向对象编程快速入门
updated: 2024/12/07 10:56:43
---
# 什么是面向对象编程
通俗易懂的说,就是把一坨一坨的数据放到一起存储
比如要存储一个学生的语文成绩和数学成绩
新建一个类
```java
public class Student {
String name;//名字
double chinese;//语文成绩
double math;//数学成绩
public void printTotalScore() {
System.out.println(name + "的总成绩是" + (chinese + math));
}
public void printAverageScore() {
System.out.println(name + "的平均成绩是" + (chinese + math) / 2.0);
}
}
```
这样学生的模板就创建好了,但是这个模板还没有指向学生的每一个个体。我们可以再同一个包下再新建一个类。
```java
public class Test {
public static void main(String[] args) {
//1.创建宇哥学生对象来封装学生a的数据
Student s1 = new Student();
s1.name = "学生a";
s1.chinese = 100;
s1.math = 100;
s1.printTotalScore();
s1.printAverageScore();
//2.再创建一个学生对象,封装学生b的数据
Student s2 = new Student();
s2.name = "学生b";
s2.chinese = 100;
s2.math = 59;
s2.printTotalScore();
s2.printAverageScore();
}
}
```
用以上代码新建一个学生类并且调用我们之前写好的功能
- 开发一个一个的对象,把数据交给对象,再用调用对象的方法来完成对数据的处理,这种方法叫**面向对象编程**。
|