首页 > 其他 > 详细

foreach遍历、包装类、Object类

时间:2017-09-26 12:30:44      阅读:265      评论:0      收藏:0      [点我收藏+]

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是所有类的父类
    }
}

 

foreach遍历、包装类、Object类

原文:http://www.cnblogs.com/FrankLiner/p/7596035.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!