美文网首页
第四章:初始化和清除

第四章:初始化和清除

作者: pgydbh | 来源:发表于2018-04-17 09:29 被阅读6次

4.1用构建器自动初始化

第一种情况
public class Four {

    public static void main(String[] args){
        new Four("自动初始化");
    }

    public Four(String temp){
        System.out.println(temp);
    }
}
第二种情况
public class Four {

    public static void main(String[] args){
        new Four("自动初始化");
    }

    private String temp;
    public Four(String temp){
        this.temp = temp;
        System.out.println(this.temp);
    }
}

4.2区别不同函数的方法

1.参数类型,函数名 任一不同为不同函数
public class Four {

    public static void main(String[] args){
        new Four("自动初始化");
    }

    private String tempStr;
    private int tempInt;

    public Four(String temp){
        this.tempStr = temp;
        System.out.println(this.tempStr);
    }

    public Four(int temp){
        this.tempInt = temp;
    }

    public Four(int tempInt, String tempStr){
        this.tempInt = tempInt;
        this.tempStr = tempStr;
    }

    public String func(int tempInt){
        return tempInt + "";
    }

    public String func(String tempStr){
        return tempStr;
    }
}
2.!!!返回类型不能决定。(如下,编译器提示为函数已定义,不能重复定义)
无标题.png
3.默认构建器 如果不写构造函数 默认执行空的构造函数
public class Four {

    public static void main(String[] args){
        Four four = new Four();
    }
  /*
    public Four(){
        
    }
    */
}
4.如果有必须选择一个执行
public class Four {

    public static void main(String[] args){
        Four four = new Four("构造器");
    }


    public Four(String temp){
        System.out.println(temp);
    }

    public Four(int temp){
        System.out.println(temp);
    }
}
5.this关键字(演示用法)

用法1
代码

public class Four {

    public static void main(String[] args){
        Four four = new Four();
    }


    private String tempStr;
    public Four(){
        tempStr = "爸爸";
        Inner inner = new Inner();
    }

    private class Inner{
        private String tempStr;
        public Inner(){
            tempStr = "儿子";
            System.out.println(Four.this.tempStr);
            System.out.println(tempStr);
        }
    }
}

输出

爸爸
儿子

Process finished with exit code 0

用法2
代码

public class Four {

    public static void main(String[] args){
        Four four = new Four("构造器");
    }
    
    private String tempStr;
    public Four(String tempStr){
        this.tempStr = tempStr;
        System.out.println(tempStr);
        System.out.println(this.tempStr);
    }
}

输出

构造器
构造器

Process finished with exit code 0
6.成员初始化

1.默认初始化

public class Four {

    public static void main(String[] args){
        Four four = new Four();
    }

    private String tempStr;
    private char tempChar;
    private boolean tempBool;
    private int tempInt;
    private float tempFloat;
    public Four(){
        System.out.println(tempStr);
        System.out.println(tempChar);
        System.out.println(tempBool);
        System.out.println(tempInt);
        System.out.println(tempFloat);
    }
}

output:
null
 
false
0
0.0

Process finished with exit code 0

2.赋值初始化

public class Four {

    public static void main(String[] args){
        Four four = new Four();
    }

    private String tempStr = "字符串";
    private char tempChar = 'a';
    private boolean tempBool = true;
    private int tempInt = 10;
    private float tempFloat = 10.2f;
    private double tempDouble = 10.2;
    public Four(){
        System.out.println(tempStr);
        System.out.println(tempChar);
        System.out.println(tempBool);
        System.out.println(tempInt);
        System.out.println(tempFloat);
        System.out.println(tempDouble);
    }
}

output:
字符串
a
true
10
0.0
10.2

Process finished with exit code 0

3.类也可以直接初始化

public class Four {

    public static void main(String[] args){
        Four four = new Four();
    }

    private Inner inner = new Inner();
    public Four(){
        inner.out();
    }

    private class Inner{
        public void out(){
            System.out.println("我是某某");
        }
    }
}

output:
我是某某

Process finished with exit code 0

4.构造器初始化(常用 灵活)

public class Four {

    public static void main(String[] args){
        Four four = new Four(7);
    }

    private int a;
    private int b;
    public Four(int a){
        this.a = a;
        this.b = a + 3;
        System.out.println("a = " + a);
        System.out.println("b = " + b);
    }
}

output:
a = 7
b = 10

Process finished with exit code 0

5.数组初始化

public class Four {

    public static void main(String[] args){
        int[] a1 = { 1, 2, 3, 4, 5 };
        int[] a2;
        int[] a3 = new int[10];
        a2 = a1;
        System.out.println("a2");
        for(int i = 0; i < a2.length; i++)
            System.out.print(a2[i] + " ");
        System.out.println("\na1");
        for(int i = 0; i < a1.length; i++)
            System.out.print(a1[i] + " ");
        System.out.println("\na3");
        for(int i = 0; i < a3.length; i++)
            System.out.print(a3[i] + " ");
    }
}

output:
a2
1 2 3 4 5 
a1
1 2 3 4 5 
a3
0 0 0 0 0 0 0 0 0 0 
Process finished with exit code 0

6.多维数组初始化

public class Four {

    public static void main(String[] args){
        int[][] a1 = {{ 1, 2, 3, 4, 5 },{ 1, 2, 3, 4, 5 }};
        int[][] a2 = a1;
        int[][] a3 = new int[2][5];
        
        System.out.println("a2");
        for(int i = 0; i < a2.length; i++)
            for (int j = 0; j < a2[i].length; j++)
                System.out.print(a2[i][j] + " ");

        System.out.println("\na1");
        for(int i = 0; i < a1.length; i++)
            for (int j = 0; j < a1[i].length; j++)
                System.out.print(a1[i][j] + " ");

        System.out.println("\na3");
        for(int i = 0; i < a3.length; i++)
            for (int j = 0; j < a3[i].length; j++)
                System.out.print(a3[i][j] + " ");
    }
}

output:
a2
1 2 3 4 5 1 2 3 4 5 
a1
1 2 3 4 5 1 2 3 4 5 
a3
0 0 0 0 0 0 0 0 0 0 
Process finished with exit code 0

相关文章

网友评论

      本文标题:第四章:初始化和清除

      本文链接:https://www.haomeiwen.com/subject/fvtukftx.html