初识面向对象
1. 万物皆对象
自然界中的任何事物,都可以理解为一个对象。都可以通过分析其共有的特征,和共有的行为进行描述。
2. 类和对象的关系
类是对象的抽象,比如学生类,我们无法确定这个是哪个学生,所以是抽象的概念
对象是类的具体,比如赵四学生,这是一个真真实实存在的个体
3. 编写类和对象
package com.edu.test1; /** * 类和对象的关系:类是对象的抽象,对象是类的具体 * * 学生类 * 属性(一类事物共有的特征): * 姓名 * 年龄 * 性别 * 身高 * 学号 * 体重 * 班级 * 爱好 * …… * 方法(一类事物共有的行为) * 学习 * 运动 * 吃饭 * 睡觉 * 打游戏 * 谈恋爱 * */ public class Student { String name; // 实例变量 属性 int age; String sex; double height; String studentNO; double weight; String gradeName; String hobby; // 实例方法 public void study() { System.out.println("认真学习"); } public void sport() { System.out.println("锻炼身体"); } public void eat() { System.out.println("吃饭"); } public void sleep() { System.out.println("睡觉"); } public void playGame() { System.out.println("打游戏"); } public static void main(String[] args) { // 1.创建对象 通过new关键字 类名 对象名 = new 类名(); Student stu1 = new Student(); // 2.给当前对象的属性赋值 具体化 对象名.属性名 = 值 stu1.name = "赵四"; stu1.age = 17; stu1.gradeName = "三年二班"; stu1.height = 180; stu1.sex = "男"; stu1.studentNO = "zz666"; stu1.weight = 75; stu1.hobby = "尬舞"; // 3.访问当前对象的行为 对象名.方法名(); stu1.study(); stu1.eat(); stu1.sleep(); stu1.playGame(); stu1.sport(); System.out.println("============================================="); Student stu2 = new Student(); stu2.name = "广坤"; stu2.gradeName = "三年二班"; stu2.height = 175; stu2.weight = 65; stu2.sex = "男"; stu2.studentNO = "zz888"; stu2.hobby = "口技"; stu2.age = 18; stu2.study(); stu2.sleep(); stu2.sport(); stu2.playGame(); stu2.eat(); } }
4. 实例变量和局部变量的区别
特点 | 局部变量 | 实例变量 |
---|---|---|
存储位置 | 基本数据类型全部存在栈中 引用数据类型名字在栈,值在堆 | 全部存在堆中 |
生命周期 | 随着方法的入栈而生效,随着方法的出栈而死亡 | 随着对象的创建而生效,随着对象被垃圾回收而死亡 |
package com.edu.test2; /** * 类和对象的关系:类是对象的抽象,对象是类的具体 * * 学生类 * * 使用属性完善行为 * * 实例变量(属性): * 定义位置:直接写在类 * 作用范围:整个类中 * 是否可以重名:可以与局部变量重名,就近原则使用 * 默认值:有默认值,与数组相同,当我们new完对象,默认值即存在 * * 存储位置:全部存放在堆中 * 生命周期:随着对象的创建而有生命,随着对象被垃圾回收GC而死亡 * */ public class Student { String name; // 实例变量 属性 int age; char sex; double height; String studentNO; // 实例方法 public void study() { String name = "大拿"; System.out.println(name + "认真学习"); } public void sport() { System.out.println(name + "锻炼身体"); } public void printInfo() { System.out.println("我的名字是:" + name+ ",年龄是:" + age + ",性别是" + sex + ",身高是:" + height + ",学号是:" + studentNO); } public static void main(String[] args) { Student stu1 = new Student(); stu1.printInfo(); } }
5. 方法重载
方法重载:同一个类中,方法名相同,参数列表不同,与返回值,访问修饰符无关
参数列表不同:包括参数的个数、类型、顺序,有一个不同即可称之为不同
package com.edu.test3; /** * 方法重载:同一个类中,方法名相同,参数列表不同,与返回值,访问修饰符无关 * 参数列表不同:包括参数的个数、类型、顺序,有一个不同即可称之为不同 * */ public class Calc { public void add(int a,int b) { System.out.println(a + b); } // public void add(int c,int d) { // System.out.println(c + d); // } public void add(int a,int b,int c) { System.out.println(a + b + c); } public void add(int a,int b,int c,int d) { System.out.println(a + b + c + d); } public void add(int a,int b,int c,int d,int e) { System.out.println(a + b + c + d + e); } public void add(int a,int b,int c,int d,int e,int f) { System.out.println(a + b + c + d + e + f); } public static void main(String[] args) { Calc calc = new Calc(); calc.add(10, 20); calc.add(10, 20, 30); } }
package com.edu.test3; public class Test1 { public void m1(int a,String str) { System.out.println("第一个参数为int,第二个参数为String"); } public void m1(String str,int a) { System.out.println("第一个参数为String,第二个参数为int"); } public void m1(String str1,String str2) { System.out.println("两个参数都为String"); } public void m1(boolean bl1) { System.out.println("一个boolean类型参数"); } public static void main(String[] args) { Test1 test1 = new Test1(); test1.m1(true); } }
6. 构造方法和构造方法重载
构造方法:构造方法是用于创建对象的特殊方法
书写格式:访问修饰符 + 类名(形参列表){}
实例方法:访问修饰符 + 返回值类型 + 方法名(形参列表){}
无参构造方法是JVM编译器默认提供 不管是否书写都存在,如果书写了有参构造方法,无参的将被覆盖,想要使用必须显式的书写
构造方法是用于创建对象的,有参构造还可以创建对象同时给属性赋值
package com.edu.test4; /** * 人类 * 名字 * 年龄 * 身高 * 性别 * 自我介绍 * * 构造方法:构造方法是用于创建对象的特殊方法 * 书写格式:访问修饰符 + 类名(形参列表){} * 实例方法:访问修饰符 + 返回值类型 + 方法名(形参列表){} * * 无参构造方法是JVM编译器默认提供 不管是否书写都存在,如果书写了有参构造方法,无参的将被覆盖,想要使用 * 必须显式的书写 * * 构造方法是用于创建对象的,有参构造还可以创建对象同时给属性赋值 * */ public class Person { String name; int age; double height; char sex; public void printInfo() { System.out.println("名字:" + name + ",年龄:" + age + ",身高:" + height + ",性别:" + sex); } public Person() { System.out.println("Person类无参构造方法执行了"); } public Person(String n) { name = n; } public Person(String n,int a) { name = n; age = a; } public Person(String n,int a,double h) { name = n; age = a; height = h; } public Person(String n,int a,double h,char s) { name = n; age = a; height = h; sex = s; } public Person(int a,double h,char s,String n) { name = n; age = a; height = h; sex = s; } public Person(String n,char s) { name = n; sex = s; } public static void main(String[] args) { Person p1 = new Person(); p1.printInfo(); System.out.println("==========================="); Person p2 = new Person("赵四", 18, 175, '男'); p2.printInfo(); System.out.println("==========================="); Person p3 = new Person("广坤"); p3.printInfo(); } }
7. this关键字
this可以访问 属性、方法、构造方法
this关键字访问本类的构造方法 必须在构造方法的第一句
package com.edu.test5; /** * 人类 * 名字 * 年龄 * 身高 * 性别 * * this :这个 * this表示当前正在创建对象 * * this可以访问 属性、方法、构造方法 * */ public class Person { String name; int age; double height; char sex; public void printName() { System.out.println("我的名字是:" + name); } public void printAge() { System.out.println("我的年龄是:" + age); } public void printHeight() { System.out.println("身高是:" + height); } public void printInfo() { this.printName(); this.printAge(); printHeight(); System.out.println("性别:" + sex); } public Person() { System.out.println("Person类无参构造方法执行了"); } public static void main(String[] args) { Person p1 = new Person("赵四"); p1.printInfo(); Person p2 = new Person("广坤", 20); p2.printInfo(); Person p3 = new Person("大拿", 22, 188); p3.printInfo(); Person p4 = new Person("小宝", 22, 195, '男'); p4.printInfo(); } public Person(String name) { this.name = name; } public Person(String name,int age) { this(name); // this关键字访问本类的构造方法 必须在第一句 this.age = age; } public Person(String name,int age,double height) { this(name, age); this.height = height; } public Person(String name,int age,double height,char sex) { this(name, age, height); this.sex = sex; } }