疯狂JAVA讲义---第六章(中):面向对象二

这本书对java的每个知识点分析的比较细,虽说不用知道所有的知识点你也可以写代码,但写代码总是仿照他人的标准格式却不知道为什么。这样会使人觉得写代码是一种负担,还要背出那么多的语法,一旦你掌握了细节,写代码时你会觉得那些语法自然而然的就出来了,不用死记。

这章中间讲了抽象类和接口,抽象方法没有方法体,所以不用加花括号,接口的方法也一样,具体作用我在前几天写的blog《面向对象的重要概念》中已经讲的非常清楚了,这里就补充下书中的例子,eg(抽象类)

  1. public abstract class SpeedMeter
  2. {
  3.     //转速
  4.     private double turnRate;
  5.     public SpeedMeter()
  6.     {
  7.     }
  8.     //把返回车轮半径的方法定义成抽象方法
  9.     public abstract double getRadius();
  10.     public void setTurnRate(double turnRate)
  11.     {
  12.         this.turnRate = turnRate;
  13.     }
  14.     //定义计算速度的通用算法
  15.     public double getSpeed()
  16.     {
  17.         //速度等于 车轮半径 * 2 * PI * 转速
  18.         return java.lang.Math.PI * 2 * getRadius() * turnRate;
  19.     }
  20. }
  21. public class CarSpeedMeter extends SpeedMeter
  22. {
  23.     public double getRadius()
  24.     {
  25.         return 0.28;
  26.     }
  27.     public static void main(String[] args) 
  28.     {
  29.         CarSpeedMeter csm = new CarSpeedMeter();
  30.         csm.setTurnRate(15);
  31.         System.out.println(csm.getSpeed());
  32.     }
  33. }

eg(接口)

  1. interface interfaceA
  2. {
  3.     int PROP_A = 5;
  4.     void testA();
  5. }
  6. interface interfaceB
  7. {
  8.     int PROP_B = 6;
  9.     void testB();
  10. }
  11. interface interfaceC extends interfaceA, interfaceB
  12. {
  13.     int PROP_C = 7;
  14.     void testC();
  15. }
  16. public class TestInterfaceExtends 
  17. {
  18.     public static void main(String[] args)
  19.     {
  20.         System.out.println(interfaceC.PROP_A);
  21.         System.out.println(interfaceC.PROP_B);
  22.         System.out.println(interfaceC.PROP_C);
  23.     }
  24. }

抽象类是一种模板式的设计,接口是一种规范。

面向接口编程实例,eg(工厂模式)

  1. public class Computer
  2. {
  3.     private Output out;
  4.     public Computer(Output out)
  5.     {
  6.         this.out = out;
  7.     }
  8.     //定义一个模拟获取字符串输入的方法
  9.     public void keyIn(String msg)
  10.     {
  11.         out.getData(msg);
  12.     }
  13.     //定义一个模拟打印的方法
  14.     public void print()
  15.     {
  16.         out.out();
  17.     }
  18. }
  19. public class OutputFactory
  20. {
  21.     public Output getOutput()
  22.     {
  23.         //下面两行代码用于控制系统到底使用Output的哪个实现类。
  24.         //return new Printer();
  25.         return new BetterPrinter();
  26.     }
  27.     public static void main(String[] args) 
  28.     {
  29.         OutputFactory of = new OutputFactory();
  30.         Computer c = new Computer(of.getOutput());
  31.         c.keyIn("轻量级J2EE企业应用实战");
  32.         c.keyIn("Struts2权威指南");
  33.         c.print();
  34.     }
  35. }
  36. public class BetterPrinter implements Output
  37. {
  38.     private String[] printData = new String[MAX_CACHE_LINE * 2];
  39.     //用以记录当前需打印的作业数
  40.     private int dataNum = 0;
  41.     public void out()
  42.     {
  43.         //只要还有作业,继续打印
  44.         while(dataNum > 0)
  45.         {
  46.             System.out.println("高速打印机正在打印:" + printData[0]);
  47.             //把作业队列整体前移一位,并将剩下的作业数减1
  48.             System.arraycopy(printData , 1, printData, 0, --dataNum);
  49.         }
  50.     }
  51.     public void getData(String msg)
  52.     {
  53.         if (dataNum >= MAX_CACHE_LINE * 2)
  54.         {
  55.             System.out.println("输出队列已满,添加失败");
  56.         }
  57.         else
  58.         {
  59.             //把打印数据添加到队列里,已保存数据的数量加1。
  60.             printData[dataNum++] = msg;
  61.         }
  62.     }
  63. }

又学到一招,当内部类和外部类的属性或方法同名时可以用以下例子中的,外部类名.this来引用。eg,

  1. public class DiscernVariable
  2. {
  3.     private String prop = "外部类属性";
  4.     private class InClass
  5.     {
  6.         private String prop = "内部类属性";
  7.         public void info()
  8.         {
  9.             String prop = "局部变量";
  10.             //通过 外部类类名.this.varName 访问外部类实例属性
  11.             System.out.println("外部类的属性值:" + DiscernVariable.this.prop);
  12.             //通过 this.varName 访问外内部类实例的属性
  13.             System.out.println("内部类的属性值:" + this.prop);
  14.             //直接访问局部变量
  15.             System.out.println("局部变量的属性值:" + prop);
  16.         }
  17.     }
  18.     public void test()
  19.     {
  20.         InClass in = new InClass();
  21.         in.info();
  22.     }
  23.     public static void main(String[] args) 
  24.     {
  25.         new DiscernVariable().test();
  26.     }
  27. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值