今天闲着就想起学学java,还不错,挺顺畅的,主要是多态之类的。
E1:
what.java
import ok.*; class abc extends ok{ public abc() { System.out.print("abc() called;\n"); } public void x() { System.out.print("abc.x() called;\n"); } public void x2() { System.out.print("abc."); super.x(); } } class abc2 extends ok{ private int x=0; private int y=0; public abc2() { System.out.print("abc2() called;\n"); } public void x() { System.out.print("abc2.x() called;\n"); } public void x2() { System.out.print("abc2."); super.x(); } } public class what{ public static void main(String args[]) { try{ ok[] y={new abc2(),new abc(),new abc2()}; for(ok e : y){ e.x(); try{ ((abc)e).x2(); } catch(Exception x){ ((abc2)e).x2(); //x.printStackTrace(System.out); //throw x; } } }catch(Exception x){ System.out.print("The upper Deal!\n"); x.printStackTrace(System.out); } } }./ok/ok.java
package ok; abstract public class ok{ protected int ac=1; protected int bc=1; public ok() { System.out.print("ok() called;\n"); } /*public ok(int abc,int ca) { this.ac=abc; this.bc=ca; System.out.println(ac); System.out.println(bc); }*/ public void giveAnswer(){} public void x(){ System.out.print("super of x() called;\n"); } }output:
ok() called;
abc2() called;
ok() called;
abc() called;
ok() called;
abc2() called;
abc2.x() called;
abc2.super of x() called;
abc.x() called;
abc.super of x() called;
abc2.x() called;
abc2.super of x() called;
E2:
imp.java
interface Inte{ void ok(); } class Abc implements Inte{ public void ok(){ System.out.println("Abc.ok() called!"); } } class Problem implements Inte{ public void ok(){ System.out.println("Problem.ok() called!"); } } public class imp{ public static void main(String args[]){ Inte[] temp={new Abc(),new Problem()}; for(Inte e : temp){ e.ok(); } } }output:
Abc.ok() called!
Problem.ok() called!
java I
最新推荐文章于 2025-12-03 20:20:13 发布
3170

被折叠的 条评论
为什么被折叠?



