Java 构造器、this关键字、final关键字、数组容器(一维、二维数组)
构造器也叫构造方法
构造方法和普通方法的区别:
-
构造方法的修饰符只有权限修饰符,普通方法还可以有static等关键词
-
构造器是无返回值类型的(连void都没有),普通方法是有返回值的
-
构造器的名字只能和类名一样,普通方法的方法名可以随意写
构造器的作用:用于创建对象开辟空间、做初始化操作
class User{
private int age;
//无参构造器
public User(){
System.out.println("--无参构造器--");
}
}
public class ConstrutorDemo{
public static void main(String[] args){
//所谓的创建对象其实就是通过new关键字去调用了构造方法
User user = new User();
}
}
构造器的特点
-
当类中没有提供构造器时,虚拟机会默认给提供一个无参的构造器,如果有构造器,虚拟机就不再提供默认的
-
构造器可以重载的形式存在多个,具体根据实际需求进行选择使用
public class ConstructorDemo2{
public static void main(String[] args){
/*Person p1 = new Person();
p1.setAge(12);
System.out.println(p1.getAge());*/
Person p2 = new Person(13);
System.out.println(p2.getAge());
}
}
class Person{
private int age;
private String name;
//public Person(){}
public Person(int newAge){
age = newAge;
}
public Person(String newName){
name = newName;
}
public Person(String newName,int newAge){
name = newName;
age = newAge;
}
public Person(int newAge,String newName){
name = newName;
age = newAge;
}
public void setAge(int newAge){
age = newAge;
}
public int getAge(){
return age;
}
}
this关键字
概念:this代指当前对象
运用前提的思考点:类中所有的非静态方法和属性都需要通过对象调用
思考以下代码的结果:
public class ConstructorDemo3{
public static void main(String[] args){
People p = new People();
p.setAge(12);
System.out.println(p.getAge());
}
}
class People{
private int age;
public People(){}
//注意:成员变量和局部变量名字冲突时,在方法中优先使用局部变量,因此想要将局部变量的值赋值给成员变量就需 //要将成员变量和局部变量区分开来,此时就需要使用到this关键字
public void setAge(int age){
age = age;
}
public int getAge(){
return age;
}
}
//结果为0
//修改后的方法为
public void setAge(int age){
this.age = age;
}
结论:类中所有的非静态方法和变量都默认持有一个this对象,this可省略
示例:
public class ConstructorDemo3{
public static void main(String[] args){
People p = new People("haha",13);
//p.setAge(12);
System.out.println(p.getAge());
}
}
//this指代当前对象
//类中的非静态的属性和方法都默认持有一个this对象
class People{
private int age;
private String name;
public People(){}
public People(int age){
this.age = age;
}
public People(String name){
this.name = name;
}
public People(String name,int age){
this(age);
this.name = name;
//this.age = age;
}
public People(int age,String name){
this.name = name;
this.age = age;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
public void show(){}
//方法可互相调用--都默认持有一个this对象
public void run(){
this.show();
}
}
构造代码块
代码块:在java中{}就称为代码块,例如:if(){}条件语句代码块 for(){}循环语句代码块
构造代码块:构造器后面的大括号
public class CodeArea{
public static void main(String[] args){
User user = new User(12);
user.show();
}
}
class User{
private int age;
{
System.out.println("匿名构造代码块1");
}
public User(int age){
System.out.println("构造器2");
this.age = age;
}
public void show(){
System.out.println("--show3--");
}
}
静态构造代码块:
-
static修饰的代码块或者变量是随着类的加载而加载的
-
静态构造代码块是没有名字的,因为是和构造器相关联,所以会在创建对象时自动调用
//执行的顺序为:4 1 2
public class CodeArea{
public static void main(String[] args){
User user = new User();
/*user.show();*/
}
}
class User{
private int age;
//和构造器就差个名字,因此不能直接调用,在创建对象时自动调用
/*public User(int age){
System.out.println("构造器2");
this.age = age;
}*/
public User(){
System.out.println("无参构造器2");
}
{
System.out.println("匿名构造代码块1");
}
static{
System.out.println("静态构造代码块4");
}
public void show(){
System.out.println("--show3--");
}
public static void run(){
System.out.println("--run5--");
}
}
5 变量的作用域
成员变量:定义在类中的变量统称为成员变量,可以在整个类中使用,都有默认值
int-0 boolean-false double-0.0 引用类型-null
局部变量:定义在方法中的统称为局部变量,只能在当前的方法中使用,都需要初始化
//考察的是作用域及方法的返回值问题
public class Practice{
static int count = 5;
public static void main(String[] args){
//int count = 6;
add(count);
System.out.println(count);
}
public static int add(int count){
count++;
return count;
}
}
//考察的是static的资源共享及作用域
public class Practice{
static int count = 5;
public static void main(String[] args){
//int count = 6;
add();
System.out.println(count);
}
public static int add(){
count++;
return count;
}
}
6 final关键字
概念:final最终的 finally最终地
-
final修饰的变量称为常量,只能赋一次值
-
final修饰的方法称为最终方法,不能被重写
-
final修饰的类称为最终类,不能被继承
应用场景:对于结果不变且常用的变量,都会修饰常量使用,例如:春夏秋冬、异常代码200 404 505、状态(订单、支付、配送等)0(已取消) 1(已支付) 2(未支付) 3(配送中) 4(已完成)
public class FinalDemo{
final static int ORDER_CANCEL = 0;
static final int ORDER_PAY = 1;
static final int ORDER_NO_PAY = 2;
static final int ORDER_SENDING = 3;
static final int ORDER_DONE = 4;
public static void main(String[] args){
//无法为最终变量order_cancel分配值
//order_cancel = 8;
//订单的状态
int status = 0;
switch(status){
case ORDER_CANCEL:
System.out.println("订单已取消");
break;
case ORDER_PAY:
System.out.println("订单已支付");
break;
case ORDER_NO_PAY:
System.out.println("订单未支付");
break;
case ORDER_SENDING:
System.out.println("订单配送中");
break;
case ORDER_DONE:
System.out.println("订单已完成");
break;
}
//final int a = 1;
//a = 2;
final int b ;
b = 1;
}
}
数组是一个容器,当需要存储多个数据的时候,就需要有这么一个容器,以方便存储使用
数组的特点:大小固定,数组中能存储是数据必须是同一种类型的数据
数组的优缺点:查询快(下标),增删速度慢
语法:
需求:定义一个大小为3的int类型的数组
语法一:数据类型[] 变量名 = new 数据类型[size];//size是指一个具体的数字,表示数组的大小
int[] arr = new int[3];
String[] arr = new Strinrg[3];
语法二:数据类型[] 变量名 = new 数据类型[]{值,...};//直接给定结果,值的多少决定了容器的大小
int[] arr = new int[]{1,2,3};
String[] arr = new String[]{"a","b","c"};
语法三:数据类型[] 变量名 = {值,...};//简写形式
int[] arr = {1,2,3};
String[] arr = {"a","b","c"};
//注意点:数组的下标起始位置是从0开始的,这是不能变的
//获取数组大小的属性---length
System.out.println(arr.length);//3
//获取数组中的值
arr[0]//a arr[1] //b arr[2]//c
//定义一个数组,大小为3,并输出数组中的值及对应的下标
int[] arr = {1,2,3};
for(int i=0;i<arr.length;i++){
System.out.println("下标为"+i+",值为"+arr[i]);
}
//当只是定义了数组的大小,那么该数组中是否有值----都有默认值
int[] arr = new int[3];
System.out.println(arr[0]);//0
System.out.println(arr[1]);//0
System.out.println(arr[2]);//0
System.out.println(arr[3]);//报错,数组下标越界异常,ArrayIndexOutofBoundsException
String[] arr = new String[3];
System.out.println(arr[0]);//null
System.out.println(arr[1]);//null
System.out.println(arr[2]);//null
System.out.println(arr[3]);//报错,数组下标越界异常,ArrayIndexOutofBoundsException
//需求:输出字符串的长度
//获取字符串的长度--length()
String[] nameArr = {"haha","heheda","heihei"};
for(int i=0;i<nameArr.length;i++){
//1、找到要删除的元素 i==1
if(i==1){
//2、将该位置的值改为null
nameArr[i] = null;
}
System.out.println("字符串为:"+nameArr[i]+",长度为:"+nameArr[