390 likes | 482 Views
Chapter 7. Interface & Inner classes. super class ----- Pet :. 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"); } }
E N D
Chapter 7 Interface & Inner classes
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 { public void speak() { System.out.println("Meow"); } } public class Bird extends Pet { public void speak() { System.out.println("Tweedle "); } }
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
abstract class Pet { • public abstract void speak(); • } • public class Dog extends Pet { • public void speak() { • System.out.println("Woof"); • } • } • public class Cat extends Pet { • public void speak() { • System.out.println("Meow"); • } • } • public class Bird extends Pet { • public void speak() { • System.out.println("Tweedle"); • } • }
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 Tweedle Woof Output completed (0 sec consumed) - Normal Termination
interface Pet { public abstract void speak(); } public class Dog implements Pet { public void speak() { System.out.println("Woof"); } } public class Cat implements Pet { public void speak() { System.out.println("Meow"); } } public class Bird implements Pet { public void speak() { System.out.println("Tweedle"); } } [zjm1]格式:右进2 [zjm2]格式:右进4 [zjm3]格式:右进2 [zjm4]格式:右进2 [zjm5]格式:右进2 [zjm6]格式:右进2
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 Tweedle Woof Output completed (0 sec consumed) - Normal Termination
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."); } }
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; }
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."); } }
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) { this.name = 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."); } }
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."); } }
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
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. speak:Tweedle walk:Birds, of course, can't walk; they fly . Output completed (0 sec consumed) - Normal Termination
Many unrelated classes can implement the same interface A class can implement many unrelated interfaces
// 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
// 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
// OuterNested3.java public class OuterNested3 { private int size; public class Nested { private int size; 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)); } }