foreach遍历、包装类、Object类

1.foreach

public class Foreach1 {
    public static void main(String[] args) {
        int[] ins=new int[]{1,4,6,7,2};//整数数组
        for(int i:ins){ //foreach循环,简化了for;
            System.out.print(i+" "); //此处i就是数组具体的值
            //错误代码: System.out.println(ins[i]);
            
        }
    }
}

2.包装类

class X{
    private char ch;
    public X(){};
    public X(char ch){
        this.ch=ch;
    }
    public char getCh(){return this.ch;}
    //包装的目的,是为了调用其他普通方法
    
    public char daxie(){//将字符ch转为大写的方法
        return (char)(ch-32);
    }
}

public class Baozhuanglei {
    public static void main(String[] args) {
        char ch='r';
        X x=new X(ch); //包装类-> 包装到类的对象里面去
        char c=x.getCh();//从对象里取出来
        System.out.println(c);
        System.out.println(x.daxie());
    }
}

3.Object

class A{
    //此类隐含继承Object类
}

public class Objiekete {
    public static void main(String[] args) {
        A a=new A();
        a.toString();//toString方法继承的是Object类的方法
        a.equals(a); //同上
        
        //object是所有类的父类
    }
}
原文地址:https://www.cnblogs.com/FrankLiner/p/7596035.html