======================================================================
java 第七天 : 接口的作用 , 接口的思想
======================================================================
(1) 接口的第一种用途 : 实现多继承 .( 最低的要求 .)
例子 : 就是让某些子类实现这个接口 :// 在已经形成的树状结构 , 实现再抽象 .
1. 在原有的雇员练习上修改代码
公司会给 SalaryEmployee 每月另外发放 2000 元加班费 , 给 BasePlusSalesEmployee 发放 1000 元加班费
改写原有代码 , 加入以上的逻辑
并写一个方法 , 打印出本月公司总共发放了多少加班费 ;
package test;
java 第七天 : 接口的作用 , 接口的思想
======================================================================
(1) 接口的第一种用途 : 实现多继承 .( 最低的要求 .)
例子 : 就是让某些子类实现这个接口 :// 在已经形成的树状结构 , 实现再抽象 .
1. 在原有的雇员练习上修改代码
公司会给 SalaryEmployee 每月另外发放 2000 元加班费 , 给 BasePlusSalesEmployee 发放 1000 元加班费
改写原有代码 , 加入以上的逻辑
并写一个方法 , 打印出本月公司总共发放了多少加班费 ;
package test;
public
class
TestEmployee {
public
static
void
main(String[] args) {
Employee[] es =
new
Employee[4];
es[0] =
new
SalariedEmployee(
"Liucy"
, 10, 8000);
es[1] =
new
HourlyEmployee(
"Chenzq"
, 2, 20, 300);
es[2] =
new
SalesEmployee(
"DuBin"
, 8, 100000, 0.05);
es[3] =
new
BasePlusSalesEmployee(
"Hanlj"
, 10, 30000, 0.2, 5000);
for
(
int
i = 0; i < es.
length
; i++) {
System.
out
.print(es[i].getName() +
": "
);
System.
out
.println(es[i].getSalary(10));
}
double
result = 0;
for
(
int
i = 0; i < es.
length
; i++) {
if
(es[i]
instanceof
addAble) {
addAble a = (addAble) es[i];
result += a.getAddMoney();
}
}
System.
out
.println(
"
总共发的加班费是
"
+ result);
}
}
class
Employee {
private
String
name
;
private
int
birthday
;
Employee() {
}
Employee(String name,
int
birthday) {
this
.
name
= name;
this
.
birthday
= birthday;
}
double
getSalary(
int
month) {
if
(month ==
birthday
) {
return
100;
}
return
0;
}
String getName() {
return
name
;
}
}
interface
addAble {
double
getAddMoney();
}
class
SalariedEmployee
extends
Employee
implements
addAble {
private
int
monthsalary
;
SalariedEmployee() {
}
SalariedEmployee(String name,
int
birthday,
int
monthsalary) {
super
(name, birthday);
this
.
monthsalary
= monthsalary;
}
double
getSalary(
int
month) {
return
super
.getSalary(month) +
monthsalary
+ getAddMoney();
}
public
double
getAddMoney() {
return
2000;
}
}
class
HourlyEmployee
extends
Employee {
private
int
hoursalary
;
private
int
hour
;
HourlyEmployee() {
}
HourlyEmployee(String name,
int
birthday,
int
hoursalary,
int
hour) {
super
(name, birthday);
this
.
hoursalary
= hoursalary;
this
.
hour
= hour;
}
double
getSalary(
int
month) {
double
m = 0;
if
(
hour
>= 160) {
return
super
.getSalary(month) +
hoursalary
* 160 + 1.5 *
hoursalary
* (
hour
- 160);
}
else
return
super
.getSalary(month) +
hoursalary
*
hour
;
}
}
class
SalesEmployee
extends
Employee {
private
int
sale
;
private
double
rate
;
SalesEmployee() {
}
SalesEmployee(String name,
int
birthday,
int
sale,
double
rate) {
super
(name, birthday);
this
.
sale
= sale;
this
.
rate
= rate;
}
double
getSalary(
int
month) {
return
super
.getSalary(month) +
sale
*
rate
;
}
}
class
BasePlusSalesEmployee
extends
SalesEmployee
implements
addAble {
private
int
basesalary
;
BasePlusSalesEmployee() {
}
BasePlusSalesEmployee(String name,
int
birthday,
int
sale,
double
rate,
int
basesalary) {
super
(name, birthday, sale, rate);
this
.
basesalary
= basesalary;
}
double
getSalary(
int
month) {
return
super
.getSalary(month) +
basesalary
+ getAddMoney();
}
public
double
getAddMoney() {
return
1000;
}
}
======================================================================
(2) 接口的单继承:
接口像一个类的额外的信息().
java的多继承是有主次的,有主类型,附加的类型(副类型[接口]是一个附加信息.)
java中的多继承: 区分主类型和次要类型,接口的引用就使得我们可以对事物的共性,作一个再抽象.抽象出副类型.这种多继承的关系,是不会破坏单继承树状关系的复杂度.
======================================================================
(3) 接口的精华.( 没有实现和标准)
最重要的精华,是在于方法没有实现上在于接口是一个标准上,
接口是一种标准.有分工就一定要有接口.接口定义了标准,让分工后的能够
package test;
======================================================================
(2) 接口的单继承:
接口像一个类的额外的信息().
java的多继承是有主次的,有主类型,附加的类型(副类型[接口]是一个附加信息.)
java中的多继承: 区分主类型和次要类型,接口的引用就使得我们可以对事物的共性,作一个再抽象.抽象出副类型.这种多继承的关系,是不会破坏单继承树状关系的复杂度.
======================================================================
(3) 接口的精华.( 没有实现和标准)
最重要的精华,是在于方法没有实现上在于接口是一个标准上,
接口是一种标准.有分工就一定要有接口.接口定义了标准,让分工后的能够
package test;
public
class
TestJDBC {
public
static
void
main(String[] args) {
Driver d = getDriver(args[0]);
//
这个接口由
sun
公司制定
(
多态
),
把标准的制定者
,
标准的实现者
(
二者分离
),
//
标准的使用者
.(
使用标准
.)
不同的
[
我们是针对接口编程
]
d.connect();
//
传入不同的参数
,
会调用不同的实现
,
在这里具体的调用了不同的实现
.
}
//
屏蔽接口不同实现的差异
.!!!
public
static
Driver getDriver(String dbName) {
if
(dbName.equals(
"MySql"
))
return
new
MySqlDriver();
if
(dbName.equals(
"Db2"
))
return
new
Db2Driver();
return
null
;
}
}
// sun
公司和数据库厂商
,
协调出了一个都遵循的标准
(
接口
),
接口的定义由
sun
公司来作
,
//
具体的实现由数据库厂商实现
interface
Driver {
//
这个
void
connect();
}
// MySql
class
MySqlDriver
implements
Driver {
public
void
connect() {
System.
out
.println(
"ConnetMySql!!!
呵呵
"
);
}
}
class
Db2Driver
implements
Driver {
public
void
connect() {
System.
out
.println(
"ConnectDb2!!
呵呵
"
);
}
}
==========================================================
package test;
package test;
public
class
TestJDBC2 {
public
static
void
main(String[] args) {
//
这样会造成程序和数据库有强藕合的特性
.
MySqlDriver ss =
new
MySqlDriver();
//
这些代码都要换
,
造成重复开发
.
ss.connect();
Db2Driver dd =
new
Db2Driver();
dd.getConnect();
}
}
class
MySqlDriver {
public
void
connect() {
System.
out
.println(
"ConnetMySql!!!
呵呵
"
);
}
}
class
Db2Driver {
public
void
getConnect() {
System.
out
.println(
"ConnectDb2!!
呵呵
"
);
}
}
======================================================================
package test;
======================================================================
package test;
public
class
TestArray {
public
static
void
main(String[] args) {
int
i = Integer.parseInt(args[0]);
ArrayTool at;
if
(i == 0)
at =
new
ArrayTool1();
else
at =
new
ArrayTool2();
User u =
new
User();
u.method(at);
}
}
class
User {
public
void
method(ArrayTool at) {
int
[] a = { 1, 3, 4, 6, 5, 0, 0, 0 };
int
index = 5;
a = at.insert(a, index, 2, 7);
for
(
int
i = 0; i < a.
length
; i++) {
System.
out
.print(a[i] +
"/t"
);
}
System.
out
.println();
}
}
class
ArrayTool1
implements
ArrayTool {
public
int
[] insert(
int
[] data,
int
index,
int
pos,
int
value) {
for
(
int
i = index; i > pos; i--) {
data[i] = data[i - 1];
}
data[pos] = value;
return
data;
}
}
class
ArrayTool2
implements
ArrayTool {
public
int
[] insert(
int
[] data,
int
index,
int
pos,
int
value) {
int
[] data2 =
new
int
[data.
length
+ 1];
System.arraycopy(data, 0, data2, 0, pos);
data2[pos] = value;
System.arraycopy(data, pos, data2, pos + 1, (data.
length
- pos));
return
data2;
}
}
interface
ArrayTool {
int
[] insert(
int
[] data,
int
index,
int
pos,
int
value);
}