修改以后的代码如下,解释在注释里面
class Class2 {private int y
private void show() {
// System首字母大写,""用英文的
System.out.println("show method is private")
}
public void usesecret() {
show()
}
}
public class Class1 {
public static void main(String args[]) {
// 创建Class2的对象才能调用Class2里的方法
Class2 Ob = new Class2()
Ob.usesecret()
// private访问权限仅野桐槐限于类的内部,所以此处轮信访问不了
// 颂友Ob.show()
}
}
运行结果:
package com.testimport java.io.IOExceptionpublic class ThrowDemo { public static void methodDemo(int i) throws IOException,ClassNotFoundException{ if(i>0) System.out.println("你输入的参数是正数")if(i<0) System.out.println("你者晌输入的参数是负数"首枯锋)} public static void main(String args[]){ if(args[0].length()<1){ System.out.println("请以败肢命令行参数形式输入1个整数")return} int i=Integer.parseInt(args[0])try {methodDemo(i)
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace()
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace()
} } }
catch要从小到大,棚升要先catch Exception2才行念森,因为Exception2继承链高老Excption1class Exception1 extends Exception {}
class Exception2 extends Exception1 {}
public class Test {
public static void main(String[] args) {
try {
throw new Exception2()
} catch(Exception2 a) {
System.out.println("Caught Exception2 ")
}catch(Exception1 s) {
System.out.println("Caught Exception1")
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)