案例一:
BookStore:
package BookStore;
public class BookStore {
private String name;
public void bookType(){
}
}
XHBookStore:
package BookStore;
public class XHBookStore extends BookStore {
@Override
public void bookType() {
System.out.println("售卖中小学生读物");
}
}
DYBookStore:
package BookStore;
public class DYBookStore extends BookStore {
@Override
public void bookType() {
System.out.println("售卖中学生杂志读物");
}
}
Student:
package BookStore;
public class Student {
public void buy(BookStore book){
book.bookType();
}
}
Test:
package BookStore;
import org.junit.Test;
public class Jtest {
@Test
public void test(){
BookStore bk1=new XHBookStore();
BookStore bk2=new DYBookStore();
Student stu1=new Student();
stu1.buy(bk1);
Student stu2=new Student();
stu2.buy(bk2);
}
}
案例二:
Animal :
package LadysPet;
public class Animal {
public String name;
public Animal(String name) {
this.name = name;
}
public void enjoy(){
System.out.println("dongwudeenjoyways");
}
}
Cat:
package LadysPet;
public class Cat extends Animal {
public String eyecolor;
public Cat(String name, String eyecolor) {
super(name);
this.eyecolor = eyecolor;
}
@Override
public void enjoy() {
System.out.println("喵喵。。。");
}
@Override
public String toString() {
return "我是一只猫,"+this.name+"是我的名字,我眼睛的颜色是"+this.eyecolor;
}
}
Dog:
package LadysPet;
public class Dog extends Animal {
public String furcolor;
public Dog(String name, String furcolor) {
super(name);
this.furcolor = furcolor;
}
@Override
public void enjoy() {
System.out.println("汪汪汪。。。");
}
@Override
public String toString() {
return "我是一只狗,"+this.name+"是我的名字,我的毛色是"+this.furcolor;
}
}
Lady:
package LadysPet;
public class Lady {
private String name;
///private Animal pet;
public Lady(String name) {
this.name = name;
//this.pet = pet;
}
public void enjoy233(Animal pet){
pet.enjoy();
}
}
Test:
package LadysPet;
import org.junit.Test;
public class Jtest {
@Test
public void test(){
Dog d=new Dog("金毛", "yellow");
Cat c=new Cat("mimi", "black");
Lady mary=new Lady("Maty");
Lady lucy=new Lady("lucy");
mary.enjoy233(d);
lucy.enjoy233(c);
}
}