栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

javaSE:基础知识【1】

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

javaSE:基础知识【1】

目录

1、this、super的用法

2、static关键字

3、Math常用方法

4、 Calendar、Date、DateFormate、System、String、StringBuilder类

5、迭代器

6、异常


1、this、super的用法
public class Fu {
   int num=20;
}
  
public class Zi extends Fu {
    int num = 20;
    public Zi(){
        //有了this,不再赠送super
        this(123);//本类的无参构造,调用本类的有参构造。即构造方法的重载调用。
        //this(1,2);调用成功的只能是第一个this且唯一一个。
    }
    public Zi(int n){
        this(1,2);
    }
    public Zi(int n,int m){
        //this();如果在调用无参,三个构造方法将循环,没有尽头所以报错。
    }
    public void showNumber() {
        int num = 10;
        System.out.println(num);//局部变量
        System.out.println(this.num);//本类的成员变量
        System.out.println(super.num);//父类的成员变量
    }
    public void methodA(){
        System.out.println("AAA");
    }
    public void methodB(){
        this.methodA();
        System.out.println("BBB");
    }
}
 

2、static关键字
 

3、Math常用方法
 

4、 Calendar、Date、DateFormate、System、String、StringBuilder类
import java.util.Calendar;
=======================================================================
========================================================================
========================================================================

 import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

============================================================

=========================================================================

5、迭代器
public class Iterator_01 {
    public static void main(String[] args) {
        //创建一个集合对象
        Collection coll=new ArrayList<>();//多态
        coll.add("姚明");
        coll.add("麦迪");
        coll.add("周琦");
        coll.add("詹姆斯");
        coll.add("艾弗森");
        //多态   接口       接口的实现类对象【这个方法在Collection类,所以先用他的一个对象调用着】
       Iterator it=coll.iterator();
        boolean b = it.hasNext();
        while(it.hasNext()){//优化后 这里它自动返回true或false作为循环体方法前提
            String e=it.next();
            System.out.print(e);//姚明麦迪周琦詹姆斯艾弗森
        }//或者【了解下】
        for(Iterator it2=coll.iterator();it2.hasNext();){//初始化表达式:是指针指向索引-1位。判断:如果指针后面有元素		步进表达式:省略 因为hasNext(),做了两件事:1 取出下一个元素 2 把指针向后移走一位
            String e=it2.next();
            System.out.print(e);姚明麦迪周琦詹姆斯艾弗森
        }

6、异常


}

public class Fu_lianxi {
    public void show01() throws NullPointerException,ClassCastException{}
    public void show02() throws IndexOutOfBoundsException{}
    public void show03() throws IndexOutOfBoundsException{}
    public void show04(){}
}
class Zi extends Fu_lianxi{
    //子类重写父类方法时,抛出和父类相同的异常
    public void show01() throws NullPointerException,ClassCastException{}
    //子类重写父类方法时,抛出父类异常的子类
    public void show02() throws ArrayIndexOutOfBoundsException{}
    //子类重写父类方法时
    public void show03(){}
    //父类没有抛出异常,子类重写父类该方法时,也不能抛出异常;此时子类产生异常,只能捕获,不能声明抛出
    //public void show04(){}
    public void show04(){
        try {
            throw new Exception("编译器异常");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

public class zidingyiException extends RuntimeException{
    public zidingyiException(){
        super();
    }
    public zidingyiException(String message){
        super(message);
    }
}
public class lianxi_03 {
    static String[] usernames={"张三","李四","王五"};
    public static void main(String[] args){//运行期异常
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入注册用户名:");
        String username = sc.next();
        checkUsername(username);
    }
    public static void checkUsername(String username) {//运行期异常处理
        for (String name : usernames) {
            if(name.equals(username)){
                throw new zidingyiException("亲,该用户已经被注册");
            }
        }
        System.out.println("恭喜注册成功");
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/953345.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号