package com.pb.demo.demo1;public class Demo { public static void main(String[] args) { try { func(); System.out.); } catch (Exception e) { //e.printStackTrace(); System.out.println("C"); } System.out.println("D"); } /** * 拋出異常 * @throws Exception */ public static void func() throws Exception{ try { throw new Exception(); } finally { System.out.println("B"); } }}
結果:B,C,D
二、對象的實例化過程
package com.pb.demo.demo2;public class Test { public Test(){ System.out.println("Test"); } }package com.pb.demo.demo2;/** * 繼承父類test * @author Administrator * */public class Demo extends Test { public Demo(){ //這里隱藏了super() System.out.println("Demo"); } public static void main(String[] args) { new Demo(); new Test(); }}
結果:
Test
Demo
Test
package com.pb.demo.demo2;public interface A {}package com.pb.demo.demo2;public class B implements A { public String func(){ return "func"; } public static void main(String[] args) { A a=new B(); a.func(); //編譯失敗,因為A接口中并沒有這個方法}}
package com.pb.demo.demo3;public class Fu { public boolean show(char a){ System.out.println(a); return true; }}package com.pb.demo.demo3;public class Demo extends Fu { /** * 重寫父類的方法 * 調用時會調用子類重寫后的方法 */ public boolean show(char a){ System.out.println(a); return false; } public static void main(String[] args) { int i=0; Fu f=new Demo(); Demo d=new Demo(); for(f.show('A');f.show('B')&&(i<2);f.show('C')){ i++; d.show('D'); } //結果:A,B, }}
結果;A,B
package com.pb.demo.demo4;public interface A {}package com.pb.demo.demo4;public class B implements A { public String test(){ return "yes"; }}package com.pb.demo.demo4;public class Demo { static A get(){ return new B(); } public static void main(String[] args) { A a=Demo.get(); System.out.println(a.test()); //編譯失敗 接口中沒有這個方法,這個方法是子類中特有的 }}
package com.pb.demo.demo5;public class Super { int i=0; public Super(String a){ System.out.println("A"); i=1; } public Super(){ System.out.println("B"); i+=2; }}package com.pb.demo.demo5;public class Demo extends Super { public Demo(String a){ System.out.println("C"); i=5; } public static void main(String[] args) { int i=4; Super d=new Demo("A"); System.out.println(d.i); } //B,C,5 }
結果:B,C,5
package com.pb.demo.demo6;public interface Inter { public void show(int a,int b); public void func();}package com.pb.demo.demo6;public class Demo { public static void main(String[] args) { // 補足代碼,調用兩個函數,用匿名內部類實現 Inter inter = new Inter() { @Override public void show(int a, int b) { } @Override public void func() { } }; inter.show(3, 5); inter.func(); }}
package com.pb.demo.demo7;public class TD { int y = 6; class Inner { static final int y = 3; // 內部類中如果有static成員,這個內部類必須是static的, void show() { System.out.println(y); //只能調用局部中final修飾的 } }}package com.pb.demo.demo7;public class TC { public static void main(String[] args) { TD.Inner ti=new TD().new Inner(); ti.show(); //3 }}
非靜態內部類,不能定義靜態成員,
內部類如果有靜態成員,則這個內部類必須是靜態內部類
public class Demo { int show(int a,int b){ return 0; } }/** * 下列哪里方法可以存在Demo的子類中 */ public int show(int a,int b){ return 0; } //可以方法重寫 private int show(int a,int b){ return 0; } //權限不夠 private int show(int a,long b){ return 0; } //可以,和父類不是同一個方法,方法重載 public short show(int a,int b){ return 0; }//不可以,不是方法重載,不可以和給定的函數出現在同一類中,或者其子類 public static int show(int a,int b){ return 0; } //不可以,靜態方法,只能覆蓋靜態
this:代表本類對象,哪個對象調用this所有的函數,this就代表哪個對象
final:
1.修飾類,變量(成員變量,靜態變量,局部變量),函數
2.修飾的類不可以被繼承
3.修飾的函數不可以被重寫
4.修飾的變量是一個常量,只能賦值一次
5.內部類只能訪問局部中的final變量
package com.pb.demo.demo8;public class Fu { int num=4; void show(){ System.out.println("show Fu"); }}package com.pb.demo.demo8;public class Zi extends Fu { int num=5; void show(){ System.out.println("show Zi"); }}package com.pb.demo.demo8;public class T { public static void main(String[] args) { Fu f=new Zi(); Zi z=new Zi(); System.out.println(f.num);//4 System.out.println(z.num);//5 f.show(); //方法已經重寫,執行子類的方法 z.show();}}
結果:
4
5
show Zi
show Zi
package com.pb.demo.demo8;public interface A { void show();}package com.pb.demo.demo8;public interface B { void add(int a,int b);}package com.pb.demo.demo7;import com.pb.demo.demo8.A;import com.pb.demo.demo8.B;public class C implements A,B{ private int x; private int y; // private int sum; @Override public void add(int a, int b) { this.x=a; this.y=b; //sum=a+b; } @Override public void show() { System.out.println(x+y); //System.out.println(sum); } public static void main(String[] args) { C c=new C(); c.add(4, 2); c.show();//通過函數打印以上2個數的和 }}
package com.pb.demo.demo8;public class Demo { public static void main(String[] args) { try { showExce(); System.out.println("A"); } catch (Exception e) { System.out.println("B"); }finally{ System.out.println("C"); } System.out.println("D"); //B,C,D } public static void showExce() throws Exception{ throw new Exception(); }}
結果:B,C,D
package com.pb.demo.demo8;public class Super { int i=0; Super(){ }; public Super(String s){ i=1; } }package com.pb.demo.demo8;public class Demo1 extends Super { public Demo1(String s){ i=2; } public static void main(String[] args) { Demo1 d=new Demo1("yes"); System.out.println(d.i); //2 }}
結果:2
package com.pb.demo.demo9;public class Super { public int get(){ return 4; }}package com.pb.demo.demo9;public class Demo1 extends Super { public long get(){ //方法重寫不能改變返回值類型和參數 return 4; } public static void main(String[] args) { Super s=new Demo1(); System.out.println(s.get()); }}
結果:編譯失敗:方法重寫不能改變方法的返回值 和參數
package com.day10.demo2;public class Demo { public static void main(String[] args) { try { func(); } catch (Exception e) { System.out.println("C"); } System.out.println("D"); } public static void func(){ try { throw new Exception(); System.out.println("A");//拋出異常下方代碼不會被執行 } catch (Exception e) { System.out.println("B"); } }}
結果:throw下方的代碼不能被執行到
刪除后,執行結果:B,D
package com.day10.demo2;public class Demo1 { public void func(){ //位置1: } public static void main(String[] args) { Demo d=new Demo(); //位置2 } /** * A、在位置1寫new Inner(); //OK * B、在位置2寫new Inner(); //主函數是靜態的,如果要訪問,需要被靜態包修 * C、在位置2寫new d.Inner();//格式錯誤 new new Demo1().Inner(); * D、在位置2寫new Demo.Inner(); //錯誤因為inner不是靜態的 */}
結果
package com.day10.demo2;public class Exc0 extends Exception {}package com.day10.demo2;public class Demo2 { public static void main(String[] args) { try { throw new Exc1(); } catch (Exception e) { System.out.println("Exception"); /* * Exception是最大的異常父類, * 在最前面的后,下面的catch永遠不會被執行到 */ }catch (Exc0 e) { System.out.println("Exc0"); } }}
結果:在最前面的后,下面的catch永遠不會被執行到,父類的catch要放在最下面
package com.day10.demo2;public interface Test { public void func();}
package com.day10.demo2;public class Demo3 { public void show(Test t){ t.func(); } public static void main(String[] args) { //補足代碼:匿名內部類 new Demo3().show(new Test(){ @Override public void func() { } }); }}
package com.day10.demo3;public class Test { public static String output=""; public static void foo(int i){ try { if(i==1) throw new Exception(); output+="1"; } catch (Exception e) { output+="2"; return; }finally{ output+="3"; } output+="4"; } public static void main(String[] args) { foo(0); System.out.println(output);//1,3,4 foo(1); System.out.println(output); //134,2,3 }}
結果
package com.day10.demo3;public class Circle { private static double pi=3.14; private double radius;//半徑 public Circle(double r){ this.radius=r; } public static double compare(Circle[] cir){ //程序代碼,就是求數組中的最大值 int max=0; for(int x=1;x<cir.length;x++){ if(cir[x].getRadius()>cir[max].radius){ max=x; } } return cir[max].getRadius(); } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; }}package com.day10.demo3;public class TC { public static void main(String[] args) { Circle cir[] =new Circle[3];//創建了個類類型的數組 cir[0]=new Circle(2.0); cir[1]=new Circle(3.0); cir[2]=new Circle(5.0); System.out.println("最大半徑值是:"+Circle.compare(cir)); }}
package com.day10.demo3;public class Demo { private static int j = 0; private static boolean methodB(int k) { j += k; return true; } public static void methodA(int i) { boolean b; b = i < 10 | methodB(4); b = i < 10 ||methodB(8); } public static void main(String[] args) { methodA(0); System.out.println(j); }}
結果:4
package com.day10.demo4;public class Circle { private double radius; public Circle(double r){ this.radius=r; } public Circle compare(Circle cir){ //補足代碼 if (this.radius>cir.getRadius()){ return this; }else{ return cir; } //return (this.radius>cir.getRadius()?this:cir); } public double getRadius() { return radius; } public void setRadius(double radius) { this.radius = radius; } }package com.day10.demo4;public class TC { public static void main(String[] args) { Circle cir1=new Circle(1.0); Circle cir2=new Circle(2.0); Circle cir; cir=cir1.compare(cir2); if(cir1==cir){ System.out.println("圓1的半徑比較大"); }else{ System.out.println("圓2的半徑比較大"); } }}
新聞熱點
疑難解答