面向过程&面向对象
- 面向过程思想
- 步骤清晰简单,第一步做什么,第二步做什么…
- 面对过程适合处理一些较为简单的问题
- 面向对象思想
- 物以类聚,分类的思维模式,思考问题首先会解决问题需要哪些分类,然后对这些分类进行单独思考。最后,才对某个分类下的细节进行面向过程的思索
- 面向对象适合处理复杂的问题,适合处理需要多人协作的问题
- 对于描述复杂的事务,为了把宏观上把握、从整体上合理分析,我们需要使用面向对象的思路来分析整个系统。但是,具体到微观操作,仍然需要面向过程的思路去处理
什么是面向对象
- 面向对象编程(Object-Oriented Programming,OOP)
- 面向对象编程的本质是:以类的方式组织代码,以对象的形式组织(封装)数据
- 抽象
- 三大特性:
- 从认识论角度考虑是先有对象后有类。对象,是具体的事物。类,是抽象的,是对对象的抽象
- 从代码运行的角度考虑是先有类后有对象。类是对象的模板
方法回顾
- 方法的定义
- 修饰符
- 返回类型
- break:跳出switch,结束循环和return的区别
- 方法名:注意规范 见名知意
- 参数列表:(参数类型,参数名)…
- 异常抛出
package com.oop.demo01;
import java.io.IOException;
public class Demo01 {
public static void main(String[] args) {
}
public String sayHello(){
return "hello,word";
}
public int max(int a,int b){
return a>b?a:b;
}
public void readFile(String file) throws IOException{
}
}
- 方法的调用:递归
- 静态方法
- 非静态方法
- 形参和实参
- 值传递和引用传递
- this关键字
package com.oop.demo01;
public class Demo02 {
public static void main(String[] args) {
Student student = new Student();
student.say();
}
public static void a(){
}
public void b(){
}
}
package com.oop.demo01;
public class Student {
public void say(){
System.out.println("学生说话");
}
}
package com.oop.demo01;
public class Demo03 {
public static void main(String[] args) {
int add = new Demo03().add(2, 3);
System.out.println(add);
}
public int add(int a,int b){
return a+b;
}
}
package com.oop.demo01;
public class Demo04 {
public static void main(String[] args) {
int a=1;
System.out.println(a);
Demo04.change(a);
System.out.println(a);
}
public static void change(int a){
a=10;
}
}
package com.oop.demo01;
public class Demo05 {
public static void main(String[] args) {
Person person = new Person();
System.out.println(person.name);
Demo05.change(person);
System.out.println(person.name);
}
public static void change(Person person){
person.name="落笔成念";
}
}
class Person{
String name;
}
类与对象的关系
- 类是一种抽象的数据类型,它是对某一事物整体描述/定义,但是并不能代表某一个具体的事物
- 动物、植物、手机、电脑…
- Person类、Pet类、Car类等,这些类都是用来描述/定义某一类具体的事物应该具备的特点和行为
- 对象是抽象概念的具体实例
- 张三就是人的一个具体实例,张三家的旺财就是狗的一个具体事例
- 能够体现出特点,展现出功能的是具体的实例,而不是一个抽象的概念
创建与初始化对象
- 使用new关键字创建对象
- 使用new关键字创建的时候,除了分配内存空间之外,还会给创建好的对象进行默认的初始化以及对类中构造器的调用
- 类中的构造器也称为构造方法,是在进行创建对象的时候必须要调用的
- 构造器的特点:
- 必须和类的名字相同
- 必须没有返回类型,也不能写void
package com.oop.demo02;
public class Student {
String name;
int age;
public void study(){
System.out.println(this.name+"在学习");
}
}
package com.oop.demo02;
public class Application {
public static void main(String[] args) {
Student xiaoming = new Student();
Student xiaohong = new Student();
xiaoming.name="小明";
xiaoming.age=10;
System.out.println(xiaoming.name);
System.out.println(xiaoming.age);
xiaohong.name="小红";
xiaohong.age=10;
System.out.println(xiaohong.name);
System.out.println(xiaohong.age);
}
}
构造器
package com.oop.demo02;
public class Application {
public static void main(String[] args) {
Person person = new Person("落笔",12);
System.out.println(person.name);
System.out.println(person.age);
}
}
package com.oop.demo02;
public class Person {
String name;
int age;
public Person(){
this.name="落笔成念";
}
public Person(String name,int age){
this.name=name;
this.age=age;
}
}
package com.oop;
import com.oop.demo03.Pet;
public class Application {
public static void main(String[] args) {
Pet dog = new Pet();
dog.name="大黄";
dog.age=2;
dog.shout();
System.out.println(dog.name);
System.out.println(dog.age);
Pet cat = new Pet();
}
}
package com.oop.demo03;
public class Pet {
public String name;
public int age;
public void shout(){
System.out.println("叫了一声");
}
}
创建对象内存分析
