一. 面向对象的概念:
面向对象即使用程序高度模拟现实生活的事物。
二. 什么是类和对象
面向对象可以概述为类和对象。
而类就是相同事物共同特征的描述。例如:人类,学生类,动物类,手机,类是抽象的概念,并不是具体存在的。
对象就是是类具体存在的实例。
三. 类的定义方式:
修饰符 class 类名{};
其中修饰符为:public,protected,default,private.一个类中只能有一个修饰符
类名首字母大写。
四. 类中的成分:
1. 成员变量: 描述类或者对象的属性信息
2. 成员方法: 描述类或者对象具有的行为
3. 构造器(构造方法): 初始化对象
4. 内部类
5. 代码块
五. 成员变量:
1. 概念:成员变量是用来描述类或者对象的属性信息
2. 格式:修饰符 数据类型 变量名 ;
3. 成员变量瘦子木小写,满足于“驼峰式”命名规则
public class Demo{
public String name;
//后面的赋值内容可写可不写
public int age = 10;
public String personAddress;
}
4. 成员变量的默认值:遵循基本数据类型的默认值,引用类型的默认值为null.
public class Demo {
public String str;
public byte b;
public short s;
public int i;
public long l;
public char c;
public float f;
public double d;
public boolean bl;
import java.util.ArrayList;
public class Demo {
public String str;
public byte b;
public short s;
public int i;
public long l;
public char c;
public float f;
public double d;
public boolean bl;
public ArrayList arrayList;
public int[] arr;
public static void main(String[] args) {
Demo demo = new Demo(); //调用了无参构造器,创建了demo对象,参考第七点
System.out.println(demo.arr); // null
System.out.println(demo.arrayList); //null
System.out.println(demo.str); //null
System.out.println("========");
System.out.println(demo.b); // 0
System.out.println(demo.s); //0
System.out.println(demo.i); //0
System.out.println(demo.l); //0
System.out.println("========");
System.out.println(demo.c); //为空
System.out.println("=========");
System.out.println(demo.f); //0.0
System.out.println(demo.d); //0.0
System.out.println("=========");
System.out.println(demo.bl); //false
}
}
public static void main(String[] args) {
Demo demo = new Demo(); //调用了无参构造器
System.out.println(demo.str); //null
System.out.println("========");
System.out.println(demo.b); // 0
System.out.println(demo.s); //0
System.out.println(demo.i); //0
System.out.println(demo.l); //0
System.out.println("========");
System.out.println(demo.c); //为空
System.out.println("=========");
System.out.println(demo.f); //0.0
System.out.println(demo.d); //0.0
System.out.println("=========");
System.out.println(demo.bl); //false
}
}
六. 局部变量和成员变量的区别:
/**
* 一 局部变量和成员变量符区别:
* 1. 位置
* 局部变量定义在类中,方法之外
* 局部变量定义在类中的方法里面,也可以定义在方法中的参数里
*
* 2. 作用范围:
* 局部变量只能在定义该局部变量的方法里访问的到
* 成员变量可以在该类中访问
*
* 3. 生命周期:
* 局部变量跟方法共生死
* 成员变量跟类共生死
*
* 4. 内存位置不同:
* 局部变量在栈内存中
* 成员变量在堆内存中
*
*
*/
七. 对象的创建方式和访问成员变量/方法()的格式:
1.创建方式: 类名 对象名 = new 构造器();
2.访问格式: 对象名.成员变量/成员方法();
八. 构造器的使用
一. 什么是构造器:
构造器即构造方法,构造器分为有参构造器和无参器,其作用就是为对象进行初始化。
二. 构造器的格式(构造器没有返回值,也不能用void):
修饰符 类名(){}
public class Demo{
public String name;
public int age;
//无参构造器
public Demo(){};
//有参构造器
public Demo(String name, int age){
this.name = name;
this.age = age; //其中this暂时不用理解
};
}
三. 注意事项:
1. 如果没有创建构造器,在创建对象时,会自动调用无参构造器。
2. 如果创建对象时,调用了有参构造器,则不会再调用无参构造器
3. 每次创建对象都会调用一次构造器。
public class Demo {
public String name;
public Demo(){
System.out.println("调用了无参构造器");
}
public Demo(String name) {
this.name = name;
System.out.println(name+"调用了有参构造器");
}
public static void main(String[] args) {
Demo demo = new Demo(); //调用了无参构造器
System.out.println("========");
Demo demo1 = new Demo("张三"); //张三调用了有参构造器
}
}