知识点1:面向对象的理解
1. 面向过程 vs 面向对象
二者都是一种思想,面向对象是相对于面向过程而言的。面向过程,强调的是功能行为,以函数为最小单位,考虑怎么做。面向对象,将功能封装进对象,强调具备了功能的对象,以类/对象为最小单位,考虑谁来做。

2. 类与对象的理解
面向对象中的两大核心概念
* 1. 类(class):对一类事物的描述,是抽象的、概念上的定义。
* 2. 对象(object\instance):由类派生(new)出的一个个的实体。

3. 学习的三条主线
面向对象编程的学习三条主线:
* 1. 类及类中的成员:属性、方法、构造器;内部类、代码块
* 2. 面向对象的三个特征:封装、继承、多态、(抽象)
* 3. 其它关键字的使用:this\super\import\package\static\final\abstract\interface\....
说明:
类的内部成员:
* 属性 = 成员变量 = field
* 方法 = 成员方法 = (成员)函数 = method
知识点2:类及对象的创建过程
* 1. 设计类,就是设计类的内部成员。。
*
* 2. 设计类、创建对象、调用功能的步骤:
* 第1步:创建类(实际上,就是创建类的成员:属性、方法)
* 第2步:创建类的对象 (或 创建类的实例、类的实例化)
* 第3步:通过"对象.属性"或"对象.方法"的方式,调用对象的内部结构
- 代码说明
public class PhoneTest { //测试类
public static void main(String[] args) {
//数据类型 变量名 = 变量值
Scanner scan = new Scanner(System.in);
// int number = scan.nextInt();
//创建Phone类的对象
Phone iphone = new Phone();
//通过"对象.属性"或"对象.方法"的方式,调用对象的内部结构
iphone.name = "苹果手机";
iphone.price = 8999;
System.out.println("name:" + iphone.name + ", price:" + iphone.price);
iphone.call();
iphone.sendMessage("有内鬼,停止交易!");
String info = iphone.takePhoto();
System.out.println(info);
//##################
Phone iphone1 = new Phone();
iphone1.name = "华为P40 pro";
iphone1.price = 4999;
System.out.println("name:" + iphone1.name + ", price:" + iphone1.price);
System.out.println("name:" + iphone.name + ", price:" + iphone.price);
//##################
Phone iphone2 = iphone;
iphone2.price = 6999;
System.out.println("name:" + iphone.name + ", price:" + iphone.price);
//##############
iphone.sendMessage("你好!很高兴认识你");
}
}
class Phone{
//1. 设计属性
String name;//手机名称
int price;//价格
//2. 设计方法
public void call(){
System.out.println("手机可以拨打电话");
}
public void sendMessage(String message){
System.out.println("发送信息:" + message);
}
public String takePhoto(){
//System.out.println("拍照");
return "拍照成功";
}
}
内存解析:

如果创建了类的多个对象,则每个对象拥有一套类的属性。当我们修改其中一个对象的属性时,不影响其它对象
* 此属性的值。
*
* 如果将对象引用p1赋给新的变量p2,则p1和p2就共同指向了堆空间中唯一的一个对象实体。当我们通过其中一个引用
* 修改对象的属性的话,则会导致另外引用对同一个属性的值的调用。
知识点3:成员变量vs局部变量
测试类中属性的定义
*
* 1. 回顾:变量的分类
* ① 按照数据类型来分: 基本数据类型(8种) vs 引用数据类型(类、数组、接口)
*
* ② 按照类中声明的位置: 成员变量(或属性) vs 局部变量
* 说明:成员变量:在类中直接定义的变量
* 局部变量:在方法内、构造器内、代码块内等定义的变量
*
* 2. 成员变量(或属性) vs 局部变量
* 相同点:
* 1. 变量声明的格式相同:数据类型 变量名 = 变量值
* 2. 凡是变量,都需要先声明,后使用
* 3. 都有其作用域。超出作用域,不可调用
*
* 不同点:
* 1. 声明位置的不同。
* > 成员变量:在类中直接定义的变量
* > 局部变量:在方法内、构造器内、代码块内等定义的变量
* 2. 是否可以使用权限修饰符进行修饰。
* > 权限修饰符有:private \ 缺省 \ protected \ public
* > 成员变量可以使用权限修饰符进行修饰 ;
* 局部变量不可以使用权限修饰符进行修饰
* > 暂时,我们在定义成员变量时,大家都使用缺省即可。(讲封装性时再说)
* 3. 是否有默认初始化值的情况
* > 成员变量都有默认初始化值
* ① 如果成员变量是整型(byte\short\int\long),则默认值为:0
* ② 如果成员变量是浮点型(float \ double),则默认值为:0.0
* ③ 如果成员变量是boolean型,则默认值为:false
* ④ 如果成员变量是char型,则默认值为:0 或 '\u0000'
* ⑤ 如果成员变量是引用数据类型,则默认值为:null
*
* > 局部变量都没有默认初始化值。则意味着在调用局部变量之前,一定要显式的赋值。
*
* 4. 在内存中存储的位置不同
* >成员变量存储在堆空间中
* >局部变量存储在栈空间中
*
* @author shkstart
* @create 2020-06-30 16:19
*/
- 代码演示
public class PersonTest {
public static void main(String[] args) {
Person p1 = new Person();
System.out.println(p1.name +"," + p1.age + "," + p1.gender);
p1.sleep();
p1.eat("湖南臭豆腐");
Person p2 = new Person();
System.out.println(p2.age);
}
}
class Person{
//声明成员变量(属性)
String name;
int age = 1;
boolean gender;//性别
public void sleep(){
int hour = 6; //局部变量
System.out.println("人每天应该至少保证" + hour + "小时的睡眠");
}
public void eat(String food){//food:局部变量
System.out.println("我喜欢吃:" + food);
}
}
知识点4:方法的声明
* 类中方法的声明测试
* 1. 之前用过的一些方法:
* Scanner类的nextInt() \ next()
* Math类的random() \ sqrt(double d)
* Arrays类的equals(int[] arr1,int[] arr2) \ toString(int[] arr) \ sort(int[] arr)
* \ binarySearch(int[] arr,int value)
*
* 2. 类中方法声明的格式:
* 权限修饰符 返回值类型 方法名(形参列表){
* 方法体
* }
* 说明:关于方法中的特殊关键字:static\final\abstract\...暂时先不考虑。
*
* 3. 方法声明的详细说明:
* 3.1 权限修饰符:指名当前方法被调用权限的大小。
* 涉及到的权限修饰符有:private 、 缺省 、 protected 、 public。 详细细节见《封装性》
* 目前,大家在声明方法时,默认使用public即可。
* 3.2 返回值类型 :
* > 可以分为:有具体的返回值类型 vs 没有返回值类型:void
* > 有具体的返回值类型,可以使用任何的变量类型。比如:基本数据类型、引用数据类型
* > 有具体的返回值类型的方法,在调用方法结束后,一定要返回满足要求的返回值类型的变量或常量
* 补充:return的使用:
* 在有具体返回值类型的方法中,一定需要使用“return + 变量/常量”的方法给方法返回指定的数据。
* 如果方法没有返回值类型,也可以使用"return;"结构,表示结束当前方法的执行。
*
* 3.3 方法名:属于标识符,命名时满足标识符命名的规则和规范。“见名知意”
* 3.4 形参列表:
* 格式:参数类型1 参数名1,参数类型2,参数名2,...
* 方法在声明时,不要给形参列表的变量赋值。当我们调用方法时,才给形参列表赋值:实参。
* 3.5 方法体:执行方法时,主要操作的逻辑。
* 4. 如何确定定义一个方法时,要不要声明返回值类型?要不要声明形参?
* ① 看题目要求。
* ② 具体问题具体分析。
- 代码演示
public class PersonTest {
public static void main(String[] args) {
Person p = new Person();
p.printNumber();
int hour = 8;
p.sleep(hour);
System.out.println("##############");
p.eat();
}
}
class Person{
String name;
int age;
Account acct;
public void printNumber(){
for(int i = 0;i < 100;i++){
if(i == 40){
return;//用于结束方法
}
System.out.println(i);
}
System.out.println("hello!");
}
public Account getAccount(){
boolean flag = true;
if(flag){
return null;
}
return new Account();
}
public int getValue(){
return 1;
// return age;
}
public void sleep(int hour){
System.out.println("昨天睡了" + hour + "小时");
}
public void eat(){
System.out.println("人需要吃饭,补充营养");
System.out.println("name = " + name);
sleep(10);
// eat();
}
}
class Account{//账户
}
- 补充说明
* 5. > 可以在当前类的方法中使用当前类定义的属性或其他的方法
* > 方法内不能定义新的方法。
- 课后练习
/**
* 2.利用面向对象的编程方法,设计类Circle计算圆的面积。
*
* @author shkstart
* @create 2020-07-01 11:37
*/
public class CircleTest {
public static void main(String[] args) {
Circle c1 = new Circle();
c1.radius = 1.2;
//对应方式一
// double area = c1.findArea();
// System.out.println("圆的半径为:" + c1.radius + ",面积为:" + area);
//对应方式二:
c1.findArea();
}
}
class Circle{//圆
double radius;//半径
//求圆的面积的方法
//方式一:
// public double findArea(){
// return Math.PI * radius * radius;
// }
//方式二:
public void findArea(){
System.out.println(Math.PI * radius * radius);
}
//错误的:
// public double findArea(double r){
// return Math.PI * r * r;
// }
}
知识点5:方法的重载
- 概念的理解
* 1. 什么是方法的重载?同一个类中,相同方法名,不同参数列表的方法之间构成重载。
*
* “两同一不同”:同一个类,相同方法名;参数个数 或 参数类型不同
*
* 2. 方法的重载与权限修饰符、返回值类型、形参名没有关系!
- 代码演示
public class OverLoadTest {
public static void main(String[] args) {
OverLoadTest test = new OverLoadTest();
test.show();
test.show(1,2);
}
public void show(){
System.out.println("hello");
}
public void show(int a){
System.out.println("萨瓦迪卡");
}
void show(String info){
System.out.println(info);
}
public int show(int i,int j){
return i + j;
}
private void show(int i,String info){
}
public void show(String info,int i){
}
// public void show(String info1,int j){
//
// }
// public String show(String info,int i){
// return info + i;
// }
}
- 重点
* 3. 我们在调用类中的方法时,是如何确定调用的是某一个确定的方法呢?
* 通过方法名确定 ---> 进一步通过形参列表确定
知识点6:可变形参的方法
/**
* 可变形参方法的使用
*
* 1. JDK5.0新增的特性。
* 2. 格式为:形参类型 ... 形参名
* 3. 当调用可变形参的方法时,实参可以为0个,1个,2个,。。。
* 4. 可变形参的方法与方法同名的其他方法之间构成重载(排除第5点)
* 5. 一个类中,不能同时出现可变形参的方法 和 与其方法名相同且可变形参类型相同的数组的方法。
* 6. 如果一个方法中声明有可变形参,则可变形参一定声明在方法参数的最后。
* 7. 一个方法的形参中,最多声明一个可变个数的形参结构。
*
*
* @author shkstart
* @create 2020-07-01 14:59
*/
public class ArgsTest {
public static void main(String[] args) {
// int[] arr = new int[0];
ArgsTest test = new ArgsTest();
// test.show();
// test.show("abc");
test.show("abc","123","xyz");
//
test.show(new String[]{"abc","def"});
}
public void show(){
System.out.println("show()");
}
public void show(String s){
System.out.println("show(String s)");
}
public void show(int i){
System.out.println("show(int i)");
}
public void show(String ... info){
System.out.println("show(String ... info)");
for(int i = 0;i < info.length;i++){
System.out.println(info[i]);
}
}
// public void show(String[] info){
// for(int i = 0;i < info.length;i++){
// System.out.println(info[i]);
// }
// }
//错误的
// public void show(String ... info,String i){
//
// }
}
知识点7:方法参数的值传递机制
1. 变量的传递机制:
* 1. 如果传递的是基本数据类型的变量,则将变量本身保存的数据值传递过去
* 2. 如果传递的是引用数据类型的变量,则将变量本身保存的地址值传递过去
public class VariableTest {
public static void main(String[] args) {
int m = 10;
//对于基本数据类型变量来说:
int n = m;
n = 20;
System.out.println("m = " + m);//10
//对于引用数据类型变量来说:
Order o1 = new Order();
o1.num = 10;
//
Order o2 = o1;
o2.num = 20;
System.out.println("o1.num = " + o1.num);
//
int[] array1 = new int[]{2,3,5,7,11};
int[] array2 = array1;
array2[0] = 0;
System.out.println(array1[0]);//0
}
}
class Order{
int num;
}
2. 方法形参的值传递机制
* 1. 形参:方法声明时,小括号内的参数。
* 实参:方法调用时,实际传递给形参的数据
*
* 2. 如果方法的形参是基本数据类型的变量,则将实参保存的数据值传递给形参变量。
* 如果方法的形参是引用数据类型的变量,则将实参保存的地址值传递给形参变量。
- 基本数据类型
public class ValueTransferTest {
public static void main(String[] args) {
int m = 10;
int n = 20;
System.out.println("m = " + m + ", n = " + n);
//交换两个变量的值
// int temp = m;
// m = n;
// n = temp;
ValueTransferTest test = new ValueTransferTest();
test.swap(m,n);
System.out.println("m = " + m + ", n = " + n);
}
public void swap(int m ,int n){
int temp = m;
m = n;
n = temp;
}
}
对应图示:

- 引用数据类型
public class ValueTransferTest1 {
public static void main(String[] args) {
Data data = new Data();
data.m = 10;
data.n = 20;
System.out.println("m = " + data.m +", n = " + data.n);
//交换的操作
ValueTransferTest1 test = new ValueTransferTest1();
test.swap(data);
System.out.println("m = " + data.m +", n = " + data.n);
System.out.println(data);//com.atguigu.java1.Data@1540e19d
// User user = data;
}
public void swap(Data data1){
int temp = data1.m ;
data1.m = data1.n;
data1.n = temp;
}
}
class Data{
int m ;
int n;
}
class User{
}
对应图示:

3. 练习1
/**
* @author shkstart
* @create 2020-07-01 16:23
*/
public class ValueTransferTest2 {
public static void main(String[] args) {
int[] arr = new int[]{34, 4, 2, 6, 6, 4, 7, -87, 0, 55, 98};
System.out.println(Arrays.toString(arr));
ValueTransferTest2 test = new ValueTransferTest2();
test.sort(arr, "ascend");
System.out.println(Arrays.toString(arr));
}
public void sort(int[] arr, String sortMethod) {
if ("ascend".equals(sortMethod)) {//从小到大
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
//最初的写法:正确
// int temp = arr[j];
// arr[j] = arr[j + 1];
// arr[j + 1] = temp;
//错误的写法
// swap(arr[j], arr[j + 1]);
//正确的写法
swap(arr,j,j+1);
}
}
}
} else if ("descend".equals(sortMethod)) {//从大到小
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] < arr[j + 1]) {
//最初的写法:正确
// int temp = arr[j];
// arr[j] = arr[j + 1];
// arr[j + 1] = temp;
//错误的写法
// swap(arr[j], arr[j + 1]);
//正确的写法
swap(arr,j,j+1);
}
}
}
} else {
System.out.println("排序方式错误!");
}
}
// public void swap(int i,int j){
// int temp = i;
// i = j;
// j = temp;
// }
public void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
4.练习2:
public class TransferTest3 {
public static void main(String args[]) {
TransferTest3 test = new TransferTest3();
test.first();
}
public void first() {
int i = 5;
Value v = new Value();
v.i = 25;
second(v, i);
System.out.println(v.i);
}
public void second(Value v, int i) {
i = 0;
v.i = 20;
Value val = new Value();
v = val;
System.out.println(v.i + " " + i);
}
}
class Value {
int i = 15;
}
对应的图示:

补充内容:
NullPointerException的理解
/**
* 关于面向对象内容中的NullPointerException的说明:
* 凡是引用数据类型的变量(对象、数组),如果取值为null时,通过此变量调用其内部结构的话,一定就是
* NullPointerException的异常。
*
* @author shkstart
* @create 2020-07-01 9:15
*/
public class NullPointerExceptionTest {
public static void main(String[] args) {
User u1 = new User();
// u1 = null;
// u1.name = "孔江江";
// u1.show();
System.out.println(u1.name);
// System.out.println(u1.name.toString());
}
}
class User{
String name;
int age;
public void show(){
String str = null;
System.out.println("我是一个用户");
}
}
对象数组的理解
/**
* 定义类Student,包含三个属性:学号number(int),年级state(int),成绩score(int)。
* 创建20个学生对象,学号为1到20,年级和成绩都由随机数确定。
* 问题一:打印出3年级(state值为3)的学生信息。
* 问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
*
* 提示:
* 1) 生成随机数:Math.random(),返回值类型double;
* 2) 四舍五入取整:Math.round(double d),返回值类型long。
*
* @author shkstart
* @create 2020-07-01 11:56
*/
public class StudentArrayTest {
public static void main(String[] args) {
int[] arr = new int[10];
String[] arr1 = new String[10];
// 创建20个学生对象
Student[] stus = new Student[20];//虚位以待
for (int i = 0; i < stus.length; i++) {
stus[i] = new Student();
//给每一个学生的属性赋值
//学号
stus[i].number = i + 1;
//年级:1-6
stus[i].state = (int)(Math.random() * 6 + 1);
//成绩:0-100
stus[i].score = (int)(Math.random() * 101);
}
//问题一:打印出3年级(state值为3)的学生信息。
// for (int i = 0; i < stus.length; i++) {
// if(stus[i].state == 3){
// stus[i].info();
// }
// }
//问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息
for(int i = 0;i < stus.length - 1;i++){
for(int j = 0;j < stus.length - 1 - i;j++){
if(stus[j].score > stus[j + 1].score){
Student temp = stus[j];
stus[j] = stus[j + 1];
stus[j + 1] = temp;
}
}
}
for (int i = 0; i < stus.length; i++) {
stus[i].info();
}
}
}
class Student{
int number;//学号
int state;//年级
int score;//成绩
public void info(){
System.out.println("number : " + number + ", state : " + state + ", score : " + score);
}
}
- 对应的内存解析

本文深入讲解面向对象编程的核心概念,包括类与对象的区别,面向对象的三大特性:封装、继承和多态,以及方法的声明与重载。通过实例演示,帮助初学者理解并掌握面向对象编程的基本原则。
面向对象编程&spm=1001.2101.3001.5002&articleId=107078789&d=1&t=3&u=30f031f5b77444dfb78ec5bde33c34c1)

被折叠的 条评论
为什么被折叠?



