Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interface & Inner classes

Similar presentations


Presentation on theme: "Interface & Inner classes"— Presentation transcript:

1 Interface & Inner classes
Chapter 7 Interface & Inner classes

2 super class Pet :

3 public class Pet { public void speak() { System.out.println("I am a generic pet."); } public class Dog extends Pet { public void speak() { System.out.println("Woof"); public class Cat extends Pet { System.out.println("Meow"); public class Bird extends Pet { System.out.println("Tweedle ");

4 public class TestAnimals {
public static void main(String args[]) { Pet myPets[] = new Pet[4]; myPets[0] = new Pet(); myPets[1] = new Cat(); myPets[2] = new Bird(); myPets[3] = new Dog(); for (int index=0; index<4; index++)  myPets[index].speak(); } Result: Java I am a generic pet. Meow Tweedle Woof Output completed (0 sec consumed) - Normal Termination

5

6 abstract class Pet { public abstract void speak(); } public class Dog extends Pet { public void speak() { System.out.println("Woof"); public class Cat extends Pet { System.out.println("Meow"); public class Bird extends Pet { System.out.println("Tweedle");

7 public class TestAnimals {
public static void main(String args[ ]) { Pet myPets[] = new Pet[4]; myPets[0] = new Bird(); myPets[1] = new Cat(); myPets[2] = new Bird(); myPets[3] = new Dog(); for (int index=0; index<4; index++) myPets[index].speak(); } Result: Java Tweedle Meow Woof Output completed (0 sec consumed) - Normal Termination

8 interface Pet :

9 public abstract void speak(); } public class Dog implements Pet {
interface Pet { public abstract void speak(); } public class Dog implements Pet { public void speak() { System.out.println("Woof"); public class Cat implements Pet { System.out.println("Meow"); public class Bird implements Pet { System.out.println("Tweedle");  [zjm1]格式:右进2  [zjm2]格式:右进4  [zjm3]格式:右进2  [zjm4]格式:右进2  [zjm5]格式:右进2  [zjm6]格式:右进2

10 public class TestAnimals {
public static void main(String args[]) { Pet myPets[] = new Pet[4]; myPets[0] = new Bird(); myPets[1] = new Cat(); myPets[2] = new Bird(); myPets[3] = new Dog(); for (int index=0; index<4; index++) myPets[index].speak(); } Result: Java Tweedle Meow Woof Output completed (0 sec consumed) - Normal Termination

11 contract:契约、合同

12 revealing:展现或显示(某物)的, 显露的

13 Simulating multiple inheritance

14

15 public abstract class Animal {
protected int legs; protected Animal(int legs) { this.legs = legs; } public abstract void eat(); public void walk() { System.out.println("This animal walks on " + legs + " legs.");

16 public interface Pet { public abstract void setName(String name); public abstract String getName(); public abstract void speak(); public abstract void play(); } public class Bird extends Animal implements Pet { private String name; public Bird() { super(2); // 这行是必需的 } public void setName(String name) { this.name = name; public String getName() { return name;

17 public void speak() { System.out.println("speak:Tweedle"); } public void play() { System.out.println("play:Birds fly on the sky all day."); //Bird 覆盖父类Animal 的方法walk() public void walk() { //super.walk(); System.out.println("walk:Birds, of course, can't walk; they fly ."); //Bird 覆盖父类Animal 的抽象方法eat() public void eat() { System.out.println("eat:Birds eat corn ."); //Bird 创建自己的方法buildNest() public void buildNest() { System.out.println("buildNest: Birds buid the nest within the branches."); //Bird 创建自己的方法layEggs() public void layEggs() { System.out.println("layEggs: Birds lay eggs.");

18 public class Cat extends Animal implements Pet {
private String name; public Cat(String name) { super(4); this.name = name; } public Cat() { this(""); public void setName(String name) { public String getName() { return name; public void speak() { System.out.println("speak:Meow"); cpublic void play() { System.out.println("play:"+name + " likes to play with string."); //Cat 覆盖父类Animal 的抽象方法eat() public void eat() { System.out.println("eat:Cats like to eat spiders and mice.");

19 public class Spider extends Animal {
public Spider() { super(8); } //Spider 覆盖父类Animal 的抽象方法eat() public void eat() { System.out.println("eat:Spiders catch flies in their webs to eat.");

20 public class TestAnimals { public static void main(String[] args) {
Bird b = new Bird(); Cat c = new Cat("Fluffy"); Animal ab = new Bird(); Animal as = new Spider(); Pet p = new Cat(); //允许定义接口类型的变量 //示例对接口的不同实现 b.speak(); //调用Bird类对接口Pet的实现 b.play(); //调用Bird类对接口Pet的实现 b.eat(); b.walk(); p.speak(); //通过接口类型的变量p访问Cat实现的接口方法 c.play(); //调用Cat类对接口Pet的实现 c.eat(); c.walk(); as.eat(); as.walk(); ((Bird)ab).speak(); ab.walk(); }  [zjm1]格式:右进3

21 Result: Java speak:Tweedle play:Birds fly on the sky all day. eat:Birds eat corn . walk:Birds, of course, can't walk; they fly . speak:Meow play:Fluffy likes to play with string. eat:Cats like to eat spiders and mice. This animal walks on 4 legs. eat:Spiders catch flies in their webs to eat. This animal walks on 8 legs. Output completed (0 sec consumed) - Normal Termination

22 Many unrelated classes can implement the same interface
A class can implement many unrelated interfaces

23 Multiple Interface Example

24 Multiple Interface Example

25 Multiple Interface Example

26 Multiple Interface Example

27

28

29

30 // OuterNested1.java public class OuterNested1 private int size; /* 声明名为Nested的嵌套类 */ public class Nested { public int doStuff() { // 嵌套类可以访问OuterNested1类的私有成员size变量 return(size++); } public int testTheNested() { Nested i = new Nested(); return(i.doStuff()); // TestOuterNested1.java public class TestOuterNested1 { public static void main(String[] args) { OuterNested1 outer = new OuterNested1(); System.out.println(outer.testTheNested()); Result: 0

31

32 // OuterNested2.java public class OuterNested2 { private int size; public class Nested { public int doStuff() { return(++size); } // TestOuterNested2.java public class TestOuterNested2 { public static void main(String[] args) { OuterNested2 outer = new OuterNested2(); //创建OuterNested2类的嵌套对象必须通过OuterNested2的对象引用 OuterNested2.Nested nested = outer.new Nested(); System.out.println(nested.doStuff()); Reault: 1

33

34 // OuterNested3.java public class OuterNested3 { private int size; public class Nested { public int doStuff(int size) { size++; // 局部变量size this.size++; // 嵌套类Nested的成员变量 OuterNested3.this.size++;// 外部类OuterNested3 的成员变量 return ((size++)+(this.size++)+(OuterNested3.this.size++)); } // TestOuterNested3.java public class TestOuterNested3 { public static void main(String[] args) { OuterNested3 outer = new OuterNested3(); OuterNested3.Nested nested = outer.new Nested(); System.out.println(nested.doStuff(1));

35

36

37

38

39


Download ppt "Interface & Inner classes"

Similar presentations


Ads by Google