面向对象
容器中 添加数据、移除数据、获得数据
MyContainer.add(…)
MyContainer.remove(…)
MyContainer.get(…)
object oriented programming (OOP) 面向对象编程
三大特征:封装、继承、多态
**类、对象:**对象是 类在现实生活中的一个个实实在在的个体
//Student 封装了 学生共有的属性与行为
//private : 确保 元数据的安全性、可维护性
public class Student{
private int StuNo;
//无参构造函数 constructor : 初始化对象
public Student(){}
//有参构造函数
public Student(int stuNo){
this(); //调用无参构造函数,必须放在首行
this.stuNo = stuNo; // this : 当前对象
}
}
//创建对象
Student stu1 = new Student();
方法重载:
方法名相同 + 参数列表不一样(个数,数据类型,顺序)
访问权限
public : 公共的,其他类都能访问
private:私有的,只有本类能访问
缺省:只有本包能访问
protected:只有本包能访问,还有不同包中的子类
public > protected > 缺省 > private
day06 : A是父类{
public int x = 10;
int y = 20;
protected int z = 30;
private int m = 40;
}
B 是子类(public, 缺省 , protected)
C 是普通类(new A().public , 缺省 ,protected )
day05 :
D 是子类(public,protected)
E 是普通类(new A().public)
本文深入探讨面向对象编程(OOP)的基本概念,包括封装、继承和多态三大特性。通过具体示例,如使用Student类展示封装原则,以及介绍类与对象的关系,构造函数的作用,方法重载和访问权限等关键知识点。
10万+





