Java总结(叁)
實驗二?Java簡單類與對象
- 實驗目的
- 掌握類的定義,熟悉屬性、構造函數、方法的作用,掌握用類作為類型聲明變量和方法返回值;
- 理解類和對象的區別,掌握構造函數的使用,熟悉通過對象名引用實例的方法和屬性;
- 理解static修飾付對類、類成員變量及類方法的影響。
- 實驗內容
(1) 使用構造函數完成各屬性的初始賦值
(2) 使用get…()和set…()的形式完成屬性的訪問及修改
(3) 提供計算面積的getArea()方法和計算周長的getLength()方法、
實驗代碼:
public class Rectangle {
??? private double width;
??? private double height;
??? private String color;
??? public Rectangle(double width,double height,String color){
??????? this.width=width;
??????? this.height=height;
??????? this.color=color;
??? }
??? public double getWidth() {
??????? return width;
??? }
??? public void setWidth(double width) {
??????? this.width = width;
??? }
??? public double getHeight() {
??????? return height;
??? }
??? public void setHeight(double height) {
??????? this.height = height;
??? }
??? public String getColor() {
??????? return color;
??? }
??? public void setColor(String color) {
??????? this.color = color;
??? }
??? public double area(){
??????? double area=0;
??????? area=width*height;
??????? return area;
??? }
??? public double length(){
??????? double? length=0;
??????? length=(width+height)*2;
??????? return length;
??? }
???
??? public static void main (String[] args){
??????? Rectangle r1=new Rectangle(4.0,8.0,"red");
??????? Rectangle r2=new Rectangle(3.0,6.0,"green");
??????? if(r1.area()>r2.area()){
??????????? System.out.println(r1.getHeight()+","+r1.getWidth());
??????? }
??????? else{System.out.println(r2.getHeight()+","+r2.getWidth());
???????
??????? }
????? System.out.println(r1.area());
????? System.out.println(r2.area());
????? System.out.println(r1.length());
??????? System.out.println(r2.length());
??? }
}
運行截圖:
?
?
2:銀行的賬戶記錄Account有賬戶的唯一性標識(11個長度的字符和數字的組合),用戶的姓名,開戶日期,賬戶密碼(六位的數字,可以用0開頭),當前的余額。銀行規定新開一個賬戶時,銀行方面提供一個標識符、賬戶初始密碼123456,客戶提供姓名,開戶時客戶可以直接存入一筆初始賬戶金額,不提供時初始余額為0。定義該類,并要求該類提供如下方法:存款、取款、變更密碼、可以分別查詢賬戶的標識、姓名、開戶日期、當前余額等信息。
不會;get set 基本內容懂,操作起來一團糟
總結:
這周主要有關string類的學習,string類常用操作方法;
對象數組,了解了之后需要學習的(exception)異常;
包&對象數組;
還是有很多不足的地方,實驗二的第一題到還能寫出來,至于第二題對我來說就有點難度了,之后會在老師教的講解中解決這一問題,亦或是詢問同學
?
轉載于:https://www.cnblogs.com/lsy2380821-/p/11557879.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
- 上一篇: 第四周课程总结及实验报告
- 下一篇: Kaggle: House Prices