知识点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
);
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{
String name
;
int price
;
public void call(){
System
.out
.println("手机可以拨打电话");
}
public void sendMessage(String message
){
System
.out
.println("发送信息:" + message
);
}
public String
takePhoto(){
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
){
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;
}
public void sleep(int hour
){
System
.out
.println("昨天睡了" + hour
+ "小时");
}
public void eat(){
System
.out
.println("人需要吃饭,补充营养");
System
.out
.println("name = " + name
);
sleep(10);
}
}
class Account{
}
补充说明
* 5. > 可以在当前类的方法中使用当前类定义的属性或其他的方法
* > 方法内不能定义新的方法。
课后练习
public class CircleTest {
public static void main(String
[] args
) {
Circle c1
= new Circle();
c1
.radius
= 1.2;
c1
.findArea();
}
}
class Circle{
double radius
;
public void findArea(){
System
.out
.println(Math
.PI
* radius
* radius
);
}
}
知识点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
){
}
}
重点
* 3. 我们在调用类中的方法时,是如何确定调用的是某一个确定的方法呢?
* 通过方法名确定 ---> 进一步通过形参列表确定
知识点6:可变形参的方法
public class ArgsTest {
public static void main(String
[] args
) {
ArgsTest test
= new ArgsTest();
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
]);
}
}
}
知识点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
);
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]);
}
}
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
);
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
);
}
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
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]) {
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]) {
swap(arr
,j
,j
+1);
}
}
}
} else {
System
.out
.println("排序方式错误!");
}
}
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的理解
public class NullPointerExceptionTest {
public static void main(String
[] args
) {
User u1
= new User();
System
.out
.println(u1
.name
);
}
}
class User{
String name
;
int age
;
public void show(){
String str
= null
;
System
.out
.println("我是一个用户");
}
}
对象数组的理解
public class StudentArrayTest {
public static void main(String
[] args
) {
int[] arr
= new int[10];
String
[] arr1
= new String[10];
Student
[] stus
= new Student[20];
for (int i
= 0; i
< stus
.length
; i
++) {
stus
[i
] = new Student();
stus
[i
].number
= i
+ 1;
stus
[i
].state
= (int)(Math
.random() * 6 + 1);
stus
[i
].score
= (int)(Math
.random() * 101);
}
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
);
}
}
对应的内存解析