王之泰201771010131《面向对象程序设计(java)》第四周学习总结
王之泰201771010131《面向對象程序設計(java)》第四周學習總結
第一部分:理論知識學習部分
第四章
1.類與對象的基礎概念。
a.類(class)是構造對象的模板或藍圖。由類構造對象的過程稱為創建類的實例;
? b.對象:即數據,對象有三個特性——1.行為 2.狀態 3.標識。
2.類與對象的關系
a.類是對象,事物的描述和抽象,是具有相同屬性和行為的對象集合。對象則是該類事物的實例。
b.類是一個靜態的概念,類本身不攜帶任何數據。當沒有為類創建任何對象時,類本身不存在于內存空間中。對象是一個動態的概念。每一個對象都存在著有別于其它對象的屬于自己的獨特的屬性和行為。對象的屬性可以隨著它自己的行為而發生改變。
3.對象與對象變量的關系
a.Java中想使用對象就必須先構造對象,并指定其初始狀態。
4.通過實驗掌握了預定義類的基本使用方法,熟悉Math類、String類、math類、Scanner類、LocalDate類的常用API。
5.掌握用戶自定義類的語法規則,包括實例域、靜態域、構造器方法、更改器方法、訪問器方法、靜態方法、main方法、方法參數的定義要求
a.實例域:可將實例域定義為final,構建對象時必須初始化這樣的域。
b.靜態域:絕大多數面向對象程序設計語言中,靜態域被稱為類域。如果將域定義為static,每個類中只有一個這樣的域。而每個對象對于所有的實例域卻都有自己的一份拷貝。
c.靜態方法:靜態方法是一種不能向對象實時操作的方法。可以使用對象調用靜態方法。
d.構造器方法:構造器與類同名。構造器總是伴隨著new操作符的執行被調用,而不能對一個已經存在的對象調用構造器來達到重新設置實例域的目的。
e.更改器方法:調用更改器方法后對象的狀態會改變。
f.訪問器方法:只訪問對象而不修改對象的方法。
g.main方法:main方法不對任何對象進行操作。靜態的main方法將執行并創建程序所需要的對象。
?
6.重載
多個方法有相同的名字、不同的參數、便產生了重載。Java允許重載任何方法,而不只是構造器方法。
7.包
? ? ? ?Java允許使用包將類組織起來。借助包可以方便地組織自己的代碼,并將自己的代碼與別人提供的代碼庫分開管理。而且使用包可以確保類名的唯一性。
8.文檔注釋技術
a.類注釋
b.方法注釋
c.域注釋
d.通用注釋
e.包與概述注釋
??第二部分:實驗部分
1、實驗目的與要求
(1) 理解用戶自定義類的定義;
(2) 掌握對象的聲明;
(3) 學會使用構造函數初始化對象;
(4) 使用類屬性與方法的使用掌握使用;
(5) 掌握package和import語句的用途。
2、實驗內容和步驟
實驗1 測試以下程序,掌握文件輸入輸出程序設計技術(文件輸入輸出,教材61-62)
示例代碼:
1 import java.io.*; 2 import java.util.*; 3 public class FileWriteReadTest { 4 public static void main(String[] args) throws IOException{ 5 //寫入文件演示 6 PrintWriter out = new PrintWriter("myfile.txt"); 7 out.println("姓名 高數 Java 數據結構 平均成績 總成績"); 8 out.println("張三 20 30 40 0 0"); 9 out.println("李四 50 60 70 0 0"); 10 out.close();//輸出完畢,需要close 11 //讀入文件演示 12 Scanner in = new Scanner(new File("myfile.txt"));//為myfile.txt這個File創建一個掃描器in 13 int number = 1;//行號 14 System.out.println(in.nextLine()); 15 while(in.hasNextLine()){//判斷掃描器是否還有下一行未讀取,該循環把文件的每一行都讀出 16 String line = in.nextLine();//讀出myfile.txt的下一行 17 System.out.print("第"+(++number)+"行的內容: "); 18 Scanner linescanner = new Scanner(line);//行內容建立掃描器 19 linescanner.useDelimiter(" ");//使用空格作為分隔符 20 String name = linescanner.next(); 21 String math = linescanner.next(); 22 String java = linescanner.next(); 23 String ds = linescanner.next(); 24 String avg = linescanner.next(); 25 String total = linescanner.next(); 26 System.out.println("name="+name+" math="+math+" java="+java+" ds="+ds+" avg"+avg+" total="+total); 27 } 28 in.close();//讀入完畢,最后需要對其進行close。 29 } 30 }文件輸出結果如下:
實驗2 導入第4章示例程序并測試。
測試程序1:
編輯、編譯、調試運行程序4-2(教材104頁);
結合程序運行結果,掌握類的定義與類對象的用法,并在程序代碼中添加類與對象知識應用的注釋;
嘗試在項目中編輯兩個類文件(Employee.java、 EmployeeTest.java ),編譯并運行程序。
程序4-2如下:
1 import java.time.*; 2 3 /** 4 * This program tests the Employee class. 5 * @version 1.12 2015-05-08 6 * @author Cay Horstmann 7 */ 8 public class EmployeeTest 9 { 10 public static void main(String[] args) 11 { 12 // fill the staff array with three Employee objects 13 Employee[] staff = new Employee[3]; 14 15 staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15); 16 staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1); 17 staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15); 18 19 // raise everyone's salary by 5% 20 for (Employee e : staff) 21 e.raiseSalary(5); 22 23 // print out information about all Employee objects 24 for (Employee e : staff) 25 System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay=" 26 + e.getHireDay()); 27 } 28 } 29 30 class Employee 31 { 32 private String name; 33 private double salary; 34 private LocalDate hireDay; 35 36 public Employee(String n, double s, int year, int month, int day) 37 { 38 name = n; 39 salary = s; 40 hireDay = LocalDate.of(year, month, day); 41 } 42 43 public String getName() 44 { 45 return name; 46 } 47 48 public double getSalary() 49 { 50 return salary; 51 } 52 53 public LocalDate getHireDay() 54 { 55 return hireDay; 56 } 57 58 public void raiseSalary(double byPercent) 59 { 60 double raise = salary * byPercent / 100; 61 salary += raise; 62 } 63 }程序運行結果如下:
EmployeeTest.java
1 package test; 2 3 public class EmployeeTest { 4 public static void main(String[] args) 5 { 6 // fill the staff array with three Employee objects 7 Employee[] staff = new Employee[3]; 8 9 staff[0] = new Employee("Carl Cracker", 75000, 1987, 12, 15); 10 staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1); 11 staff[2] = new Employee("Tony Tester", 40000, 1990, 3, 15); 12 13 // raise everyone's salary by 5% 14 for (Employee e : staff) 15 e.raiseSalary(5); 16 17 // print out information about all Employee objects 18 for (Employee e : staff) 19 System.out.println("name=" + e.getName() + ",salary=" + e.getSalary() + ",hireDay=" 20 + e.getHireDay()); 21 } 22 }Employee.java
package test;import java.time.LocalDate;class Employee {private String name;private double salary;private LocalDate hireDay;public Employee(String n, double s, int year, int month, int day){name = n;salary = s;hireDay = LocalDate.of(year, month, day);}public String getName(){return name;}public double getSalary(){return salary;}public LocalDate getHireDay(){return hireDay;}public void raiseSalary(double byPercent){double raise = salary * byPercent / 100;salary += raise;} }參考教材104頁EmployeeTest.java,設計StudentTest.java,定義Student類,包含name(姓名)、sex(性別)、javascore(java成績)三個字段,編寫程序,從鍵盤輸入學生人數,輸入學生信息。
?
按以下表頭輸出學生信息表:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?姓名????? 性別???? java成績
?程序代碼如下:
1 import java.util.Scanner; 2 3 public class tudent { 4 String name; 5 String sex; 6 double javascore; 7 public static void main(String[] args) { 8 System.out.println("請輸入學生人數"); 9 Scanner sc = new Scanner(System.in); 10 int totalStudent = sc.nextInt(); 11 tudent[] stus = new tudent[totalStudent]; 12 for(int i=0;i<totalStudent;i++){ 13 tudent s = new tudent(); 14 stus[i]=s; 15 System.out.println("請輸入第"+(i+1)+"個學生的姓名"); 16 s.name = sc.next(); 17 System.out.println("請輸入第"+(i+1)+"個學生的性別"); 18 s.sex = sc.next(); 19 System.out.println("請輸入第"+(i+1)+"個學生的Java成績"); 20 s.javascore = sc.nextDouble(); 21 } 22 printtudents(stus); 23 sc.close(); 24 } 25 26 public static void printtudents(tudent[] s){ 27 System.out.println("姓名\t性別\tJava成績"); 28 for(int i=0;i<s.length;i++){ 29 System.out.println(s[i].name+"\t"+s[i].sex+"\t"+s[i].javascore); 30 } 31 } 32 }程序運行結果如下:
?
測試程序2:
編輯、編譯、調試運行程序4-3(教材116);
結合程序運行結果,理解程序代碼,掌握靜態域(netxtId)與靜態方法(getNextId)的用法,在相關代碼后添加注釋;
理解Java單元(類)測試的技巧。
程序4-3如下
1 /** 2 * This program demonstrates static methods. 3 * @version 1.01 2004-02-19 4 * @author Cay Horstmann 5 */ 6 public class StaticTest 7 { 8 public static void main(String[] args) 9 { 10 // fill the staff array with three Employee objects 11 Employee[] staff = new Employee[3]; 12 13 staff[0] = new Employee("Tom", 40000); 14 staff[1] = new Employee("Dick", 60000); 15 staff[2] = new Employee("Harry", 65000); 16 17 // print out information about all Employee objects 18 for (Employee e : staff) 19 { 20 e.setId(); 21 System.out.println("name=" + e.getName() + ",id=" + e.getId() + ",salary=" 22 + e.getSalary()); 23 } 24 25 int n = Employee.getNextId(); // calls static method 26 System.out.println("Next available id=" + n); 27 } 28 } 29 30 class Employee 31 { 32 private static int nextId = 1; 33 34 private String name; 35 private double salary; 36 private int id; 37 38 public Employee(String n, double s) 39 { 40 name = n; 41 salary = s; 42 id = 0; 43 } 44 45 public String getName() 46 { 47 return name; 48 } 49 50 public double getSalary() 51 { 52 return salary; 53 } 54 55 public int getId() 56 { 57 return id; 58 } 59 60 public void setId() 61 { 62 id = nextId; // set id to next available id 63 nextId++; 64 } 65 66 public static int getNextId() 67 { 68 return nextId; // returns static field 69 } 70 71 public static void main(String[] args) // unit test 72 { 73 Employee e = new Employee("Harry", 50000); 74 System.out.println(e.getName() + " " + e.getSalary()); 75 } 76 }程序運行結果如下:
測試程序3:
編輯、編譯、調試運行程序4-4(教材121);
結合程序運行結果,理解程序代碼,掌握掌握Java方法參數的用法,在相關代碼后添加注釋;
程序4-4如下
1 /** 2 * This program demonstrates parameter passing in Java. 3 * @version 1.00 2000-01-27 4 * @author Cay Horstmann 5 */ 6 public class ParamTest 7 { 8 public static void main(String[] args) 9 { 10 /* 11 * Test 1: Methods can't modify numeric parameters 12 */ 13 System.out.println("Testing tripleValue:"); 14 double percent = 10; 15 System.out.println("Before: percent=" + percent); 16 tripleValue(percent); 17 System.out.println("After: percent=" + percent); 18 19 /* 20 * Test 2: Methods can change the state of object parameters 21 */ 22 System.out.println("\nTesting tripleSalary:"); 23 Employee harry = new Employee("Harry", 50000); 24 System.out.println("Before: salary=" + harry.getSalary()); 25 tripleSalary(harry); 26 System.out.println("After: salary=" + harry.getSalary()); 27 28 /* 29 * Test 3: Methods can't attach new objects to object parameters 30 */ 31 System.out.println("\nTesting swap:"); 32 Employee a = new Employee("Alice", 70000); 33 Employee b = new Employee("Bob", 60000); 34 System.out.println("Before: a=" + a.getName()); 35 System.out.println("Before: b=" + b.getName()); 36 swap(a, b); 37 System.out.println("After: a=" + a.getName()); 38 System.out.println("After: b=" + b.getName()); 39 } 40 41 public static void tripleValue(double x) // doesn't work 42 { 43 x = 3 * x; 44 System.out.println("End of method: x=" + x); 45 } 46 47 public static void tripleSalary(Employee x) // works 48 { 49 x.raiseSalary(200); 50 System.out.println("End of method: salary=" + x.getSalary()); 51 } 52 53 public static void swap(Employee x, Employee y) 54 { 55 Employee temp = x; 56 x = y; 57 y = temp; 58 System.out.println("End of method: x=" + x.getName()); 59 System.out.println("End of method: y=" + y.getName()); 60 } 61 } 62 63 class Employee // simplified Employee class 64 { 65 private String name; 66 private double salary; 67 68 public Employee(String n, double s) 69 { 70 name = n; 71 salary = s; 72 } 73 74 public String getName() 75 { 76 return name; 77 } 78 79 public double getSalary() 80 { 81 return salary; 82 } 83 84 public void raiseSalary(double byPercent) 85 { 86 double raise = salary * byPercent / 100; 87 salary += raise; 88 } 89 }程序運行結果如下:
測試程序4:
編輯、編譯、調試運行程序4-5(教材129);
結合程序運行結果,理解程序代碼,掌握Java用戶自定義類的用法,掌握對象構造方法及對象使用方法,在相關代碼后添加注釋。
程序4-5如下:
1 import java.util.*; 2 3 /** 4 * This program demonstrates object construction. 5 * @version 1.01 2004-02-19 6 * @author Cay Horstmann 7 */ 8 public class ConstructorTest 9 { 10 public static void main(String[] args) 11 { 12 // fill the staff array with three Employee objects 13 Employee[] staff = new Employee[3]; 14 15 staff[0] = new Employee("Harry", 40000); 16 staff[1] = new Employee(60000); 17 staff[2] = new Employee(); 18 19 // print out information about all Employee objects 20 for (Employee e : staff) 21 System.out.println("name=" + e.getName() + ",id=" + e.getId() + ",salary=" 22 + e.getSalary()); 23 } 24 } 25 26 class Employee 27 { 28 private static int nextId; 29 30 private int id; 31 private String name = ""; // instance field initialization 32 private double salary; 33 34 // static initialization block 35 static 36 { 37 Random generator = new Random(); 38 // set nextId to a random number between 0 and 9999 39 nextId = generator.nextInt(10000); 40 } 41 42 // object initialization block 43 { 44 id = nextId; 45 nextId++; 46 } 47 48 // three overloaded constructors 49 public Employee(String n, double s) 50 { 51 name = n; 52 salary = s; 53 } 54 55 public Employee(double s) 56 { 57 // calls the Employee(String, double) constructor 58 this("Employee #" + nextId, s); 59 } 60 61 // the default constructor 62 public Employee() 63 { 64 // name initialized to ""--see above 65 // salary not explicitly set--initialized to 0 66 // id initialized in initialization block 67 } 68 69 public String getName() 70 { 71 return name; 72 } 73 74 public double getSalary() 75 { 76 return salary; 77 } 78 79 public int getId() 80 { 81 return id; 82 } 83 }程序運行結果如下:
測試程序5:
編輯、編譯、調試運行程序4-6、4-7(教材135);
結合程序運行結果,理解程序代碼,掌握Java包的定義及用法,在相關代碼后添加注釋;
程序4-6如下:
1 import com.horstmann.corejava.*; 2 // the Employee class is defined in that package 3 4 import static java.lang.System.*; 5 6 /** 7 * This program demonstrates the use of packages. 8 * @version 1.11 2004-02-19 9 * @author Cay Horstmann 10 */ 11 public class PackageTest 12 { 13 public static void main(String[] args) 14 { 15 // because of the import statement, we don't have to use 16 // com.horstmann.corejava.Employee here 17 Employee harry = new Employee("Harry Hacker", 50000, 1989, 10, 1); 18 19 harry.raiseSalary(5); 20 21 // because of the static import statement, we don't have to use System.out here 22 out.println("name=" + harry.getName() + ",salary=" + harry.getSalary()); 23 } 24 }程序運行結果如下:
程序4-7如下:
1 package com.horstmann.corejava; 2 3 // the classes in this file are part of this package 4 5 import java.time.*; 6 7 // import statements come after the package statement 8 9 /** 10 * @version 1.11 2015-05-08 11 * @author Cay Horstmann 12 */ 13 public class Employee 14 { 15 private String name; 16 private double salary; 17 private LocalDate hireDay; 18 19 public Employee(String name, double salary, int year, int month, int day) 20 { 21 this.name = name; 22 this.salary = salary; 23 hireDay = LocalDate.of(year, month, day); 24 } 25 26 public String getName() 27 { 28 return name; 29 } 30 31 public double getSalary() 32 { 33 return salary; 34 } 35 36 public LocalDate getHireDay() 37 { 38 return hireDay; 39 } 40 41 public void raiseSalary(double byPercent) 42 { 43 double raise = salary * byPercent / 100; 44 salary += raise; 45 } 46 }實驗3 ?編寫長方形類Rectangle與圓形類Circle,其中Rectangle類設置私有屬性:width,length;Circle類設置私有屬性radius。編寫Rectangle類的帶參構造函數Rectangle(int width,int length), Circle類的帶參構造函數Circle(int radius),編寫兩個類的toString方法(Eclipse可自動生成)。上述2個類均定義以下方法:
求周長的方法public int getPerimeter()
求面積的方法public int getArea()
在main方法中完成以下任務:
(1)? 輸入1行長與寬,創建一個Rectangle對象;
(2)? 輸入1行半徑,創建一個Circle對象;
(3)? 將兩個對象的周長加總輸出,將兩個對象的面積加總輸出。
程序如下:
1 import java.util.*; 2 3 public class shapecount { 4 5 public static void main(String[] args) { 6 Scanner in = new Scanner(System.in); 7 System.out.println("輸入長:"); 8 double length = in.nextDouble(); 9 System.out.println("輸入寬:"); 10 double width = in.nextDouble(); 11 System.out.println("輸入半徑:"); 12 double radius = in.nextDouble(); 13 Rectangle a=new Rectangle(length,width); 14 Circle b=new Circle(radius); 15 System.out.println("矩形周長:"+a.getPerimeter()+"矩形面積:"+a.getArea()); 16 System.out.println("圓周長"+b.getPerimeter()+"圓面積:"+b.getArea()); 17 double c = a.getPerimeter()+b.getPerimeter(); 18 double d = a.getArea()+b.getArea(); 19 System.out.println("周長和:"+c+"面積和:"+d); 20 } 21 22 } 23 24 25 class Rectangle { 26 private double width; 27 private double length; 28 public Rectangle(double w,double l) 29 { 30 width=w; 31 length=l; 32 } 33 public double getPerimeter() 34 { 35 double Perimeter = (width+length)*2; 36 return Perimeter; 37 } 38 public double getArea() 39 { 40 double Area = width*length; 41 return Area; 42 } 43 } 44 45 class Circle { 46 47 private double radius; 48 double PI = 3.14; 49 public Circle(double r) 50 { 51 radius=r; 52 } 53 public double getPerimeter() 54 { 55 double Perimeter = 2*PI*radius; 56 return Perimeter; 57 } 58 public double getArea() 59 { 60 double Area = PI*radius*radius; 61 return Area; 62 } 63 }程序結果如下:
?第二部分:總結
本周自學了第四章,通過這周的學習,我掌握了預定義類的基本使用方法,如Math類、String類、math類、Scanner類、LocalDate類等常用API;大致掌握了用戶自定義類的語法規則,如實例域、靜態域、構造器方法、更改器方法、訪問器方法、靜態方法、main方法、方法參數的定義要求等。
但還是有些不足,應該是概念理解不夠深刻。上課實驗時老師帶著我們完成了文件的讀寫,更重要的是教會了我們正確的讀別人代碼的方法,作為一個優秀的猿和農,應該具備能寫出高質量的代碼和讀懂別人代碼的能力。在課后自學時間里,完成了剩余的實驗任務。在運行示例代碼的過程中不僅學會了相關知識,還更加規范了自己的編碼風格。學習的一些漏洞和疑惑,在看翁愷老師的視頻時解決了部分,但還是感覺自己有著很多的欠缺,在以后的學習中會更加努力。總體來講這個中秋過得很充實!!!?
轉載于:https://www.cnblogs.com/hackerZT-7/p/9692454.html
總結
以上是生活随笔為你收集整理的王之泰201771010131《面向对象程序设计(java)》第四周学习总结的全部內容,希望文章能夠幫你解決所遇到的問題。