OnJava8_初始化和清理

2020-01-07  本文已影响0人  啊啊啊哼哼哼

Java对象初始化通过构造器,清理通过垃圾收集器(Garbage Collector, GC)

构造器

无参构造函器,有参构造器
如果创建一个类,没有自定义构造器,编译器会自动创建一个无参构造器

方法重载

函数名相同,参数个数或者参数类型不同,也可参数顺序不同(不推荐)

this关键字

public class Leaf{
int i = 0;
Leaf increment(){
   this.i++;
   return this;
}
void print(){
   System.out.println("i = "+i);
}
public static void main(String [] args){
   Leaf x = new Leaf();
   x.increment().increment().increment.print();
}
}
class Person {
    public void eat(Apple apple) {
        Apple peeled = apple.getPeeled();
        System.out.println("Yummy");
    }
}

public class Peeler {
    static Apple peel(Apple apple) {
        // ... remove peel
        return apple; // Peeled
    }
}

public class Apple {
    Apple getPeeled() {
        return Peeler.peel(this);
    }
}

public class PassingThis {
    public static void main(String[] args) {
        new Person().eat(new Apple());
    }
}
public class Flower {
    private int preCount = 0;
    String s = "initial value";
    Flower(int preCount){
        this.preCount = preCount;
        System.out.println("Constructor w/ int arg only, preCount = " + preCount);
    }
    Flower(String s){
        System.out.println("Constructor w/ string arg only, s = " + s);
        this.s = s;
    }
    Flower(int preCount, String s){
        this(preCount);
        this.s = s;
        System.out.println("String & int args");
    }
    Flower(){
        this(7,"hi");
        System.out.println("no-arg constructor");
    }
    public void print(){
        System.out.println("preCount = " + preCount + " s = " + s);
    }

    public static void main(String []args){
        Flower flower1 = new Flower();
        flower1.print();
//        Tree tree1 = new Tree(1);
//        flower1.print();
//        flower1.preCount = 2;
//        flower1.print();
    }

}
class Tree{
    private int height;
    Tree(int height){
        this.height = height;
    }
}

在构造器中调用构造器

static的含义

垃圾回收器(Garbage Collector)

"标记-清扫"所依据的思路仍然是从栈和静态存储区出发,遍历所有的引用,找出所有存活的对象。但是,每当找到一个存活对象,就给对象设一个标记,并不回收它。只有当标记过程完成后,清理动作才开始。在清理过程中,没有标记的对象将被释放,不会发生任何复制动作。"标记-清扫"后剩下的堆空间是不连续的,垃圾回收器要是希望得到连续空间的话,就需要重新整理剩下的对象

构造器初始化

数组初始化

        int [] intA = new int[5];
        System.out.println(intA[0]);
        System.out.println(Arrays.toString(intA));
        System.out.println("*********");
        char [] charA = new char[5];
        System.out.println(charA[0]);
        System.out.println(Arrays.toString(charA));
        System.out.println("*********");
        Character [] characterA = new Character[5];
        System.out.println(characterA[0]);
        System.out.println(Arrays.toString(characterA));
        System.out.println("*********");
        Integer [] IntegerA = new Integer[5];
        System.out.println(IntegerA[0]);
        System.out.println(Arrays.toString(IntegerA));

结果:

0
[0, 0, 0, 0, 0]
*********
 
[ ,  ,  ,  ,  ]
*********
null
[null, null, null, null, null]
*********
null
[null, null, null, null, null]

可变参数列表

import java.util.Arrays;

public class OverloadingVarargs2 {
    static void f(float i, Character... args) {
        System.out.println(i);
        System.out.println(Arrays.toString(args));
        System.out.println("first");
    }

    static void f(Character... args) {
        System.out.println("second");
    }
//  修改为下面函数就不会报错
// static void f(char c, Character... args) {
//        System.out.println("second");
//    }

    public static void main(String[] args) {
//        f(1, 'a');
        f('a', 'b');
//        System.out.println((float) 'r');
    }
}

会报错

Error:(19, 9) java: 对f的引用不明确
  OverloadingVarargs2 中的方法 f(float,java.lang.Character...) 和 OverloadingVarargs2 中的方法 f(java.lang.Character...) 都匹配

因为char向上转换(隐式转换)到float和装箱为Character优先级相同,如果改为注释,char能找到对应的参数类型,就不会产生歧义。

枚举类型 enum

public enum Course {
    ENGLISH, CHINESE, MATH, PHYSICAL,
}
public class EnumTest {
    Course takeCourse;

    EnumTest(Course takeCourse) {
        this.takeCourse = takeCourse;
    }


    public static void main(String[] args) {
        Course course1 = Course.CHINESE;
        System.out.println(course1);
        for (Course c1 : Course.values())
            System.out.println(c1.ordinal());

        EnumTest test1 = new EnumTest(Course.CHINESE);
        EnumTest test2 = new EnumTest(Course.ENGLISH);
        EnumTest test3 = new EnumTest(Course.MATH);

        test1.printTakeCourse();
        test2.printTakeCourse();
        test3.printTakeCourse();
    }

    private void printTakeCourse() {
        switch (this.takeCourse) {
            case ENGLISH:
                System.out.println("take english");
                break;
            case CHINESE:
                System.out.println("take chinese");
                break;
            default:
                System.out.println("take other courses");
                break;
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读