- 一、什么是面向对象
- 本质:
- 三大特性:
- 二、回顾方法
- 1. 方法的定义
- 2. 方法的定义
- (1)静态方法
- (2)非静态方法
- (3)形参和实参
- (4)值传递和引用传递
- 3. this 关键字
- (1) 运行==程序1==,调用 Student 类
- (2) 运行==程序2==,调用 Student 类
- (3) 运行==程序3==,调用 Student 类
| 面向过程 | 面向对象 |
|---|---|
| 第一步做··· 第二步做··· | 分类的思维模式,单独思考,在某个分类的细节里用面向过程 |
| 解决简单问题 | 解决复杂问题 |
整体分析——面向对象
细节——面向过程
- 以类的方式组织代码
- 以对象的组织(封装)数据
- 封装
- 继承
- 多态
二、回顾方法
1. 方法的定义
- 修饰符
- 返回类型
- break 跳出循环stwich,结束循环
- 方法名:注意规范
- 参数列表(参数类型、参数名)
- 异常抛出
public class Demo02 {
// 静态的方法 static
public static void main(String[] args) {
student.say();
}
调用
// 学生类
public class Student {
// 非静态方法
public void say(){
System.out.println("学生说话了");
}
}
(2)非静态方法
// 非静态方法
public static void main(String[] args) {
// 实例化这个类 new
// 对象类型 对象名 = 对象值
Student student = new Student();
student.say();
}
// 和类一起加载的
public static void a(){
b();
}
// 类实例化之后才存在 不加static 调用不了
public static void b(){
}
(3)形参和实参
public class Demo03 {
public static void main(String[] args) {
//实际参数和形式参数的关型要对应!
int add = Demo03.add(1, 2);
System.out.println(add);
}
//不加 static 要 new 调用
public static int add(int a, int b){
return a+b;
}
}
(4)值传递和引用传递
public class Demo04 {
public static void main(String[] args) {
int a = 1;
System.out.println(a);//1
Demo04.change(a);
System.out.println(a);//1
}
// 返回值为空
public static void change(int a){
a = 10;
}
}
//引用传递: 对象,本质还是值传递
// 对象, 内存!
public class Demo05 {
public static void main(String[] args) {
Person person = new Person();
System.out.println(person.name);//null
Demo05.change(person);
System.out.println(person.name);// sk
}
public static void change(Person person) {
// perosn是一个对象:指向的---> Perosn perosn= new Perosn(;这是一个具体的人,可以改变属性!
person.name = "sk";
}
}
// 定义了一个Person类, 有一个属性:name
class Person{
String name;//null
}
3. this 关键字
- 简单的一个类
public class Student {
//属性:字段
String name; //null
int age; //0
// 方法
public void study(){
System.out.println(this.name+"在学习");
}
}
(1) 运行程序1,调用 Student 类
/ 一个项目应该只在一个main方法
public class Application {
public static void main(String[] args) {
// 类:抽象的, 实例化
// 类实例化后会返回一个自己的对象!
Student xiaoming = new Student();
Student xh = new Student();
System.out.println(xiaoming.name);
System.out.println(xiaoming.age);
//使用new关键字创建的时候,除了分配内存空间之外,还会给创建好的对象
// 进行默认的初始化以及对类中构造器的调用。
}
输出为
null 0 Process finished with exit code 0(2) 运行程序2,调用 Student 类
/ 一个项目应该只在一个main方法
public class Application {
public static void main(String[] args) {
// 类:抽象的, 实例化
// 类实例化后会返回一个自己的对象!
Student xiaoming = new Student();
Student xh = new Student();
xiaoming.name = "xaoming";
xiaoming.age = 3;
System.out.println(xiaoming.name);
System.out.println(xiaoming.age);
//使用new关键字创建的时候,除了分配内存空间之外,还会给创建好的对象
// 进行默认的初始化以及对类中构造器的调用。
}
输出为
xaoming 3 Process finished with exit code 0(3) 运行程序3,调用 Student 类
// 一个项目应该只在一个main方法
public class Application {
public static void main(String[] args) {
// 类:抽象的, 实例化
// 类实例化后会返回一个自己的对象!
Student xiaoming = new Student();
Student xh = new Student();
xiaoming.name = "xaoming";
xiaoming.age = 3;
System.out.println(xiaoming.name);
System.out.println(xiaoming.age);
//使用new关键字创建的时候,除了分配内存空间之外,还会给创建好的对象
// 进行默认的初始化以及对类中构造器的调用。
xh.name = "xh";
xh.age = 3;
System.out.println(xh.name);
System.out.println(xh.age);
}
}
输出为
xaoming 3 xh 3



