Вы находитесь на странице: 1из 7

01.

abstract class SuperTest {


02. public abstract void aMethod();
03. public void anotherMethod(String s) {
04. System.out.println(s);
05. }
06.
07. public static void main(String[] args) {
08. SuperTest s = new Test();
09. s.aMethod();
10. }
11. }
12.
13. public class Test extends SuperTest {
14. public void aMethod() {
15. System.out.println("Sub");
16. }
17. }
01. class A {
02. String as = name();
03. String name() {
04. return "A";
05. }
06. }
07.
08. class B extends A {
09. String as = super.as;
10. String name() {
11. return "B";
12. }
13. }
14.
15. public class Test {
16. public static void main(String[] args) {
17. A a = new B();
18. System.out.println(a.as);
19. }
20. }
public class Test {
02. public static void operate(int[] arr, int x, int y) {
03. x = x+++y;
04. y += (y = 1);
05. arr[y] = x;
06. for(int i = 0; i < arr.length; i++) {
07. System.out.println(arr[i]);
08. }
09. }
10.
11. public static void main(String[] args) {
12. short a = 1;
13. byte b = 2;
14. Test.operate(new int[]{a, ++a, ++a, ++a}, b++, --b);
15. }
16. }

01. class BadObjectException extends Exception {}


02. class BadIndexException extends IndexOutOfBoundsException {}
03.
04. public class Test {
05.
06. public void doSomething() {}
07.
08. public void aMethod() {
09. try {
10. doSomething();
11. }catch(BadIndexException be) {
12. System.out.println("BadIndexException");
13. throw new BadIndexException(){};
14. }
15. }
16.
17. public void anotherMethod() throws BadObjectException{
18. try {
19. doSomething();
20. }catch(BadObjectException be) {
21. System.out.println("BadObjectException");
22. }
23. }
24.
25. public static void main(String[] args) throws BadObjectException{
26. Test t = new Test();
27. t.aMethod();
28. t.anotherMethod();
29. }
30. }

01. import Outer.Inner.InnerMost;


02. import Outer.*;
03.
04. class Outer {
05. int i;
06. class Inner {
07. Inner() {i++;}
08.
09. class InnerMost extends Inner {
10. InnerMost() {i++; System.out.println(i);}
11. }
12. }
13. interface InnerInterface {
14. void doSomething();
15. }
16. }
17.
18. public class Test implements InnerInterface{
19.
20. public void doSomething() {
21. System.out.println("done");
22. }
23.
24. public static void main(String[] args) {
25. Inner im = new Outer().new Inner().new InnerMost();
26. }
27. }

01. class Super {


02. Super() {
03. System.out.print("Super ");
04. }
05. }
06.
07. class Outer extends Super {
08. Outer() {
09. System.out.println("Outer ");
10. }
11. class Inner {
12. Inner() {
13. System.out.println("Inner ");
14. }
15. }
16. }
17.
18. public class SubInner extends Outer.Inner {
19. SubInner() {
20. System.out.println("SubInner ");
21. }
22.
23. public static void main(String[] args) {
24. SubInner sub = new SubInner();
25. }
26. }

1. class BadTasteException extends Exception {}


02.
03. public class Fruit {
04. private String name;
05. private boolean tastesGood;
06.
07. {
08. if(! tastesGood) throw new BadTasteException();
09. }
10.
11. public Fruit(String name, boolean isTasty) throws BadTasteException{
12. this.name = name;
13. tastesGood = isTasty;
14. }
15.
16. public void consume() {
17. this = null;
18. }
19.
20. public static void main(String[] args) {
21. Fruit pear = new Fruit("Pear", true);
22. pear.consume();
23. }
24. }
01. class Ticker extends Thread {
02. public synchronized void run() {
03. try {
04. interrupt();
05. this.sleep(500);
06. }catch(InterruptedException e) {
07. System.out.println(interrupted());
08. }
09. }
10. }
11.
12. public class Test {
13. public static void main(String[] args) {
14. new Ticker().start();
15. }
16. }

01. class Super { }


02. class Sub extends Super { }
03.
04. public class Test {
05. public void foo(Super sup) {
06. System.out.println("Super");
07. }
08.
09. public void foo(Sub sub) {
10. System.out.println("Sub");
11. }
12.
13. public static void main(String[] args) {
14. new Test().foo(null);
15. }
16. }
01. public class Test {
02. public static void main(String[] args) {
03. int[] a = new int[]{1, 2, 3, 4};
04. int b = 1;
05. b = b+++b;
06. a[b++] = b += 0;
07. for(int i = 0; i < a.length; i++) {
08. System.out.println(a[i]);
09. }
10. }
11. }

01. class A {
02. void f() {
03. System.out.print("A.f() called");
04. }
05. }
06.
07. class B extends A {
08. void f() {
09. System.out.println("B.f() called");
10. }
11. }
12.
13. public class Test {
14. static void getType(A a) {
15. System.out.print("Type A ");
16. }
17.
18. static void getType(B b) {
19. System.out.print("Type B ");
20. }
21.
22. public static void main(String[] args) {
23. A a = new B();
24. getType(a);
25. a.f();
26. }
27. }

01. class Super {


02. int a = f();
03. int f() {
04. return 1;
05. }
06. }
07.
08. class Sub extends Super{
09. int b = 2;
10. int f() {
11. return b;
12. }
13. }
14.
15. public class Test {
16. public static void main(String[] args) {
17. Super sup = new Sub();
18. System.out.println(sup.a);
19. System.out.println(sup.f());
20. }
21. }

01. class A {
02. String s = "A";
03. public String getName() {
04. return s;
05. }
06. }
07.
08. class B extends A {
09. String s = "B";
10. public String getName() {
11. return s;
12. }
13. }
14.
15. public class Test extends B {
16. String s = "C";
17. public static void main(String[] args) {
18. A a = new Test();
19. System.out.println(a.getName());
20. }
21. }

01. interface Edible {


02. public double getCalories();
03. public boolean shouldBeCooked();
04. }
05.
06. class RawVeg {
07. protected int calories;
08. protected boolean cooked;
09.
10. public double getCalories() {
11. return calories;
12. }
13.
14. protected boolean shouldBeCooked() {
15. return !cooked;
16. }
17. }
18.
19. class CookedVeg extends RawVeg implements Edible {
20.
21. public boolean shouldBeCooked() {
22. return cooked;
23. }
24.
25. public static void main(String[] args) {
26. final RawVeg veg = new CookedVeg();
27. Edible food = (Edible) veg;
28. System.out.println(food.getCalories());
29. }
30. }
01. public class Test {
02. public static void main(String[] args) {
03. int i = 0;
04. outer:
05. while(true) {
06. i++;
07. inner:
08. for(int j = i; j < 10; j++) {
09. i += j;
10. if(j == 3) continue inner;
11. break outer;
12. }
13. }
14. System.out.println("i is " + i + " and j is " + j);
15. }
16. }
01. public class Test {
02. public static void main(String[] args) {
03. int n = 6;
04. n <<= 2;
05. n = n & n + 1 | n + 2 ^ n + 3;
06. n >>= 2;
07. System.out.println(n);
08. }
09. }

01. public class Test {


02. public static void main(String[] args) {
03. int i = 0;
04. int j = 1;
05. outer:
06. for(; i < j; i++) {
07. inner:
08. do {
09. ++j;
10. if( i++ % 3 == 2) break inner;
11. else if( j++ % 3 == 1) break outer;
12. System.out.print(i*j);
13. }while( j < i);
14. }
15. System.out.println(i + "" + j);
16. }
17. }

01. class Outer {


02. class Inner {
03. public void doSomething(){
04. System.out.println("Inner");
05. }
06. }
07.
08. public static void doSomething() {
09. System.out.println("Outer");
10. Inner inner = Outer.this.new Inner();
11. inner.doSomething();
12. }
13. }
14.
15. public class Test {
16. public static void main(String[] args) {
17. Outer outer = null;
18. outer.doSomething();
19. }
20. }

Вам также может понравиться