1. interface Frob{
    2. Object display();
    3. }
    4. class Inform implements Frob{
    5. public Frob display() {
    6. System.out.print("通知:");
    7. return null;
    8. }
    9. }
    10. class Car implements Frob{
    11. public Frob display() {
    12. System.out.print("汽车油量:");
    13. return null;
    14. }
    15. }
    16. class Ad implements Frob{
    17. public Frob display() {
    18. System.out.print("广告:");
    19. return null;
    20. }
    21. }
    22. public class test8_1 {
    23. public static void main(String[] args) {
    24. Inform T=new Inform();
    25. T.display();
    26. }
    27. }
    1. interface shape{
    2. float size();
    3. }
    4. class rectangle implements shape{
    5. int length;
    6. int wide;
    7. public rectangle(int x,int y){
    8. length=x;
    9. wide=y;
    10. }
    11. public float size() {
    12. return length*wide;
    13. }
    14. }
    15. class circle implements shape{
    16. int r;//半径
    17. public circle(int x){
    18. r=x;
    19. }
    20. public float size() {
    21. return (float) (3.14*r*r);
    22. }
    23. }
    24. class column implements shape{
    25. int r;//半径
    26. int hight;//高
    27. public column(int x,int y){
    28. r=x;
    29. hight =y;
    30. }
    31. public float size() {
    32. return (float) ((float) (2*3.14*r*r)+(2*3.14*r*hight));
    33. }
    34. }
    35. public class test8_2 {
    36. public static void main(String[] args) {
    37. shape[] S=new shape[3];
    38. S[0]=new rectangle(1,5);
    39. S[1]=new circle(5);
    40. S[2]=new column(5,2);
    41. for(int i=0;i<3;i++)
    42. System.out.println(S[i].size());
    43. }
    44. }
    1. abstract class fruit{
    2. abstract double getWeight();
    3. }
    4. class Apple extends fruit{
    5. double W;
    6. public Apple(int w){
    7. W=w;
    8. }
    9. public double getWeight() {
    10. return W;
    11. }
    12. }
    13. class Peach extends fruit{
    14. double W;
    15. public Peach(int w){
    16. W=w;
    17. }
    18. public double getWeight() {
    19. return W;
    20. }
    21. }
    22. class Orange extends fruit{
    23. double W;
    24. public Orange(int w){
    25. W=w;
    26. }
    27. public double getWeight() {
    28. return W;
    29. }
    30. }
    31. public class test8_3 {
    32. public static void main(String[] args) {
    33. fruit[] f=new fruit[3];
    34. f[0]=new Apple(100);
    35. f[1]=new Peach(200);
    36. f[2]=new Orange(300);
    37. for(int i=0;i<3;i++)
    38. System.out.println(f[i].getClass().getSimpleName()+":"+f[i].getWeight()+"KG");
    39. }
    40. }