概念
面向对象程序设计(OOP): 以对象为核心的程序设计范式,将程序中的数据和操作的方法封装在一起。
面向过程程序设计(POP): 以过程为中心的程序设计范式,将程序分解为一系列的函数调用,每个函数完成特定功能,程序执行顺序是函数的顺序调用。
区别:
- 核心不同: 前者以对象为核心,后者以过程为核心。
- 侧重点不同: 前者侧重对象的封装和交互,后者侧重函数的顺序调用。
- 代码复用性不同: 前者通过继承和多态,复用性更高;后者通过函数调用,复用性相对较低。
- 可维护性: 前者程序结构清晰,可维护性好;后者结构相对复杂,难以维护。
面向对象优点:
- 封装性: 将类的数据和实现细节封装起来,对外提供简洁的接口。
- 继承性: 通过继承机制实现代码的复用。
- 多态性: 能够实现不同类之间的灵活转换
- 可维护性: 封装和继承使面向对象有较高的可读性和可维护性。
抽象: 抽象使对现实世界中复杂事物的简化与概括,通过抽象事物的共性和本质特征,忽略细节,形成一个更通用的模型。
封装: 将对象的属性和方法封装在一起,隐藏内部细节,只暴露必要接口。可保护对象的内部状态,防止外部的非法访问,同时提供一个简洁的接口供外部使用。
继承: 一种代码复用机制,允许一个子类继承父类的属性和方法。子类可以扩展或修改父类的行为,同时继承父类的多有非私有属性和方法。
多态: 允许不同的对象对同一消息做出不同回应,即同一个接口可以被不同的实例以不同的方式实现。多态性分为 编译时多态 和 运行时多态。
对象: 一个自包含的实体,用一组可识别的特征和行为来标识。
类: 具有相同属性和功能的对象的抽象集合。
类和对象关系: 对象是类的具体实例,具有类定义的属性和方法;类是对象的模板,定义了对象的结构和行为。如对于Person
类,定义属性name
、age
和方法sayHello
,此时可通过类来创建对象
1 2 3 4 5 6 7 8 9 10 11 12 13
| public class Main { public static void main(String[] args) { Person person1 = new Person(); person1.name = "Alice"; person1.age = 25; person1.sayHello();
Person person2 = new Person(); person2.name = "Bob"; person2.age = 30; person2.sayHello(); } }
|
此时person1
和person2
就是类Person
的具体实例,具有其的属性和方法。
类与C语言中的类型的不同:
类含有方法,类型不含有方法。
类存储类型属性和方法,类型定义变量存储方式和大小。
java中的类支持面向对象的特性。
Java程序中的类
系统定义类: 如java.lang,java.util,java.io
用户自定义类: 示例如下
1 2 3 4 5 6 7 8
| class Person { String name; int age;
void sayHello() { System.out.println("Hello! My name is " + name + " and I am " + age + " years old."); } }
|
1 2 3 4 5 6 7 8
| public class Main { public static void main(String[] args) { Person person = new Person(); person.name = "Alice"; person.age = 25; person.sayHello(); } }
|
访问控制符
- 概念: 是用于控制类、类的成员以及构造函数的访问权限的关键字。它们决定了哪些代码可以访问这些元素,从而实现封装和信息隐藏,保护类的内部实现细节。
- 类别:
private
default
protect
public
- 修饰类:
public
: 类可以在任何地方被访问。
default
: 类只能在同一个包内被访问。
- 修饰域和方法:
private
:只能在定义的类内部访问。
default
:只能在同一个包内访问。
protected
:可在同一个包内访问,也可在包的子类访问。
public
:可在任何地方访问。
编程运用
编程实现计算正方形、三角形、矩形及圆形面积计算功能。
要求:
1)定义两个包,所有类几何形体类定义shape包中,含main()方法的类定义在app包中,
2)
- 定义抽象类Shape,在其中定义area()方法计算并输出图形面积;
- 定义正方形、三角形、矩形及圆形类继承Shape类,实现各自的面积计算,其中长、宽、高、半径等基本几何要素设为private;
- 定义各类必要的构造方法及其它辅助方法;
3)实现使用静态成员变量来计算内存中的实例化的各几何图形对象数目。
实现:
结构
1 2 3 4 5 6 7 8 9
| /src |--/app | |--Main | |--/shape |--Square |--Triangle |--Rectangle |--Circle
|
shape/Shape: 定义抽象类Shape,抽象类只能被继承,不能直接实例化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package shape;
public abstract class Shape{ public abstract double area();
private static int Count = 0; public Shape(){ Count++; } public static int ShapeCount(){ return Count; } }
|
shape/Square: 定义类Square,覆盖area方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package shape;
public class Square extends Shape{ private float a; private static int SquareCount = 0;
public Square(float a){ this.a = a; SquareCount++; } public double area(){ return a * a; } public static int getSquareCount() { return SquareCount; } }
|
shape/Triangle: 定义Triangle类,覆盖area方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package shape;
public class Triangle extends Shape{ private float a,h; private static int TriangleCount = 0;
public Triangle(float a, float h){ this.a = a; this.h = h; TriangleCount++; } public double area(){ return 0.5 * a * h; } public static int getTriangleCount() { return TriangleCount; } }
|
shape/Rectangle: 定义Rectangle类,覆盖area方法 。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package shape;
public class Rectangle extends Shape{ private float a,b; private static int rectangleCount = 0; public Rectangle(float a, float b){ this.a = a; this.b = b; rectangleCount++; } public double area(){ return a * b; } public static int getRectangleCount() { return rectangleCount; } }
|
shape/Circle: 定义Circle类,覆盖area方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package shape;
public class Circle extends Shape{ private float r; private static int circleCount = 0; public Circle(float r){ this.r = r; circleCount++; } public double area(){ return Math.PI * r * r; }
public static int getCircleCount() { return circleCount; } }
|
app/Main: 定义Main类,实现main方法。
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 app; import shape.*;
public class Main { public static void main(String[] args){ Shape s1 = new Square(4); Shape s2 = new Triangle(3,4); Shape s2_2 = new Triangle(5,6); Shape s3 = new Rectangle(5,4); Shape s3_2 = new Rectangle(3,3); Shape s3_3 = new Rectangle(10,3); Shape s4 = new Circle(7);
System.out.println("Square's area:" + s1.area()); System.out.println("Triangle's area:" + s2.area()); System.out.println("Triangle's area:" + s2_2.area());
System.out.println("Rectangle's area:" + s3.area()); System.out.println("Rectangle's area:" + s3_2.area()); System.out.println("Rectangle's area:" + s3_3.area());
System.out.println("Circle's area:" + s4.area()); System.out.println("Count:" + Shape.ShapeCount());
System.out.println("总图形对象数目:" + Shape.ShapeCount()); System.out.println("Circle对象数目:" + Circle.getCircleCount()); System.out.println("Rectangle对象数目:" + Rectangle.getRectangleCount()); System.out.println("Triangle对象数目:" + Circle.getCircleCount()); System.out.println("Square对象数目:" + Rectangle.getRectangleCount());
} }
|
运行结果:

建模实现
某学院教学场景。
- 学院有院长、内设若干系、实验中心及研究所;
- 每系有教师、学生、课程:按教学方案组织教学活动。
- 教师:若干,主要工作有教学(授课布置作业、出考题)、科研(课题研究、应用开发)等;
- 学生:按班级组织, 主要工作是上课、完成作业、参加考试;也可以参与科研或担任助教等;
- 课程有理论课、实习课、体育课等。
使用java程序给上述场景建模并实现以下计算功能:
1、某门课程所有学生总分及平均分;
2、某学生所有课程成绩总分及平均分;
3、某教师所教授的所有学生人数。
实现:
项目结构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| /src |--/app | |--Main | |--/college | |--College | |--Department | |--LabCenter | |--MyClass | |--ResearchCenter | |--Task | |--/course | |--Course | |--Score | |--/people |--Dean |--Person |--Student |--Teacher |--Dean
|
college/College:
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
| package college;
import java.util.ArrayList; import people.Dean;
public class College { private String name; private Dean dean; private ArrayList<Department> departments;
public College(String name) { this.name = name; this.dean = null; this.departments = new ArrayList<>(); }
public void setDean(Dean dean) { this.dean = dean; }
public Dean getDean() { return dean; }
public void addDepartment(Department dept) { departments.add(dept); }
public String getName() { return name; } }
|
college/Department:
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 college; import java.util.ArrayList; import people.Teacher; import people.Student; import course.Course;
public class Department { private String name; private ArrayList<Teacher> teachers; private ArrayList<Student> students; private ArrayList<Course> courses; private ArrayList<ResearchCenter> researchCenters; private ArrayList<LabCenter> labCenters; private ArrayList<MyClass> myClasses;
public Department(String name) { this.name = name; this.teachers = new ArrayList<>(); this.students = new ArrayList<>(); this.courses = new ArrayList<>(); this.researchCenters = new ArrayList<>(); this.labCenters = new ArrayList<>(); this.myClasses = new ArrayList<>(); }
public void addTeacher(Teacher teacher) { teachers.add(teacher); }
public void addStudent(Student student) { students.add(student); }
public void addCourse(Course course) { courses.add(course); }
public void addresearchCenter(ResearchCenter researchCenter){ researchCenters.add(researchCenter); }
public void addlabCenter(LabCenter labCenter){ labCenters.add(labCenter); }
public void addClasses(MyClass myclass){ myClasses.add(myclass); } }
|
college/LabCenter:
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
| package college; import java.util.ArrayList; import people.Teacher; import people.Student;
public class LabCenter { private String name; private ArrayList<Teacher> teachers; private ArrayList<Student> students;
public LabCenter(String name) { this.name = name; this.teachers = new ArrayList<>(); this.students = new ArrayList<>(); }
public void addTeacher(Teacher teacher) { teachers.add(teacher); }
public void addStudent(Student student) { students.add(student); }
}
|
college/ReseachCenter:
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 college; import java.util.ArrayList; import people.Teacher; import people.Student; import course.Course;
public class ResearchCenter { private String name; private ArrayList<Teacher> teachers; private ArrayList<Student> students; private ArrayList<Task> tasks;
public ResearchCenter(String name) { this.name = name; this.teachers = new ArrayList<>(); this.students = new ArrayList<>(); this.tasks = new ArrayList<>();
} public void addTeacher(Teacher teacher) { teachers.add(teacher); }
public void addStudent(Student student) { students.add(student); }
public void addTask(Task task) { tasks.add(task); } }
|
college/MyClass:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package college; import java.util.ArrayList; import people.Student;
public class MyClass { private String name; private int id; private ArrayList<Student> students;
public MyClass(String name) { this.name = name; this.id = id; this.students = new ArrayList<>(); }
public void addStudent(Student student) { students.add(student); } }
|
college/Task:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package college;
public class Task { private String description;
public Task(String description) { this.description = description; }
public String getDescription() { return description; } }
|
course/Course:
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
| package course; import java.util.ArrayList; import people.Student;
public class Course { private String name; private ArrayList<Student> students;
public Course(String name) { this.name = name; this.students = new ArrayList<>(); }
public void addStudent(Student student) { students.add(student); }
public ArrayList<Student> getStudents() { return students; }
public double getTotalScore() { double total = 0; for (Student student : students) { Score score = student.getScores().get(this); if (score != null) { total += score.getScore(); } } return total; }
public double getAverageScore() { if (students.isEmpty()) { return 0; } return getTotalScore() / students.size(); } }
|
course/Score:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package course;
public class Score { private double score;
public Score() { this.score = 0; }
public void setScore(double score) { this.score = score; }
public double getScore() { return score; } }
|
people/Person:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package people;
public abstract class Person { protected String name; protected int id;
public Person(String name, int id) { this.name = name; this.id = id; }
public String getName() { return name; } }
|
people/Dean:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package people;
public class Dean extends Person { private String name;
public Dean(String name,int id) { super(name,id); }
public String getName() { return name; } }
|
people/Student:
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
| package people;
import java.util.HashMap; import course.Course; import course.Score;
public class Student extends Person { private HashMap<Course, Score> scores;
public Student(String name, int id) { super(name, id); this.scores = new HashMap<>(); }
public void enroll(Course course) { scores.put(course, new Score()); course.addStudent(this); }
public void setScore(Course course, double score) { if (scores.containsKey(course)) { scores.get(course).setScore(score); } } public Score getScore(Course course) { return scores.get(course); }
public HashMap<Course, Score> getScores() { return scores; }
public double getTotalScore() { double total = 0; for (Score score : scores.values()) { total += score.getScore(); } return total; }
public double getAverageScore() { return getTotalScore() / scores.size(); } }
|
people/Teacher:
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
| package people; import java.util.ArrayList; import course.Course;
public class Teacher extends Person { private ArrayList<Course> courses;
public Teacher(String name, int id) { super(name, id); this.courses = new ArrayList<>(); }
public void assignCourse(Course course) { courses.add(course); }
public int getTotalStudents() { int count = 0; for (Course course : courses) { count += course.getStudents().size(); } return count; } }
|
app/Main:
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
| package app; import people.Student; import people.Teacher; import course.Course; import college.College; import college.Department;
public class Main { public static void main(String[] args) { College college = new College("国家网络安全学院"); Department Xinan = new Department("信息安全系"); college.addDepartment(Xinan);
Teacher teacher1 = new Teacher("张老师", 1001); Teacher teacher2 = new Teacher("王老师",1002); Xinan.addTeacher(teacher1); Xinan.addTeacher(teacher2);
Course Course1 = new Course("计算机组成原理"); Course Course2 = new Course("Java网络程序设计"); Course Course3 = new Course("数据结构"); Xinan.addCourse(Course1); Xinan.addCourse(Course2); Xinan.addCourse(Course3);
teacher1.assignCourse(Course1); teacher2.assignCourse(Course2); teacher1.assignCourse(Course3);
Student student1 = new Student("张三", 2001); Student student2 = new Student("李四", 2002); Student student3 = new Student("王五", 2002); Student student4 = new Student("赵六", 2002);
Xinan.addStudent(student1); Xinan.addStudent(student2); Xinan.addStudent(student3); Xinan.addStudent(student4);
student1.enroll(Course1); student1.enroll(Course2); student2.enroll(Course1); student3.enroll(Course3); student4.enroll(Course3);
student1.setScore(Course1, 85); student1.setScore(Course2, 99); student2.setScore(Course1, 90); student3.setScore(Course3, 88); student4.setScore(Course3, 92);
System.out.println("计算机组成原理总分: " + Course1.getTotalScore()); System.out.println("计算机组成原理平均分: " + Course1.getAverageScore());
System.out.println(student1.getName() + " 总分: " + student1.getTotalScore()); System.out.println(student1.getName() + " 平均分: " + student1.getAverageScore());
System.out.println(teacher1.getName() + " 教授的学生总数: " + teacher1.getTotalStudents()); } }
|
运行结果:
