什么是面向对象编程
通俗易懂的说,就是把一坨一坨的数据放到一起存储
比如要存储一个学生的语文成绩和数学成绩
新建一个类
1 2 3 4 5 6 7 8 9 10 11 12 13
| 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); } }
|
这样学生的模板就创建好了,但是这个模板还没有指向学生的每一个个体。我们可以再同一个包下再新建一个类。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public class Test { public static void main(String[] args) { Student s1 = new Student(); s1.name = "学生a"; s1.chinese = 100; s1.math = 100; s1.printTotalScore(); s1.printAverageScore();
Student s2 = new Student(); s2.name = "学生b"; s2.chinese = 100; s2.math = 59; s2.printTotalScore(); s2.printAverageScore(); } }
|
用以上代码新建一个学生类并且调用我们之前写好的功能
- 开发一个一个的对象,把数据交给对象,再用调用对象的方法来完成对数据的处理,这种方法叫面向对象编程。
