佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1832|回复: 6

Java学习笔记高级篇01

[复制链接]
发表于 31-8-2012 01:54 PM | 显示全部楼层 |阅读模式
学习Java三个月,开贴与同样学习著Java的朋友讨论。我安装了三个IDE,NetBeans,Eclipse,JCreator。

以下是个OOP的范列:
  1. // Fig. 10.4: Employee.java
  2. // Employee abstract superclass.
  3. public abstract class Employee implements Payable
  4. {
  5. private String firstName;
  6. private String lastName;
  7. private String socialSecurityNumber;

  8. // three-argument constructor
  9. public Employee( String first, String last, String ssn )
  10. {
  11. firstName = first;
  12. lastName = last;
  13. socialSecurityNumber = ssn;
  14. } // end three-argument Employee constructor

  15.   // set first name
  16. public void setFirstName( String first )
  17. {
  18. firstName = first; // should validate
  19. } // end method setFirstName

  20. // return first name
  21. public String getFirstName()
  22. {
  23. return firstName;
  24. } // end method getFirstName

  25. // set last name
  26. public void setLastName( String last )
  27. {
  28. lastName = last; // should validate
  29. } // end method setLastName

  30. // return last name
  31. public String getLastName()
  32. {
  33. return lastName;
  34. } // end method getLastName

  35. // set social security number
  36. public void setSocialSecurityNumber( String ssn )
  37. {
  38. socialSecurityNumber = ssn; // should validate
  39. } // end method setSocialSecurityNumber

  40. // return social security number
  41. public String getSocialSecurityNumber()
  42. {
  43. return socialSecurityNumber;
  44. } // end method getSocialSecurityNumber

  45. // return String representation of Employee object
  46. @Override
  47. public String toString()
  48. {
  49. return String.format( "%s %s\nsocial security number: %s",
  50. getFirstName(), getLastName(), getSocialSecurityNumber() );
  51. } // end method toString

  52. public abstract double earnings();
  53. }



  54. // Fig. 10.8: BasePlusCommissionEmployee.java
  55. // BasePlusCommissionEmployee class extends CommissionEmployee.

  56. public class BasePlusCommissionEmployee extends CommissionEmployee
  57. {

  58. private double baseSalary; // base salary per week

  59. //six-argument constructor
  60. public BasePlusCommissionEmployee( String first, String last,
  61. String ssn, double sales, double rate, double salary )
  62. {
  63. super( first, last, ssn, sales, rate );
  64. setBaseSalary( salary ); // validate and store base salary
  65. } // end six-argument BasePlusCommissionEmployee constructor

  66. // set base salary
  67. public void setBaseSalary( double salary )
  68. {
  69. if ( salary >= 0.0 )
  70. baseSalary = salary;
  71. else
  72. throw new IllegalArgumentException(
  73. "Base salary must be >= 0.0" );
  74. } // end method setBaseSalary
  75. public double getBaseSalary()
  76. {
  77. return baseSalary;
  78. } // end method getBaseSalary
  79. // calculate earnings; override method earnings in CommissionEmployee
  80. @Override
  81. public double earnings()
  82. {
  83. return getBaseSalary() + super.earnings();
  84. } // end method earnings
  85. // return String representation of BasePlusCommissionEmployee object
  86. @Override
  87. public String toString()
  88. {
  89. return String.format( "%s %s; %s: $%,.2f",
  90. "base-salaried", super.toString(),
  91. "base salary", getBaseSalary() );
  92. } // end method toString
  93. }

  94. // Fig. 10.7: CommissionEmployee.java
  95. // CommissionEmployee class extends Employee.
  96. public class CommissionEmployee extends Employee
  97. {
  98. private double grossSales; // gross weekly sales
  99. private double commissionRate; // commission percentage

  100. // five-argument constructor
  101. public CommissionEmployee( String first, String last, String ssn,
  102. double sales, double rate )
  103. {
  104. super( first, last, ssn );

  105.   setGrossSales( sales );
  106. setCommissionRate( rate );

  107. }
  108. // set commission rate
  109. public void setCommissionRate( double rate )
  110. {
  111. if ( rate > 0.0 && rate < 1.0 )
  112. commissionRate = rate;
  113. else
  114. throw new IllegalArgumentException(
  115. "Commission rate must be > 0.0 and < 1.0" );
  116. } // end method setCommissionRate

  117. // return commission rate
  118. public double getCommissionRate()
  119. {
  120. return commissionRate;
  121. } // end method getCommissionRate

  122. // set gross sales amount
  123. public void setGrossSales( double sales )
  124. {
  125. if ( sales >= 0.0 )
  126. grossSales = sales;
  127. else
  128. throw new IllegalArgumentException(
  129. "Gross sales must be >= 0.0" );
  130. } // end method setGrossSales

  131. // return gross sales amount
  132. public double getGrossSales()
  133. {
  134. return grossSales;
  135. } // end method getGrossSales

  136. // calculate earnings; override abstract method earnings in Employee
  137. @Override
  138. public double earnings()
  139. {
  140. return getCommissionRate() * getGrossSales();
  141. } // end method earnings
  142. // return String representation of CommissionEmployee object
  143. @Override
  144. public String toString()
  145. {
  146. return String.format( "%s: %s\n%s: $%,.2f; %s: %.2f",
  147. "commission employee", super.toString(),
  148. "gross sales", getGrossSales(),
  149. "commission rate", getCommissionRate() );
  150. } // end method toString
  151. } // end class CommissionEmployee

  152. // Fig. 10.6: HourlyEmployee.java
  153. // HourlyEmployee class extends Employee.

  154. public class HourlyEmployee extends Employee
  155. {

  156. private double wage; // wage per hour
  157. private double hours; // hours worked for week

  158. //five-argument constructor
  159. public HourlyEmployee( String first, String last, String ssn,
  160. double hourlyWage, double hoursWorked )
  161. {
  162. super( first, last, ssn );
  163. setWage( hourlyWage ); // validate hourly wage
  164. setHours( hoursWorked ); // validate hours worked
  165. } // end five-argument HourlyEmployee constructor

  166. // set wage
  167. public void setWage( double hourlyWage )
  168. {
  169. if ( hourlyWage >= 0.0 )
  170. wage = hourlyWage;
  171. else
  172. throw new IllegalArgumentException(
  173. "Hourly wage must be >= 0.0" );
  174. } // end method setWage

  175. // return wage
  176. public double getWage()
  177. {
  178. return wage;
  179. } // end method getWage

  180. // set hours worked
  181. public void setHours( double hoursWorked )
  182. {
  183. if ( ( hoursWorked >= 0.0 ) && ( hoursWorked <= 168.0 ) )
  184. hours = hoursWorked;
  185. else
  186. throw new IllegalArgumentException(
  187. "Hours worked must be >= 0.0 and <= 168.0" );
  188. } // end method setHours

  189. // return hours worked
  190. public double getHours()
  191. {
  192. return hours;
  193. } // end method getHours
  194. // calculate earnings; override abstract method earnings in Employee
  195. @Override
  196. public double earnings()
  197. {
  198. if ( getHours() <= 40 ) // no overtime
  199. return getWage() * getHours();
  200. else
  201. return 40 * getWage() + ( getHours() - 40 ) * getWage() * 1.5;
  202. } // end method earnings
  203. // return String representation of HourlyEmployee object
  204. @Override
  205. public String toString()
  206. {
  207. return String.format( "hourly employee: %s\n%s: $%,.2f; %s: %,.2f",
  208. super.toString(), "hourly wage", getWage(),
  209. "hours worked", getHours() );
  210. } // end method toString
  211. }

复制代码
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 31-8-2012 01:55 PM | 显示全部楼层
  1. // Fig. 10.12: Invoice.java
  2. // Invoice class that implements Payable.

  3. public class Invoice implements Payable
  4. {
  5.        

  6. private String partNumber;
  7. private String partDescription;
  8. private int quantity;
  9. private double pricePerItem;

  10. // four-argument constructor
  11. public Invoice( String part, String description, int count,
  12. double price )
  13. {
  14. partNumber = part;
  15. partDescription = description;
  16. setQuantity( count ); // validate and store quantity
  17. setPricePerItem( price ); // validate and store price per item
  18. } // end four-argument Invoice constructor

  19. // set part number
  20. public void setPartNumber( String part )
  21. {
  22. partNumber = part; // should validate
  23. } // end method setPartNumber

  24. // get part number
  25. public String getPartNumber()
  26. {
  27. return partNumber;
  28. } // end method getPartNumber

  29. // set description
  30. public void setPartDescription( String description )
  31. {
  32. partDescription = description; // should validate
  33. } // end method setPartDescription

  34. // get description
  35. public String getPartDescription()
  36. {
  37. return partDescription;
  38. } // end method getPartDescription

  39. // set quantity
  40. public void setQuantity( int count )
  41. {
  42. if ( count >= 0 )
  43. quantity = count;
  44. else
  45. throw new IllegalArgumentException( "Quantity must be >= 0" );
  46. } // end method setQuantity
  47.   // get quantity
  48. public int getQuantity()
  49. {

  50. return quantity;
  51. } // end method getQuantity

  52. // set price per item
  53. public void setPricePerItem( double price )
  54. {
  55. if ( price >= 0.0 )
  56. pricePerItem = price;
  57. else
  58. throw new IllegalArgumentException(
  59. "Price per item must be >= 0" );
  60. } // end method setPricePerItem

  61. // get price per item
  62. public double getPricePerItem()
  63. {
  64. return pricePerItem;
  65. } // end method getPricePerItem

  66. // return String representation of Invoice object
  67. @Override
  68. public String toString()
  69. {
  70. return String.format( "%s: \n%s: %s (%s) \n%s: %d \n%s: $%,.2f",
  71. "invoice", "part number", getPartNumber(), getPartDescription(),
  72. "quantity", getQuantity(), "price per item", getPricePerItem() );
  73. } // end method toString

  74. // method required to carry out contract with interface Payable
  75. @Override
  76. public double getPaymentAmount()
  77. {
  78. return getQuantity() * getPricePerItem(); // calculate total cost
  79. } // end method getPaymentAmount
  80. }


  81. // Fig. 10.11: Payable.java
  82. // Payable interface declaration.

  83. public interface Payable
  84. {
  85. double getPaymentAmount(); // calculate payment; no implementation
  86. } // end interface Payable



  87. // Fig. 10.15: PayableInterfaceTest.java
  88. // Tests interface Payable.

  89. public class PayableInterfaceTest
  90. {
  91. public static void main( String[] args )
  92. {
  93. // create four-element Payable array
  94. Payable[] payableObjects = new Payable[ 4 ];

  95. // populate array with objects that implement Payable
  96. payableObjects[ 0 ] = new Invoice( "01234", "seat", 2, 375.00 );
  97. payableObjects[ 1 ] = new Invoice( "56789", "tire", 4, 79.95 );
  98. payableObjects[ 2 ] = new SalariedEmployee( "John", "Smith", "111-11-1111", 800.00 );
  99. payableObjects[ 3 ] = new SalariedEmployee( "Lisa", "Barnes", "888-88-8888", 1200.00 );

  100. System.out.println(
  101. "Invoices and Employees processed polymorphically:\n" );

  102. // generically process each element in array payableObjects
  103. for ( Payable currentPayable : payableObjects )
  104. {
  105. // output currentPayable and its appropriate payment amount
  106. System.out.printf( "%s \n%s: $%,.2f\n\n",
  107. currentPayable.toString(),
  108. "payment due",currentPayable.getPaymentAmount() );
  109. } // end for
  110. } // end main
  111. } // end class PayableInterfaceTest


  112. // Fig. 10.9: PayrollSystemTest.java
  113. // Employee hierarchy test program.
  114. public class PayrollSystemTest
  115. {
  116. public static void main( String[] args )
  117. {
  118.        
  119.        
  120.         // create subclass objects
  121. SalariedEmployee salariedEmployee =
  122. new SalariedEmployee( "John", "Smith", "111-11-1111", 800.00 );

  123. HourlyEmployee hourlyEmployee =
  124. new HourlyEmployee( "Karen", "Price", "222-22-2222", 16.75, 40 );
  125. CommissionEmployee commissionEmployee =
  126. new CommissionEmployee(
  127. "Sue", "Jones", "333-33-3333", 10000, .06 );
  128. BasePlusCommissionEmployee basePlusCommissionEmployee =
  129. new BasePlusCommissionEmployee(
  130. "Bob", "Lewis", "444-44-4444", 5000, .04, 300 );
  131.        
  132.   System.out.println( "Employees processed individually:\n" );

  133. System.out.printf( "%s\n%s: $%,.2f\n\n",
  134. salariedEmployee, "earned", salariedEmployee.earnings() );
  135. System.out.printf( "%s\n%s: $%,.2f\n\n",
  136. hourlyEmployee, "earned", hourlyEmployee.earnings() );
  137. System.out.printf( "%s\n%s: $%,.2f\n\n",
  138. commissionEmployee, "earned", commissionEmployee.earnings() );
  139. System.out.printf( "%s\n%s: $%,.2f\n\n",
  140. basePlusCommissionEmployee,
  141. "earned", basePlusCommissionEmployee.earnings() );

  142. Employee[] employees = new Employee[ 4 ];
  143. // initialize array with Employees
  144. employees[ 0 ] = salariedEmployee;
  145. employees[ 1 ] = hourlyEmployee;
  146. employees[ 2 ] = commissionEmployee;
  147. employees[ 3 ] = basePlusCommissionEmployee;


  148. System.out.println( "Employees processed polymorphically:\n" );

  149. // generically process each element in array employees
  150. for ( Employee currentEmployee : employees )
  151. {
  152. System.out.println(currentEmployee ); // invokes toString       

  153. // determine whether element is a BasePlusCommissionEmployee
  154. if ( currentEmployee instanceof BasePlusCommissionEmployee)
  155. {

  156. // downcast Employee reference to
  157. // BasePlusCommissionEmployee reference
  158. BasePlusCommissionEmployee employee =
  159. (BasePlusCommissionEmployee) currentEmployee;


  160. employee.setBaseSalary( 1.10 * employee.getBaseSalary() );

  161. System.out.printf(
  162. "new base salary with 10%% increase is: $%,.2f\n",
  163. employee.getBaseSalary() );
  164. } // end if

  165.         System.out.printf( "earned $%,.2f\n\n",currentEmployee.earnings() );
  166. } // end for

  167. // get type name of each object in employees array
  168. for ( int j = 0; j < employees.length; j++ )
  169. System.out.printf( "Employee %d is a %s\n", j,
  170. employees[ j ].getClass().getName() );
  171. }
  172. }


  173. // Fig. 10.5: SalariedEmployee.java
  174. // SalariedEmployee concrete class extends abstract class Employee.
  175. public class SalariedEmployee extends Employee
  176. {
  177. private double weeklySalary;

  178. // four-argument constructor
  179. public SalariedEmployee( String first, String last, String ssn,
  180. double salary )
  181. {
  182. super( first, last, ssn ); // pass to Employee constructor
  183. setWeeklySalary( salary ); // validate and store salary
  184. } // end four-argument SalariedEmployee constructor

  185. // set salary
  186. public void setWeeklySalary( double salary )
  187. {
  188. if ( salary >= 0.0 )
  189. weeklySalary = salary;
  190. else
  191. throw new IllegalArgumentException(
  192. "Weekly salary must be >= 0.0" );
  193. } // end method setWeeklySalary

  194. // return salary
  195. public double getWeeklySalary()
  196. {
  197. return weeklySalary;
  198. } // end method getWeeklySalary

  199. // calculate earnings; override abstract method earnings in Employee

  200. @Override
  201. public double getPaymentAmount()
  202. {
  203. return getWeeklySalary();
  204. } // end method getPaymentAmount

  205. @Override
  206. public double earnings()
  207. {
  208. return getWeeklySalary();
  209. } // end method earnings
  210. // return String representation of SalariedEmployee object
  211. @Override
  212. public String toString()
  213. {
  214. return String.format( "salaried employee: %s\n%s: $%,.2f",
  215. super.toString(), "weekly salary", getWeeklySalary() );
  216. } // end method toString
  217. }

复制代码
回复

使用道具 举报

发表于 31-8-2012 09:13 PM | 显示全部楼层
NetBeans,Eclipse,JCreator
其实还有一个叫RAD的.. 是 EClipse 的分支..
需要安装到3个没? 懂用一个, 知道它的理论.. 其实其它都一样的..
不同的是软件UI不一样而已, 骨子还是一样..

懂 OOP 只是JAVA的基础.. 或其它第4代电脑语言的基础..
有时间研究 Design Patterns 才是王道..
回复

使用道具 举报

发表于 16-11-2012 02:02 PM | 显示全部楼层
jasonmun 发表于 31-8-2012 09:13 PM
NetBeans,Eclipse,JCreator
其实还有一个叫RAD的.. 是 EClipse 的分支..
需要安装到3个没? 懂用一个, 知 ...

想问大大~~ 爬了很多帖~~
看到很多人讨论OOP~~ 也大概知道这东西~
可是~~怎样才能学习OOP阿?
我本身是读E-commerce 的~~
想问~~我应不应该学习java呢? 基础属否不一样?
回复

使用道具 举报

发表于 16-11-2012 02:38 PM | 显示全部楼层
BakaYY_Ling 发表于 16-11-2012 02:02 PM
想问大大~~ 爬了很多帖~~
看到很多人讨论OOP~~ 也大概知道这东西~
可是~~怎样才能学习OOP阿?

1. 懂了OOP的概念, 就多看别人的 coding. 自然你就会熟悉.. 知道怎样写与用它..
2. 学不学 JAVA, 跟你念的没有关系.. 而是看你有没有兴趣去学.

回复

使用道具 举报

发表于 16-11-2012 05:53 PM | 显示全部楼层
学不学 JAVA, 跟你念的没有关系.. 而是看你有没有兴趣去学.

+1,赞成。
回复

使用道具 举报

Follow Us
发表于 16-11-2012 10:57 PM | 显示全部楼层
不好意思...
我是java新手。
请问,lz的笔记是怎么样参考?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 9-10-2025 02:48 PM , Processed in 0.123532 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表