一、接口
?
八種基本數據類型:
整數類型:byte、short、int、long;浮點型:float、double;char;boolean。
三種引用數據類型:
數組[];
類:class;
接口:interface。
1:有些時候需要描述一組功能。而不是某種類型的對象。(面向功能開發)。
2:使用接口可以實現變相的多重繼承。而且還不存在c++多重繼承帶來的隱患。
3:使用接口同樣可以實現多態。
接口的語法:
[權限修飾符] interface 接口名{
// 接口體
}
例1:
//舉辦一個飛行比賽??參賽者?有??飛機、超人、石頭、鳥。??
//關注的是功能層面的。
//需要用同一種類型來處理這四個參賽者。?使用一個數組來管理這四個參賽者
public?class?TestInterface {
public?static?void?main(String[] args) {
Flyable[] flyables?= new?Flyable[4];
flyables[0] = new?Bird();
flyables[1] = new?SuperMan();
flyables[2] = new?Stone();
flyables[3] = new?Plane();
for(int?i=0;i
flyables[i].fly();
}
}
}
//描述飛行的功能
interface?Flyable{
abstract?void?fly();
}
//無實意,為了演示變相多重繼承
class?Animal{
}
//變相的多重繼承
class?Bird extends?Animal implements?Flyable{
public?void?fly() {
System.out.println("小鳥撲棱著翅膀,飛過了樹梢!");
}
}
class?SuperMan implements?Flyable{
@Override
public?void?fly() {
System.out.println("超人內褲外穿,穿著披風,雙手握拳伸向前方,飛向了外太空!");
}
}
class?Stone implements?Flyable{
@Override
public?void?fly() {
System.out.println("石頭被人扔出了十幾米遠,在空中劃出了一道美麗的弧線!");
}
}
class?Plane implements?Flyable{
@Override
public?void?fly() {
System.out.println("飛機屁股冒著煙在平流層飛行!");
}
}
?
例2:
public?class?TestUsb {
public?static?void?main(String[] args) {
Computer computer?= new?Computer();
Fun fun?= new?Fun();
LedLight ledLight?= new?LedLight();
computer.load(fun);
computer.load(ledLight);
}
}
class?Computer{
//定義一個裝載USB設備的方法
void?load(Usb usb){
usb.run(this);
}
}
//描述usb功能
interface?Usb{
//描述通過usb?接口如何讓設備運轉起來
void?run(Computer computer);
}
class?Fun implements?Usb{
@Override
public?void?run(Computer computer) {
System.out.println("通過usb 小風扇轉了起來,吹出了涼爽的風!");
}
}
class?LedLight implements?Usb{
@Override
public?void?run(Computer computer) {
System.out.println("通過usb LED 小臺燈,發出了溫馨的光芒!");
}
}
?
二、接口總結
?
定義:接口是一組終態靜態公有的成員變量(出現比較少)和抽象公有的方法的集合。
接口特點:
1:接口中定義的變量只能是 靜態的終態的公有的變量。而且public static final 都可以省略。
2:接口中不能包含代碼塊、構造塊、靜態代碼塊。
3:接口中不能包含構造方法。只能作為父接口使用,被子類實現。
4:接口中只能定義公有的抽象的方法(后續的jdk版本中支持了接口中方法可以自定義默認實現)。public abstract 可以省略。
5:接口往往是一組(1-n)個抽象方法的集合。
6:類是用來描述對象(往往存在屬性)的,接口是用來描述一組功能(沒有屬性)。
7:接口可以變相的實現多重繼承,是用關鍵字implements 。類實現某個接口,也可以實現多個接口,使用逗號分割,并可以繼承某個類。
8:接口同樣可以實現多態。父接口引用指向子類對象,子類必須要實現父接口的方法,最終訪問的是子類的實現的方法。
9:接口定義了一組規范。接口定義的是規則。給實現的子類定義的。
在某些語言中,使用單詞protocol 作為接口的關鍵字。協議的意思。
10:接口同樣還可以繼承接口使用關鍵字 extends。可以實現直接繼承多個接口,使用逗號分割。
11:繼承描述的關系是 is-a 關系。實現接口描述的是 has-a 的關系。
12:使用接口可以實現 ?設計(定義接口)和實現(子類實現接口)相分離。
13:接口是一種可以將某些功能混合到某些類型的主類型中去的類型。
比如:將USB接口、網線接口、電源充電器插頭的這些功能類,混合到電腦這個主類上,使主類的功能增加。
例3:
//定義一個類,具有報警功能的門。
class Door {
void open(){}
void close(){}
}
//類實現接口用implements,多個接口之間用“,”隔開
class AlarmDoor extends Door implements Alarmable,SeeOutable,A,B{
public void alarm(){}
public void seeOut(){}
public void A(){}
public void B(){}
public void C(){}
}
interface Alarmable{
//公有的抽象的方法?public abstract 可以省略。
void alarm();
}
//接口繼承接口用extends ,多個接口之間用“,”隔開
interface SeeOutable?extends A,B{
void seeOut();
}
interface Aable{
void A();
}
interface Bable{
void B();
void C();
}
例4:
//登錄注冊功能大概的思路
public?class?TestLogin {
public?static?void?main(String[] args) {
LoginAble login?= new?MyLogin();
int?result?= login.regist("", "");
}
}
//項目組長定義規范
interface?LoginAble{
/**
?* 注冊用戶的功能
?* @param?userName 要注冊的用戶的名字
?* @param?pwd ?注冊用戶定義的密碼
?* @return?如果用戶名不合法?返回?2,如果密碼不合法?返回?3,如果用戶名已經存在?返回?4,如果請求超時?返回?5 注冊成功返回?1;
?*/
int?regist(String userName,String pwd);
/**
?* 注冊完成之后,登錄系統
?* @param?userName 登錄的用戶名
?* @param?pwd 登錄的密碼
?* @return?如果用戶不存在?返回?2,如果用戶名密碼不匹配??返回?3,如果登錄超時?返回?4,登錄成功返回?1;
?*/
int?login(String userName,String pwd);
}
//普通碼農做的事情
class?MyLogin implements?LoginAble{
@Override
public?int?regist(String userName, String pwd) {
// TODO?Auto-generated method stub
return?1;
}
@Override
public?int?login(String userName, String pwd) {
// TODO?Auto-generated method stub
return?1;
}
}
三、冒泡排序
例:
import?java.util.Arrays;
import?com.bjsxt.util.MyUtil;
/**
?* 冒泡排序
?*
?*/
public?class?TestBubble {
public?static?void?main(String[] args) {
test1();
}
static?void?test1(){
int[] array?= new?int[10];
for(int?i=0;i
array[i] = MyUtil.getRandomNumber(0, 30);
}
System.out.println(Arrays.toString(array));
bubbleSort(array);
System.out.println(Arrays.toString(array));
}
//自定義方法,實現對指定的數組進行冒泡排序
//排序優化,如果待排序區已經是一個有序的序列,就不需要再排序了。
//如何知道待排序區是一個有序的序列。
public?static?void?bubbleSort(int[] array){
for(int?i=0;i
//判斷待排序區是否是一個有序的序列
boolean?flag?= false;
for(int?j=0;j
if(array[j] > array[j+1]){
int?temp?= array[j];
array[j] = array[j+1];
array[j+1] = temp;
flag?= true;
}
}
//在整個內層循環中都沒有執行到?flag=true,說明?判斷條件都沒有成立。
//說明待排序區是一個有序的序列,直接結束方法即可。
if(!flag){
return;
}
}
}
public?static?void?test2(){
Student[] students?= new?Student[10];
for(int?i=0;i
students[i] = new?Student(MyUtil.getRandomNumber(10, 19), MyUtil.getRandomNumber(60, 101), "佩奇");
}
System.out.println(Arrays.toString(students));
bubbleSort(students);
System.out.println(Arrays.toString(students));
}
/**
?* 根據學生的分數升序排列
?* @param?array
?*/
public?static?void?bubbleSort(Student[] array){
for(int?i=0;i
boolean?flag?= false;
for(int?j=0;j
if(array[j].getScore() > array[j+1].getScore()){
Student temp?= array[j];
array[j] = array[j+1];
array[j+1] = temp;
flag?= true;
}
}
//在整個內層循環中都沒有執行到?flag=true,說明?判斷條件都沒有成立。
//說明待排序區是一個有序的序列,直接結束方法即可。
if(!flag){
return;
}
}
}
}
//測試的Student類
class?Student{
private?int?age;
private?int?score;
private?String name;
Student(int?age, int?score, String name) {
super();
this.age?= age;
this.score?= score;
this.name?= name;
}
public?Student() {
}
public?int?getAge() {
return?age;
}
public?void?setAge(int?age) {
this.age?= age;
}
public?int?getScore() {
return?score;
}
public?void?setScore(int?score) {
this.score?= score;
}
public?String getName() {
return?name;
}
public?void?setName(String name) {
this.name?= name;
}
@Override
public?String toString() {
return?"\nStudent [age="?+ age?+ ", score="?+ score?+ ", name="?+ name?+ "]";
}
}
四、自定義內部比較器
//定義接口使類數組中需要比較的對象自身擁有比較的方法功能
import?java.util.Arrays;
import?com.bjsxt.util.MyUtil;
/**
?* 內部比較器練習
?*
?*/
public?class?TestComparable {
public?static?void?main(String[] args) {
test();
}
/**
?* 根據學生的分數升序排列
?* 內部比較器
?* @param?array
?*/
public?static?void?bubbleSort(Student[] array){
for(int?i=0;i
boolean?flag?= false;
for(int?j=0;j
if(array[j].compareTo(array[j+1]) > 0){
Student temp?= array[j];
array[j] = array[j+1];
array[j+1] = temp;
flag?= true;
}
}
//在整個內層循環中都沒有執行到?flag=true,說明?判斷條件都沒有成立。
//說明待排序區是一個有序的序列,直接結束方法即可。
if(!flag){
return;
}
}
}
public?static?void?test(){
Student[] students?= new?Student[10];
for(int?i=0;i
students[i] = new?Student(MyUtil.getRandomNumber(10, 19), MyUtil.getRandomNumber(60, 101), "佩琪");
}
System.out.println(Arrays.toString(students));
bubbleSort(students);
System.out.println(Arrays.toString(students));
}
}
//使用接口實現---內部比較器
//定義接口,描述比較功能--當前對象和傳入的對象兩者比較大小。
interface?MyComparable{
/**
?* 用于比較當前對象和?o 誰大誰小。
?* @param?o 用于比較的對象
?* @return??如果當前對象比o大,返回正數,如果比o 小,返回負數,否則返回0
?*/
int?compareTo(Object o);
}
class?Student implements?MyComparable{
private?int?age;
private?int?score;
private?String name;
Student(int?age, int?score, String name) {
super();
this.age?= age;
this.score?= score;
this.name?= name;
}
public?Student() {
}
public?int?getAge() {
return?age;
}
public?void?setAge(int?age) {
this.age?= age;
}
public?int?getScore() {
return?score;
}
public?void?setScore(int?score) {
this.score?= score;
}
public?String getName() {
return?name;
}
public?void?setName(String name) {
this.name?= name;
}
@Override
public?String toString() {
return?"\nStudent [age="?+ age?+ ", score="?+ score?+ ", name="?+ name?+ "]";
}
@Override
public?int?compareTo(Object o) {
//強轉成Student類型
Student s?= (Student)o;
int?result?= this.age?- s.age;
if(result?== 0){
return?this.score-s.score;
}
return?result;
}
}
五、自定義外部比較器
import?java.util.Arrays;
import?com.bjsxt.util.MyUtil;
/**
?* 外部比較器練習
?*
?*/
public?class?TestComparator {
public?static?void?main(String[] args) {
test();
}
public?static?void?test(){
Student[] students?= new?Student[10];
for(int?i=0;i
students[i] = new?Student(MyUtil.getRandomNumber(10, 19), MyUtil.getRandomNumber(60, 101), "佩琪");
}
System.out.println(Arrays.toString(students));
AgeComparator ageComparator?= new?AgeComparator();
bubbleSort(students,ageComparator);
System.out.println(Arrays.toString(students));
}
/**
?* 根據學生的分數升序排列
?* 外部比較器
?* @param?array
?*/
public?static?void?bubbleSort(Student[] array, MyComparator comparator){
for(int?i=0;i
boolean?flag?= false;
for(int?j=0;j
//if(array[j].compareTo(array[j+1]) > 0){
if(comparator.compare(array[j], array[j+1]) > 0){
Student temp?= array[j];
array[j] = array[j+1];
array[j+1] = temp;
flag?= true;
}
}
//在整個內層循環中都沒有執行到?flag=true,說明?判斷條件都沒有成立。
//說明待排序區是一個有序的序列,直接結束方法即可。
if(!flag){
return;
}
}
}
}
//外部比較器接口
interface?MyComparator{
/**
?* 用于比較兩個對象的大小
?* @param?o1 比較對象?1
?* @param?o2 比較對象?2
?* @return?如果o1 大于?o2 返回?正數,?否則?返回?負數?,相等返回?0
?*/
int?compare(Object o1, Object o2);
}
/**
* 根據學生年齡進行排序的外部比較器
*/
class?AgeComparator implements?MyComparator{
@Override
public?int?compare(Object o1, Object o2) {
Student s1?= (Student)o1;
Student s2?= (Student)o2;
return?s1.getAge()-s2.getAge();
}
}
/**
* 根據學生成績進行排序的外部比較器
*/
class?ScoreComparator implements?MyComparator{
@Override
public?int?compare(Object o1, Object o2) {
Student s1?= (Student)o1;
Student s2?= (Student)o2;
return?s1.getScore()-s2.getScore();
}
}
class?Student{
private?int?age;
private?int?score;
private?String name;
Student(int?age, int?score, String name) {
super();
this.age?= age;
this.score?= score;
this.name?= name;
}
public?Student() {
}
public?int?getAge() {
return?age;
}
public?void?setAge(int?age) {
this.age?= age;
}
public?int?getScore() {
return?score;
}
public?void?setScore(int?score) {
this.score?= score;
}
public?String getName() {
return?name;
}
public?void?setName(String name) {
this.name?= name;
}
@Override
public?String toString() {
return?"\nStudent [age="?+ age?+ ", score="?+ score?+ ", name="?+ name?+ "]";
}
}
?