1对于抛出异常的时候
调用者在调用一个声明异常的方法时,必须要对异常进行处理,否则编译无法通过。 调用构造方法
-
代码二:
class Animal {
public Animal(String name){
System.out.println(“我是一只”+name);
}}
Class Dog extends Animal {
public Dog(){}
}
public class Test02{
public static void main(String[] args){
Dog dog = new Dog();
}} -
分析:_不能编译通过,出错的原因的是在子类的构造方法中,一定会调用父类的某个构造方法。这时可以在子类的构造方法中通过super指定调用父类的哪个构造方法,如果没有指定,在实例化子类对象时,会自动调用父类无参的构造方法
对HashMap集合里面 的,对键值的获取,并遍历,得 到get(key)得到value值 ************************************************ *************************
import.java.util.*;
public class Demo02{
public static void main(String[] args){
HashMap<String,String>map = new HashMap<String,String>();
map.put(“1”,”Jack”);
map.put(“2”,”Rose”);
map.put(“3”,”Lucy”);
SetkeySet = ③______________map.keySet()__;//获取键的集合
Iterator it = keySet.iterator();
while(④_____________it.hasNext()){//判断集合中是否存在下一个元素
String key = ⑤_______________it.next() ________________;//获取元素
String value=map.get(key);
System.out.println(key+”;”+value);
}
}
}next和nextInt方法
import java.util.Scanner;
public class Main {
/*
* nextLine()方法返回的是Enter键之前的所有字符,它是可以得到带空格的字符串的。
next()会自动消去有效字符前的空格,只返回输入的字符,不能得到带空格的字符串。
(简单点说,next我只要字,nextLine我啥都要)
*/
public static void main(String[] args) {
String s1,s2;
Scanner sc=new Scanner(System.in);
System.out.print("请输入第一个字符串:");
s1=sc.nextLine();
System.out.println("输入的字符串是:"+s1);
System.out.print("请输入第2个字符串:");
s1=sc.nextLine();
System.out.println("输入的字符串是:"+s1);
System.out.print("请输入第3个字符串:");
s2=sc.next();
System.out.println("*******************就是next读取不了,字符串里面的空格,让下一个输入读取了,");
System.out.println("输入的字符串是:"+s2);
System.out.print("请输入第3个字符串:");
s2=sc.next();
System.out.println("字符串里面的空格,过滤了,");
System.out.println("输入的字符串是:"+s2);
System.out.print("请输入第4个字符串:他得到回车,回车前面么有字符,就什么也,么有");
s1=sc.nextLine();
System.out.println("输入的字符串是#"+s1);
}
}
nextLine是以enter结束,next以空格,tab enter结束
如何next中间有空格,需要用nextLine接受空格后面的字符,下面的字符串才能正常输入
在next下面不用用nextInt方法