查看: 1834|回复: 6
|
Java学习笔记高级篇01
[复制链接]
|
|
学习Java三个月,开贴与同样学习著Java的朋友讨论。我安装了三个IDE,NetBeans,Eclipse,JCreator。
以下是个OOP的范列:- // Fig. 10.4: Employee.java
- // Employee abstract superclass.
- public abstract class Employee implements Payable
- {
- private String firstName;
- private String lastName;
- private String socialSecurityNumber;
- // three-argument constructor
- public Employee( String first, String last, String ssn )
- {
- firstName = first;
- lastName = last;
- socialSecurityNumber = ssn;
- } // end three-argument Employee constructor
-
- // set first name
- public void setFirstName( String first )
- {
- firstName = first; // should validate
- } // end method setFirstName
- // return first name
- public String getFirstName()
- {
- return firstName;
- } // end method getFirstName
- // set last name
- public void setLastName( String last )
- {
- lastName = last; // should validate
- } // end method setLastName
- // return last name
- public String getLastName()
- {
- return lastName;
- } // end method getLastName
- // set social security number
- public void setSocialSecurityNumber( String ssn )
- {
- socialSecurityNumber = ssn; // should validate
- } // end method setSocialSecurityNumber
- // return social security number
- public String getSocialSecurityNumber()
- {
- return socialSecurityNumber;
- } // end method getSocialSecurityNumber
- // return String representation of Employee object
- @Override
- public String toString()
- {
- return String.format( "%s %s\nsocial security number: %s",
- getFirstName(), getLastName(), getSocialSecurityNumber() );
- } // end method toString
-
- public abstract double earnings();
- }
-
-
-
- // Fig. 10.8: BasePlusCommissionEmployee.java
- // BasePlusCommissionEmployee class extends CommissionEmployee.
- public class BasePlusCommissionEmployee extends CommissionEmployee
- {
- private double baseSalary; // base salary per week
- //six-argument constructor
- public BasePlusCommissionEmployee( String first, String last,
- String ssn, double sales, double rate, double salary )
- {
- super( first, last, ssn, sales, rate );
- setBaseSalary( salary ); // validate and store base salary
- } // end six-argument BasePlusCommissionEmployee constructor
- // set base salary
- public void setBaseSalary( double salary )
- {
- if ( salary >= 0.0 )
- baseSalary = salary;
- else
- throw new IllegalArgumentException(
- "Base salary must be >= 0.0" );
- } // end method setBaseSalary
- public double getBaseSalary()
- {
- return baseSalary;
- } // end method getBaseSalary
- // calculate earnings; override method earnings in CommissionEmployee
- @Override
- public double earnings()
- {
- return getBaseSalary() + super.earnings();
- } // end method earnings
- // return String representation of BasePlusCommissionEmployee object
- @Override
- public String toString()
- {
- return String.format( "%s %s; %s: $%,.2f",
- "base-salaried", super.toString(),
- "base salary", getBaseSalary() );
- } // end method toString
- }
- // Fig. 10.7: CommissionEmployee.java
- // CommissionEmployee class extends Employee.
- public class CommissionEmployee extends Employee
- {
- private double grossSales; // gross weekly sales
- private double commissionRate; // commission percentage
- // five-argument constructor
- public CommissionEmployee( String first, String last, String ssn,
- double sales, double rate )
- {
- super( first, last, ssn );
-
- setGrossSales( sales );
- setCommissionRate( rate );
- }
- // set commission rate
- public void setCommissionRate( double rate )
- {
- if ( rate > 0.0 && rate < 1.0 )
- commissionRate = rate;
- else
- throw new IllegalArgumentException(
- "Commission rate must be > 0.0 and < 1.0" );
- } // end method setCommissionRate
- // return commission rate
- public double getCommissionRate()
- {
- return commissionRate;
- } // end method getCommissionRate
- // set gross sales amount
- public void setGrossSales( double sales )
- {
- if ( sales >= 0.0 )
- grossSales = sales;
- else
- throw new IllegalArgumentException(
- "Gross sales must be >= 0.0" );
- } // end method setGrossSales
- // return gross sales amount
- public double getGrossSales()
- {
- return grossSales;
- } // end method getGrossSales
- // calculate earnings; override abstract method earnings in Employee
- @Override
- public double earnings()
- {
- return getCommissionRate() * getGrossSales();
- } // end method earnings
- // return String representation of CommissionEmployee object
- @Override
- public String toString()
- {
- return String.format( "%s: %s\n%s: $%,.2f; %s: %.2f",
- "commission employee", super.toString(),
- "gross sales", getGrossSales(),
- "commission rate", getCommissionRate() );
- } // end method toString
- } // end class CommissionEmployee
- // Fig. 10.6: HourlyEmployee.java
- // HourlyEmployee class extends Employee.
- public class HourlyEmployee extends Employee
- {
- private double wage; // wage per hour
- private double hours; // hours worked for week
- //five-argument constructor
- public HourlyEmployee( String first, String last, String ssn,
- double hourlyWage, double hoursWorked )
- {
- super( first, last, ssn );
- setWage( hourlyWage ); // validate hourly wage
- setHours( hoursWorked ); // validate hours worked
- } // end five-argument HourlyEmployee constructor
- // set wage
- public void setWage( double hourlyWage )
- {
- if ( hourlyWage >= 0.0 )
- wage = hourlyWage;
- else
- throw new IllegalArgumentException(
- "Hourly wage must be >= 0.0" );
- } // end method setWage
- // return wage
- public double getWage()
- {
- return wage;
- } // end method getWage
- // set hours worked
- public void setHours( double hoursWorked )
- {
- if ( ( hoursWorked >= 0.0 ) && ( hoursWorked <= 168.0 ) )
- hours = hoursWorked;
- else
- throw new IllegalArgumentException(
- "Hours worked must be >= 0.0 and <= 168.0" );
- } // end method setHours
- // return hours worked
- public double getHours()
- {
- return hours;
- } // end method getHours
- // calculate earnings; override abstract method earnings in Employee
- @Override
- public double earnings()
- {
- if ( getHours() <= 40 ) // no overtime
- return getWage() * getHours();
- else
- return 40 * getWage() + ( getHours() - 40 ) * getWage() * 1.5;
- } // end method earnings
- // return String representation of HourlyEmployee object
- @Override
- public String toString()
- {
- return String.format( "hourly employee: %s\n%s: $%,.2f; %s: %,.2f",
- super.toString(), "hourly wage", getWage(),
- "hours worked", getHours() );
- } // end method toString
- }
复制代码 |
|
|
|
|
|
|
|

楼主 |
发表于 31-8-2012 01:55 PM
|
显示全部楼层
- // Fig. 10.12: Invoice.java
- // Invoice class that implements Payable.
- public class Invoice implements Payable
- {
-
- private String partNumber;
- private String partDescription;
- private int quantity;
- private double pricePerItem;
- // four-argument constructor
- public Invoice( String part, String description, int count,
- double price )
- {
- partNumber = part;
- partDescription = description;
- setQuantity( count ); // validate and store quantity
- setPricePerItem( price ); // validate and store price per item
- } // end four-argument Invoice constructor
- // set part number
- public void setPartNumber( String part )
- {
- partNumber = part; // should validate
- } // end method setPartNumber
- // get part number
- public String getPartNumber()
- {
- return partNumber;
- } // end method getPartNumber
- // set description
- public void setPartDescription( String description )
- {
- partDescription = description; // should validate
- } // end method setPartDescription
- // get description
- public String getPartDescription()
- {
- return partDescription;
- } // end method getPartDescription
- // set quantity
- public void setQuantity( int count )
- {
- if ( count >= 0 )
- quantity = count;
- else
- throw new IllegalArgumentException( "Quantity must be >= 0" );
- } // end method setQuantity
- // get quantity
- public int getQuantity()
- {
- return quantity;
- } // end method getQuantity
- // set price per item
- public void setPricePerItem( double price )
- {
- if ( price >= 0.0 )
- pricePerItem = price;
- else
- throw new IllegalArgumentException(
- "Price per item must be >= 0" );
- } // end method setPricePerItem
- // get price per item
- public double getPricePerItem()
- {
- return pricePerItem;
- } // end method getPricePerItem
- // return String representation of Invoice object
- @Override
- public String toString()
- {
- return String.format( "%s: \n%s: %s (%s) \n%s: %d \n%s: $%,.2f",
- "invoice", "part number", getPartNumber(), getPartDescription(),
- "quantity", getQuantity(), "price per item", getPricePerItem() );
- } // end method toString
- // method required to carry out contract with interface Payable
- @Override
- public double getPaymentAmount()
- {
- return getQuantity() * getPricePerItem(); // calculate total cost
- } // end method getPaymentAmount
- }
- // Fig. 10.11: Payable.java
- // Payable interface declaration.
- public interface Payable
- {
- double getPaymentAmount(); // calculate payment; no implementation
- } // end interface Payable
- // Fig. 10.15: PayableInterfaceTest.java
- // Tests interface Payable.
- public class PayableInterfaceTest
- {
- public static void main( String[] args )
- {
- // create four-element Payable array
- Payable[] payableObjects = new Payable[ 4 ];
- // populate array with objects that implement Payable
- payableObjects[ 0 ] = new Invoice( "01234", "seat", 2, 375.00 );
- payableObjects[ 1 ] = new Invoice( "56789", "tire", 4, 79.95 );
- payableObjects[ 2 ] = new SalariedEmployee( "John", "Smith", "111-11-1111", 800.00 );
- payableObjects[ 3 ] = new SalariedEmployee( "Lisa", "Barnes", "888-88-8888", 1200.00 );
- System.out.println(
- "Invoices and Employees processed polymorphically:\n" );
- // generically process each element in array payableObjects
- for ( Payable currentPayable : payableObjects )
- {
- // output currentPayable and its appropriate payment amount
- System.out.printf( "%s \n%s: $%,.2f\n\n",
- currentPayable.toString(),
- "payment due",currentPayable.getPaymentAmount() );
- } // end for
- } // end main
- } // end class PayableInterfaceTest
- // Fig. 10.9: PayrollSystemTest.java
- // Employee hierarchy test program.
- public class PayrollSystemTest
- {
- public static void main( String[] args )
- {
-
-
- // create subclass objects
- SalariedEmployee salariedEmployee =
- new SalariedEmployee( "John", "Smith", "111-11-1111", 800.00 );
- HourlyEmployee hourlyEmployee =
- new HourlyEmployee( "Karen", "Price", "222-22-2222", 16.75, 40 );
- CommissionEmployee commissionEmployee =
- new CommissionEmployee(
- "Sue", "Jones", "333-33-3333", 10000, .06 );
- BasePlusCommissionEmployee basePlusCommissionEmployee =
- new BasePlusCommissionEmployee(
- "Bob", "Lewis", "444-44-4444", 5000, .04, 300 );
-
- System.out.println( "Employees processed individually:\n" );
- System.out.printf( "%s\n%s: $%,.2f\n\n",
- salariedEmployee, "earned", salariedEmployee.earnings() );
- System.out.printf( "%s\n%s: $%,.2f\n\n",
- hourlyEmployee, "earned", hourlyEmployee.earnings() );
- System.out.printf( "%s\n%s: $%,.2f\n\n",
- commissionEmployee, "earned", commissionEmployee.earnings() );
- System.out.printf( "%s\n%s: $%,.2f\n\n",
- basePlusCommissionEmployee,
- "earned", basePlusCommissionEmployee.earnings() );
- Employee[] employees = new Employee[ 4 ];
- // initialize array with Employees
- employees[ 0 ] = salariedEmployee;
- employees[ 1 ] = hourlyEmployee;
- employees[ 2 ] = commissionEmployee;
- employees[ 3 ] = basePlusCommissionEmployee;
- System.out.println( "Employees processed polymorphically:\n" );
- // generically process each element in array employees
- for ( Employee currentEmployee : employees )
- {
- System.out.println(currentEmployee ); // invokes toString
-
- // determine whether element is a BasePlusCommissionEmployee
- if ( currentEmployee instanceof BasePlusCommissionEmployee)
- {
- // downcast Employee reference to
- // BasePlusCommissionEmployee reference
- BasePlusCommissionEmployee employee =
- (BasePlusCommissionEmployee) currentEmployee;
-
- employee.setBaseSalary( 1.10 * employee.getBaseSalary() );
- System.out.printf(
- "new base salary with 10%% increase is: $%,.2f\n",
- employee.getBaseSalary() );
- } // end if
- System.out.printf( "earned $%,.2f\n\n",currentEmployee.earnings() );
- } // end for
-
- // get type name of each object in employees array
- for ( int j = 0; j < employees.length; j++ )
- System.out.printf( "Employee %d is a %s\n", j,
- employees[ j ].getClass().getName() );
- }
- }
- // Fig. 10.5: SalariedEmployee.java
- // SalariedEmployee concrete class extends abstract class Employee.
- public class SalariedEmployee extends Employee
- {
- private double weeklySalary;
- // four-argument constructor
- public SalariedEmployee( String first, String last, String ssn,
- double salary )
- {
- super( first, last, ssn ); // pass to Employee constructor
- setWeeklySalary( salary ); // validate and store salary
- } // end four-argument SalariedEmployee constructor
- // set salary
- public void setWeeklySalary( double salary )
- {
- if ( salary >= 0.0 )
- weeklySalary = salary;
- else
- throw new IllegalArgumentException(
- "Weekly salary must be >= 0.0" );
- } // end method setWeeklySalary
-
- // return salary
- public double getWeeklySalary()
- {
- return weeklySalary;
- } // end method getWeeklySalary
-
- // calculate earnings; override abstract method earnings in Employee
-
- @Override
- public double getPaymentAmount()
- {
- return getWeeklySalary();
- } // end method getPaymentAmount
-
- @Override
- public double earnings()
- {
- return getWeeklySalary();
- } // end method earnings
- // return String representation of SalariedEmployee object
- @Override
- public String toString()
- {
- return String.format( "salaried employee: %s\n%s: $%,.2f",
- super.toString(), "weekly salary", getWeeklySalary() );
- } // end method toString
- }
复制代码 |
|
|
|
|
|
|
|
发表于 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,赞成。 |
|
|
|
|
|
|
|
发表于 16-11-2012 10:57 PM
|
显示全部楼层
不好意思...
我是java新手。
请问,lz的笔记是怎么样参考? |
|
|
|
|
|
|
| |
本周最热论坛帖子
|