01面向过程与面向对象思想对比
面线过程编程: 自己完成所有的步骤.
面向对象编程: 完成任何一项功能,首先要做的事,就是看看有没有已经写好的,然后,拿过来.
//自己不干活,找别人帮忙干活.
02类和对象的关系
类:是一组具有相同属性和行为的事物的统称.
//类就是类别,比如”人类”,”鱼类”,”哺乳类”,”汽车类”
//属性就是类拥有的”名词”,行为就是类拥有的”动词”
对象:实际存在的事物.万物皆对象.
//其实就是存在的个体. “这个人”,”这条鱼”,”那辆汽车”
03类的定义
public class 类名{//成员变量(属性)//成员方法(行为)}
04对象的创建和使用
//注意: 类本身是无法使用的.所以,所谓的使用类,其实是使用类的对象.
创建对象: 类名 对象名 = new 类名();
使用对象:
对象名.成员变量;
对象名.成员方法(…);
05案例手机类的创建和使用
package com.itheima.test1;public class TestPhone {public static void main(String[] args) {// 1. 创建对象Phone p = new Phone();// 2. 给成员变量进行赋值p.brand = "大米";p.price = 2999;// 3. 打印赋值后的成员变量System.out.println(p.brand + "..." + p.price);// 4. 调用成员方法p.call("阿强");p.sendMessage();}}
package com.itheima.test1;public class Phone {// 品牌, 价格String brand;int price;// 打电话, 发短信public void call(String name){System.out.println("给"+name+"打电话");}public void sendMessage(){System.out.println("群发短信");}}
package com.itheima.object1;public class Student {// 属性 : 姓名, 年龄// 成员变量: 跟之前定义变量的格式一样, 只不过位置发生了改变, 类中方法外String name;int age;// 行为 : 学习// 成员方法: 跟之前定义方法的格式一样, 只不过去掉了static关键字.public void study(){System.out.println("学习");}}
package com.itheima.object1;public class TestStudent {/*创建对象的格式:类名 对象名 = new 类名();调用成员变量的格式:对象名.变量名调用成员方法的格式:对象名.方法名();*/public static void main(String[] args) {// 类名 对象名 = new 类名();Student stu = new Student();// 对象名.变量名// 默认初始化值System.out.println(stu.name); // nullSystem.out.println(stu.age); // 0stu.name = "张三";stu.age = 23;System.out.println(stu.name); // 张三System.out.println(stu.age); // 23// 对象名.方法名();stu.study();// com.itheima.object1.Student@b4c966a// 全类名(包名 + 类名)System.out.println(stu);}}
06单个对象内存图
07两个对象内存图
08两个引用指向同一个对象内存图
09成员变量和局部变量的区别
10 private关键字
package com.itheima.object2;/*学生类private : 私有的private是一个权限修饰符可以用来修饰成员(变量,方法)特点: 只能在本类当中进行访问*/public class Student {String name;private int age;// 设置值public void setAge(int a){if(a >= 0 && a <= 120){age = a;}else{System.out.println("您的年龄不合理");}}// 获取值public int getAge(){return age;}public void show(){System.out.println(name + "..." + age);}}
package com.itheima.object2;/*学生测试类*/public class TestStudent {public static void main(String[] args) {Student stu = new Student();stu.name = "张三";//stu.age = 23;stu.setAge(-23);stu.show();}}
11 private关键字的使用
12this关键字
package com.itheima.mthis;public class Student {private String name;public void setName(String name){System.out.println("[方法中打印this]");System.out.println(this);this.name = name;}}
package com.itheima.mthis;public class TestStudent {public static void main(String[] args) {Student s1 = new Student();System.out.println("[打印对象名]--> s1");System.out.println(s1);s1.setName("张三");System.out.println("----------------------");Student s2 = new Student();System.out.println("[打印对象名]--> s2");System.out.println(s2);s2.setName("李四");}}
13this内存原理
14封装
package com.itheima.test2;public class Student {private String name;private int age;public void setName(String name){this.name = name;}public String getName(){return name;}public void setAge(int age){this.age = age;}public int getAge(){return age;}public void show(){System.out.println(name + "..." + age);}}
package com.itheima.test2;public class TestStudent {public static void main(String[] args) {Student stu = new Student();stu.setName("张三");stu.setAge(23);System.out.println(stu.getName());System.out.println(stu.getAge());int age = stu.getAge();for(int i = 1; i <= age; i++){System.out.println("生日快乐");}stu.show(); // 仅仅是对数据做展示}}
package com.itheima.test2;public class Test2Student {public static void main(String[] args) {Student s1 = new Student();s1.setName("张三");s1.setAge(23);s1.show();}}
15构造方法的格式和执行时机
创建对象时执行
package com.itheima.constructor;public class Student {/*格式:1. 方法名需要跟类名相同, 大小写也要一致2. 没有返回值类型, 连void都没有3. 没有具体的返回值(不能由return带回具体的结果)*/private String name;private int age;// 1. 如果一个类中没有编写任何构造方法, 系统将会提供一个默认的无参数构造方法public Student(){}// 2. 如果手动编写了构造方法, 系统就不会再提供默认的无参数构造方法了public Student(String name, int age){this.name = name;this.age = age;System.out.println("我是Student类的构造方法");}public void show(){System.out.println(name + "..." + age);}}
package com.itheima.constructor;public class TestStudent {public static void main(String[] args) {Student stu1 = new Student("张三",23);stu1.show();Student stu2 = new Student();}}
16构造方法的作用
17构造方法的注意事项
18标准类的代码编写与使用
package com.itheima.test3;/*JavaBean类: 封装数据*/public class Student {private String name;private int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public void show(){System.out.println(name + "..." + age);}}
package com.itheima.test3;public class TestStudent {public static void main(String[] args) {// 1. 无参数构造方法创建对象, 通过setXxx方法给成员变量进行赋值Student stu1 = new Student();stu1.setName("张三");stu1.setAge(23);stu1.show();// 2. 通过带参数构造方法, 直接给属性进行赋值Student stu2 = new Student("李四",24);stu2.show();}}
