代碼以下:↓↓↓javascript
package test1; public class Employee implements Cloneable { private String name; private double salary; public Employee(String name,double salary){//構造函數 super(); this.name=name; this.salary=salary; } @Override public String toString() { return "Employee [name=" + name + ", salary=" + salary + "]"; } public Employee clone() throws CloneNotSupportedException{//覆蓋clone方法,聲明爲public Employee cloned=(Employee)super.clone(); return cloned; } }
package test2; import test1.*; public class TestProtected { public static void main(String[] args){ Employee employee1=new Employee("chenjinxia",100.00); Employee employee2=employee1.clone(); System.out.println(employee1); System.out.println(employee2); } }
不能成功,會報錯。根據提示修改html
... public class TestProtected { public static void main(String[] args) throws CloneNotSupportedException{ Employee employee1=new Employee("chenjinxia",100.00); ...
就能夠編譯成功。↓↓↓
java
clone()方法,通常要用public來修飾,若是用protected來修飾,就會對test1包外不可見,那樣test2的就調用不了。數組
要先實現Cloneable接口,並聲明爲public,並且這個是淺複製,如要深複製,要對其全部引用型屬性進行復制。安全
首先咱們查看一下源代碼:↓↓↓
能夠看出clone()方法是來自Object的,是public native修飾的,這樣可使clone()方法必須經過實現接口或者繼承父類進行覆蓋才能實現,使clone()方法更加安全和嚴謹。ide
使用匿名類:函數
Comparator<PersonSortable2> NameComparator = new Comparator<PersonSortable2>(){ @Override public int compare(PersonSortable2 o1, PersonSortable2 o2) { return o1.getName().compareTo(o2.getName()); } }; Comparator<PersonSortable2> AgeComparator = new Comparator<PersonSortable2>(){ @Override public int compare(PersonSortable2 o1, PersonSortable2 o2) { if (o1.getAge() < o2.getAge()) { return -1; } else if (o1.getAge() > o2.getAge()) { return 1; } else { return 0; } } };
使用Lambda表達式:學習
Comparator<PersonSortable2> NameComparator=(o1,o2)-> o1.getName().compareTo(o2.getName()); Comparator<PersonSortable2> AgeComparator=(o1,o2)-> o1.getAge()-o2.getAge();
運行結果:
this
Comparator<Shape> shapeComparator = new Comparator<Shape>() { @Override public int compare(Shape o1, Shape o2) { //你的代碼 } };
shapeComparator實現了Comparator接口,使用匿名內部類的寫法,重寫了compare方法。code
實驗總結:建立MyStarter對象時,題目要求:MyStarter類的構造函數public MyStarter(ActionListener ac)要接收ActionListener類型的對象,因此咱們應該這樣建立:ActionListener ac = new ActionListener()
,而後再這個內部類裏重寫方法。
好處:使代碼更加緊湊,方便簡潔,且能夠隱藏你不想讓別人知道的操做。
可參考:http://www.cnblogs.com/kyxyes/archive/2013/02/18/2916292.html
a.總結:push()、pop()及empty()要注意判斷當棧爲空的狀況,不然會出現數組越界的錯誤。
b.定義IntegerStack接口,要求不一樣可是一些方法相同,就能夠實現這個接口,讓代碼拓展性更高,便於維護和開展新功能適應新平臺。
雖然邏輯關係上內部類聲明在一個類的內部,可是但願其保證持續存在,可以給別的別的類隨時調用的類,能夠聲明爲靜態內部類。
非靜態內部類是附屬在外部類對象上的,須要先實例化一個外部類的對象,經過外部類對象才能實例化非靜態內部類;而靜態內部類能夠看作是直接附屬在外部類上的,這個靜態表明附屬體是外部類,而不是外部類實例;外部類在進程中是惟一的,而靜態內部類不須要惟一,能夠生成多個實例。
前提條件:要實現接口Comparable,並重寫方法。
參考Case-StudentDao.zip案例
假設在不一樣的實現中,購物車裏的商品有的是存放在ArrayList,有的是存放在數組中。
public interface CarDao{ public double getTotal(); public boolean removeGoods(Goods goods); public boolean addGoods(Goods goods,int num); }
列表:
//陳錦霞201621123061 ArrayList<Goods> shoppinglist = new ArrayList<>(); public double getTotal();{//總價格 double total=0; for(int i=0;i<shoppinglist.size();i++){ total += shoppinglist.get(i).getNum() * shoppinglist.get(i).getPrice(); } return total; } public boolean removeGoods(Goods goods) {//刪除商品 for(int i=0;i<shoppinglist.size();i++){ if(shoppinglist.contains(goods)) { shoppinglist.remove(goods); return true; } else return false; } } public boolean addGoods(Goods goods,int num){//添加商品 for(int i=0;i<shoppinglist.size();i++){ if(shoppinglist.add(goods)){ num++; return true; } } }
數組:
CartList[] shoppinglist; public double getTotal();{//總價格 double total=0; for(int i=0;i<shoppinglist.length;i++){ total += shoppinglist.get(i).getNum() * shoppinglist.get(i).getPrice(); } return total; } public boolean removeGoods(Goods goods){//刪除商品 for(int i=0;i<shopping.length;i++){ if(shoppinglist.contains(goods)) { shoppinglist.remove(goods); return true; } else return false; } } public boolean addGoods(Goods goods,int num){//添加商品 for(int i=0;i<shopping.length;i++){ if(shoppinglist.add(goods)){ num++; return true; } } }
public class Main{ public static void main(String[] args) { Goods[] goods = new Goods[2]; goods[0]=new Goods("機械鍵盤",598,1); goods[1]=new Goods("java課本",89,1); GoodsDao sdm = new GoodsDaoArrayImpl(50);//使用數組實現 //GoodsDao sdm = new GoodsDaoListImpl();//使用列表實現 System.out.println("===========計算總價格========"); for(Goods e:goods){ if(sdm.getTotal(e)){ System.out.println("%f",total); } } System.out.println("===========添加商品========"); for(Goods e:goods){ if(!sdm.addGoods(e)){ System.out.println("添加商品失敗!"); }else{ System.out.println("添加商品成功!"); } System.out.println("===========刪除商品========"); for(Goods e:goods){ if(!sdm.removeGoods(e)){ System.out.println("刪除商品失敗!"); }else{ System.out.println("刪除商品成功!"); } }
DAO(Data Access Object)接口是一個數據訪問接口,當咱們實現了這個接口,咱們能夠經過這個接口來編寫不一樣的類,使用不一樣的方法來實現數據的存儲。
將四個實例按年齡從小到大排序並輸出結果。
不行。要用super()來調用父類的構造函數,若是去掉就會出現Implicit super constructor Person() is undefined. Must explicitly invoke another constructor
的報錯信息。
對五個類中的toString()方法的重寫體現了多態性。
題集:jmu-Java-04-面向對象2-進階-多態接口內部類
在碼雲的項目中,依次選擇「統計-Commits歷史-設置時間段」, 而後搜索並截圖
須要有兩張圖(1. 排名圖。2.PTA提交列表圖)
須要將每週的代碼統計狀況融合到一張表中。
周次 | 行數 | 新增行數 | 文件數 | 新增文件數 |
---|---|---|---|---|
1 | 91 | 91 | 5 | 5 |
2 | 504 | 413 | 18 | 13 |
3 | 1092 | 588 | 28 | 10 |
5 | 1158 | 129 | 34 | 6 |
6 | 1539 | 381 | 40 | 6 |
7 | 2023 | 484 | 49 | 9 |