1. 程式人生 > 其它 >說說Spring框架中Bean的生命週期

說說Spring框架中Bean的生命週期

1)前言:

     題目集四前言:

     題目集一共有3個題目,第一題難度較簡單,相對於來說應該是過渡題,考察的主要是正則運算,對字串中數字的識別以及對數字的加減。第二題題目較難,緊接題目集三的幾道題,難度增強,分析點增多,該題考察的重點主要是合法字元的判斷讀取以及之後根據題目要求來對資料分析計算,同樣與使用到了正則運算子,並運用了.replace(" " " ")和.split(" ")和Double.parseDouble(]);將目標字串中不需要的字元去除後進行分割,並將切割後的字串讀取轉化為合法數字然後進行計算。第三題難度中等,開始大範圍使用類來簡化程式碼,使程式碼更加簡單清晰,便於後期的刪改檢查,主要問題是剛開始進行類的使用時不是很熟練導致耗時過長。

     期中考試題目集前言:

     題目集三共有三個題目,整體難度逐漸變難,相對於來說題目都是偏向於基礎題。第一題難度較易,簡單地使用類來設計程式碼,只需要按照題目要求來對類名進行定義,考點相對於來說是基礎問題。第二題同第一題類似,只是在原有的基礎上多加了一個類來定義題目要求的顏色,難度相對於來說也比較簡單。第三題

     三次題目集的題目的難度從易到難,但考察的知識點較多,大多都與字串的擷取有關,逐步向物件轉變。

     三次題目集的得分情況:

      題目集四:42分

      期中考試:68分

 (2)設計與分析

pta4 7-1(原始碼如下)

import java.util.Scanner;
import java.util.Arrays; public class Main{ public static void main(String[] args){ Scanner in=new Scanner(System.in); while(true){ String line=in.nextLine(); if(line.equals("end")) break; String[] split=line.split("\\D+");
int i; long sum=0; for(i=0;i<split.length;i++){ if(!split[i].equals("")){ double num=Double.parseDouble(split[i]);//字串強制轉換成double sum+=num; } } System.out.println(sum); } } }

此題是將字串中數字提取出來,所運用的正則表示式

String[] split=line.split("\\D+");
能夠將數字完整分割,最後在將字串強制轉化為double型,最後再進行計算,按要求逐行輸出計算結果。

pta4 7-2(原始碼如下)

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String z = input.nextLine();
        z=z.replace(","," ");
        z=z.replace(":"," ");
        double x1=0,x2=0,x3=0,x4=0,y1=0,y2=0,y3=0,y4=0;
        String k[]=z.split(" ");
        char s=z.charAt(0);
        if(s == '1') 
        {
               
                x1=Double.parseDouble(k[1]);
                y1=Double.parseDouble(k[2]);
                x2=Double.parseDouble(k[3]);
                y2=Double.parseDouble(k[4]);
                x3=Double.parseDouble(k[5]);
                y3=Double.parseDouble(k[6]);
                double a1=0;
                double a2=0;
                double a3=0;
                a1 = Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
                a2 = Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
                a3 = Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1));
                if(((a1==a2)&&(a1!=a3))||((a1==a3)&&(a1!=a2))||((a2==a3)&&(a2!=a1)))
                    {
                      System.out.println("true ");
                    }
                if(((a1==a2)&&(a2==a3)))
                    {
                      System.out.println("true");
                    }
        }
              //  else
               //  {
              //      System.out.println("Wrong Format");
             //   }
        
        if(s == '2') 
        {
            
                x1=Double.parseDouble(k[1]);
                y1=Double.parseDouble(k[2]);
                x2=Double.parseDouble(k[3]);
                y2=Double.parseDouble(k[4]);
                x3=Double.parseDouble(k[5]);
                y3=Double.parseDouble(k[6]);
                System.out.print(Math.abs(((y2-y3)*x1-(x2-x3)*y1+x2*y3-x3*y2)/(Math.sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2)))));
              
        }
        if(s == '3') 
        {
                x1=Double.parseDouble(k[1]);
                y1=Double.parseDouble(k[2]);
                x2=Double.parseDouble(k[3]);
                y2=Double.parseDouble(k[4]);
                x3=Double.parseDouble(k[5]);
                y3=Double.parseDouble(k[6]);
                double a1=0;
                double a2=0;
                a1=(y2-y1)/(x2-x1);
                a2=(y3-y1)/(x3-x1);
                if(a1==a2)
                {
                System.out.println("true");
                }
                else
                {
                 System.out.println("false");
                }
        }
        if(s == '4') {
                x1=Double.parseDouble(k[1]);
                y1=Double.parseDouble(k[2]);
                x2=Double.parseDouble(k[3]);
                y2=Double.parseDouble(k[4]);
                x3=Double.parseDouble(k[5]);
                y3=Double.parseDouble(k[6]);
                x4=Double.parseDouble(k[7]);
                y4=Double.parseDouble(k[8]);
                double a1=0;
                double a2=0;
                double a3=0;
                a1 = (y2-y1)/(x2-x1);
                a2 = (y4-y3)/(x4-x3);
                //a3 = (y3-y2)/(x3-x2);
            if(a1==a2){
                System.out.println("true");
            }
            else{
                System.out.println("false");
            }
            
        }
        if(s == '5') {
                x1=Double.parseDouble(k[1]);
                y1=Double.parseDouble(k[2]);
                x2=Double.parseDouble(k[3]);
                y2=Double.parseDouble(k[4]);
                x3=Double.parseDouble(k[5]);
                y3=Double.parseDouble(k[6]);
                x4=Double.parseDouble(k[7]);
                y4=Double.parseDouble(k[8]);
        }
        System.out.println("outof the triangle");
    }
}

此題考察的是將給出的字串於所輸入的空格來進行分割,期間還穿插著合法數的判斷,之後根據逗號識別出相應的有關x,y軸的點位,從而分析出各個點的座標,再按照題目中的各種要求進行計算。

我的想法是先將所給字串分割出需要代入運算的資料部分,先進行合法性的判斷,如果資料合法,將會對字串中":"之前的資料進行提取,然後判斷執行那一段程式碼,所代入的資料利用.split()進行分割,再利用Double.parseDouble()將需要進行運算的資料存入以double型規定的x1,x2,x3,x4,y1,y2,y3,y4,其中字串會根據首個字元來進行不同的分割以及轉化存入相對應得x1,x2,x3,x4,y1,y2,y3,y4(x3,x4,y3,y4在執行相應的程式碼時才會需要),然後進行數學計算,將計算出的結果進行判斷,輸出對應的結果。

另外還有的方法則是利用第一題所給的線索,在資料合法的情況下用正則表示式去提取所需的資料,再判斷資料的位置,判斷字元的正負性,最後按要求編寫程式碼,將資料代入計算,從而達到目的。

pta4 7-3(原始碼如下)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String a=in.nextLine();
        String b[] = a.split("\\s");
        String c=in.nextLine();
        String d=in.nextLine();
        String e=in.nextLine();
        String f=in.nextLine();
        BankBusiness bankBusiness = new BankBusiness(b[0],b[1]);
        BankBusiness.welcome();
        bankBusiness.cun(c);
        bankBusiness.cun(d);
        bankBusiness.qu(e);
        bankBusiness.qu(f);
        BankBusiness.welcomeNext();
    }

static class BankBusiness {
    private String name;//客戶名稱
    private String password;//密碼
    double balance;
    public BankBusiness() {
    }

    public BankBusiness(String name, String password) {
        this.name = name;
        this.password = password;
    }
    
    public static String bankName = "中國銀行";
    
    public static void welcome() {
        System.out.println(bankName + "歡迎您的到來!");
    }
    public static void welcomeNext() {
        System.out.println("請收好您的證件和物品,歡迎您下次光臨!");
    }
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
  
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }
    
    public void cun(String string) {
        String[] a=string.split("\\s");
        if(!password.equals(a[0])) {
            System.out.println("您的密碼錯誤!");
        } else {
            double newBalance=Double.parseDouble(a[1]);
            balance = balance + newBalance;
            System.out.println("您的餘額有" + balance + "元。");
        }
    }
    public void qu(String string) {
        String[] a=string.split("\\s");
        if(!password.equals(a[0])) {
            System.out.println("您的密碼錯誤!");
        } else {
            double newBalance=Double.parseDouble(a[1]);
            if(newBalance > balance) {
                System.out.println("您的餘額不足!");
            } else if(newBalance == balance){
                balance = 0;
                System.out.println("請取走鈔票,您的餘額還有" + balance + "元。");
            } else {
                balance = balance - newBalance;
                System.out.println("請取走鈔票,您的餘額還有" + balance + "元。");
            }

        }
    }

    }
    
}

此題考察的是類,主要目的是讓我們更加清楚明白的看清楚使用類的好處,方法與第一題類似,開始出現物件的互動。

期中考試t1(原始碼如下)

import java.util.*;
 
public class Main {
 
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Point point1 = new Point(in.nextDouble(), in.nextDouble());
        Point point2 = new Point(in.nextDouble(), in.nextDouble());
        if(point1.x<=0||point1.x>200||point2.x<=0||point2.x>200||point1.y<=0||point1.y>200||point2.y<=0||point2.y>200)
            {
                System.out.println("Wrong Format");
            }
        else{
        String a=in.next();
        System.out.println("The line's color is:"+a);
        System.out.printf("The line's begin point's Coordinate is:\n");
        point1.display();
        System.out.printf("The line's end point's Coordinate is:\n");
        point2.display();
        Line line = new Line(point1,point2);
        line.getDistance();
        }
    }
}
 
 class Point {
    double x, y;
 
    Point() {
        x = 0;
        y = 0;
    }
 
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
    
    public double getX() 
  {
   return x;
  }
  public void setX(double x) 
  {
   this.x = x;
  }

  public double getY()
  {
   return y;
  }

  public void setY(double y) 
  {
   this.y = y;
  }
 
    public void display() {
        System.out.printf("(%.2f,%.2f)\n", x, y);
    }
    
}
class Line{
    Point point1;
      Point point2;
      public Line(Point p1,Point p2)
   {
    this.point1 = p1;
    this.point2 = p2;
   }
      public Point getPoint1() 
   {
    return point1;
   }
   public void setPoint1(Point point1) 
   {
    this.point1 = point1;
   }

   public Point getPoint2()
   {
    return point2;
   }
   public void setPoint2(Point point2) 
   {
    this.point2 = point2;
   }
   public void getDistance() 
   {
       double l;
    l = Math.sqrt((this.point1.getX()-this.point2.getX())*(this.point1.getX()-this.point2.getX())+(this.point1.getY()-this.point2.getY())*(this.point1.getY()-this.point2.getY()));
       System.out.print("The line's length is:");
       System.out.println(String.format("%.2f", l));
   }
}

此題較為簡單

我的想法是先建立一個點類,獲得點與點的座標,之後在創造一個線類,來計算點與點之間的距離,然後通過呼叫這個類來獲取運算過程給出結果,最後將結果給出並且獲取結果再按要求print。

期中考試t2(原始碼如下)

import java.util.*;
 
public class Main {
 
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Point point1 = new Point(in.nextDouble(), in.nextDouble());
        Point point2 = new Point(in.nextDouble(), in.nextDouble());
        Line line = new Line(point1,point2);
        String a=in.next();
        Plane plane = new Plane(a);
        if(point1.x<=0||point1.x>200||point2.x<=0||point2.x>200||point1.y<=0||point1.y>200||point2.y<=0||point2.y>200)
            {
                System.out.println("Wrong Format");
            }
        else{
        point1.display();
        point2.display();
        System.out.println("The line's color is:"+a);
        System.out.printf("The line's begin point's Coordinate is:\n");
        point1.display();
        System.out.printf("The line's end point's Coordinate is:\n");
        point2.display();
        line.getDistance();
        plane.display();
        }
    }
}
 
 class Point {
    double x, y;
 
    Point() {
        x = 0;
        y = 0;
    }
 
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
    
    public double getX() 
  {
   return x;
  }
  public void setX(double x) 
  {
   this.x = x;
  }

  public double getY()
  {
   return y;
  }

  public void setY(double y) 
  {
   this.y = y;
  }
 
    public void display() {
        System.out.printf("(%.2f,%.2f)\n", x, y);
    }
    
}
class Line{
    Point point1;
      Point point2;
      public Line(Point p1,Point p2)
   {
    this.point1 = p1;
    this.point2 = p2;
   }
      public Point getPoint1() 
   {
    return point1;
   }
   public void setPoint1(Point point1) 
   {
    this.point1 = point1;
   }

   public Point getPoint2()
   {
    return point2;
   }
   public void setPoint2(Point point2) 
   {
    this.point2 = point2;
   }
   public void getDistance() 
   {
       double l;
    l = Math.sqrt((this.point1.getX()-this.point2.getX())*(this.point1.getX()-this.point2.getX())+(this.point1.getY()-this.point2.getY())*(this.point1.getY()-this.point2.getY()));
       System.out.print("The line's length is:");
       System.out.println(String.format("%.2f", l));
   }
}
class Plane
     {
      String color;
      public Plane(String color)
      {
       this.color = color;
      }
      public String getColor()
   {
    return color;
   }
   public void setColor(String color) 
   {
    this.color = color;
   }
   public void display()
   {
    System.out.println("The Plane's color is:"+this.getColor());
   }
     }

第二題與第一題思路類似,只需要在第一題的基礎上加上一個讀取字串的類,將特定的一串字串讀取並且儲存,然後再按要求print。

期中考試t3(原始碼如下)

import java.util.*;
 
public class Main {
 
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Point point1 = new Point(in.nextDouble(), in.nextDouble());
        Point point2 = new Point(in.nextDouble(), in.nextDouble());
        Line line = new Line(point1,point2);
        String a=in.next();
        Plane plane = new Plane(a);
        if(point1.x<=0||point1.x>200||point2.x<=0||point2.x>200||point1.y<=0||point1.y>200||point2.y<=0||point2.y>200)
            {
                System.out.println("Wrong Format");
            }
        else{
        point1.display();
        point2.display();
        System.out.println("The line's color is:"+a);
        System.out.printf("The line's begin point's Coordinate is:\n");
        point1.display();
        System.out.printf("The line's end point's Coordinate is:\n");
        point2.display();
        line.getDistance();
        plane.display();
        }
    }
}
 
 class Point {
    double x, y;
 
    Point() {
        x = 0;
        y = 0;
    }
 
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
    
    public double getX() 
  {
   return x;
  }
  public void setX(double x) 
  {
   this.x = x;
  }

  public double getY()
  {
   return y;
  }

  public void setY(double y) 
  {
   this.y = y;
  }
 
    public void display() {
        System.out.printf("(%.2f,%.2f)\n", x, y);
    }
    
}
class Line{
    Point point1;
      Point point2;
      public Line(Point p1,Point p2)
   {
    this.point1 = p1;
    this.point2 = p2;
   }
      public Point getPoint1() 
   {
    return point1;
   }
   public void setPoint1(Point point1) 
   {
    this.point1 = point1;
   }

   public Point getPoint2()
   {
    return point2;
   }
   public void setPoint2(Point point2) 
   {
    this.point2 = point2;
   }
   public void getDistance() 
   {
       double l;
    l = Math.sqrt((this.point1.getX()-this.point2.getX())*(this.point1.getX()-this.point2.getX())+(this.point1.getY()-this.point2.getY())*(this.point1.getY()-this.point2.getY()));
       System.out.print("The line's length is:");
       System.out.println(String.format("%.2f", l));
   }
}
class Plane
     {
      String color;
      public Plane(String color)
      {
       this.color = color;
      }
      public String getColor()
   {
    return color;
   }
   public void setColor(String color) 
   {
    this.color = color;
   }
   public void display()
   {
    System.out.println("The Plane's color is:"+this.getColor());
   }
     }

第三題與第二題類似,在原有的基礎上多出來一個容器類,加入容器類,能夠是我們更好的對資料進行分析,更好的儲存點、線、面對象,並對該容器進行相應增、刪、遍歷操作。

(3)踩坑心得

心得(1)

String b[] = a.split("\\s");
        String c=in.nextLine();

在編譯的時候,如果使用split進行切割時,不能簡單地在引號中加入空格,不然會對下一行字串的讀取造成影響,很有可能讀取不了資料,在這一個方面困擾了很久,所以我們應該儘量按照標準要求來進行編碼,避免出現後續各種不易查出的小問題而影響自己的效率。

心得(2)

靜態方法可以直接通過類名呼叫,任何的例項也都可以呼叫,因此靜態方法中不能用this和super關鍵字,不能直接訪問所屬類的例項變數和例項方法

心得(3)

繼承的出現提高了程式碼的複用性。  繼承的出現讓類與類之間產生了關係,提供了多型的前提。

(4)改進建議

1.儘量分檔案寫程式碼,這樣能夠方便我們在日後的學習中檢查和修改程式碼。

2.在進行情況分析時,大多數情況下我都是用列舉法來完成,這一方法的問題在於程式程式碼過於複雜且重複程式碼過多,修改起來非常麻煩,在今後的編碼過程中,對於簡單地判斷我會分析列舉法和靈活的方法,判斷兩種方法的難易程度和簡潔程度來運用,簡化程式碼,挺高程式碼的利用效果,減少不必要的過程。

3.在之後的的程式碼儘量使用類來進行編寫,並且注重細節,避免以後在關鍵是出現不易出現的問題而影響實驗。

4.在日後寫程式碼的過程中不斷優化程式碼,靈活運用所學的知識如(繼承,多型,過載)來減少程式碼的複雜度,提高程式碼的運算效率

(5)總結

 1.通過兩次階段性的題目集,我從中學了很多,比如初步認識了java語言的語法,對日後的學習打下基礎。

 2.對於這兩次階段性的題目集來說,題目本身並不是特別難,做不出的原因主要是自己在課後沒有花足夠的時間在java的學習上,總的來說就是基礎不牢,有句話說的好,基礎不牢,地動山搖,所以我會在之後的學習中加強對基礎知識的複習與新知識的預習,提高學習效率,多翻閱csdn 部落格園上大佬的文獻,豐富自己的閱歷。

 3.這兩次題目集總的來說程式碼較為簡潔,但我寫出來的程式碼過於複雜,巢狀的內容過多,且多數採用列舉法來完成對一個數據的判斷,這是一個不好的現象,這樣寫出來的程式碼只是對題目來說完成了,但是對日後真正的實際應用沒有任何作用,所以在今後我將會將程式碼寫簡潔,變靈活,增強程式碼的可實用性。

4.對於java的學習來說我是有所懈怠的,每次題目集釋出後我都是過幾天后開啟進行作業,這是一種拖拉的現象,在日後我將會盡快完成作業,達到一個自主學習的狀態,提高學習的積極性,為下一主要內容類做出努力。

5.加強日後對於java的複習與鞏固,提高課外的練習水平。